2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodingStyle\Rector\ClassMethod;
|
2019-07-14 12:54:02 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use PhpParser\Node\Stmt\Nop;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-07-14 12:54:02 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\NewlineBeforeNewAssignSetRectorTest
|
2019-07-14 12:54:02 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class NewlineBeforeNewAssignSetRector extends AbstractRector
|
2019-07-14 12:54:02 +00:00
|
|
|
{
|
2019-07-16 13:35:07 +00:00
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
private $previousStmtVariableName;
|
|
|
|
/**
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
private $previousPreviousStmtVariableName;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-07-14 12:54:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Add extra space before new assign set', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-07-14 12:54:02 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = new Value;
|
|
|
|
$value->setValue(5);
|
|
|
|
$value2 = new Value;
|
|
|
|
$value2->setValue(1);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-07-14 12:54:02 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$value = new Value;
|
|
|
|
$value->setValue(5);
|
|
|
|
|
|
|
|
$value2 = new Value;
|
|
|
|
$value2->setValue(1);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-07-14 12:54:02 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-07-14 12:54:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class];
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod|Function_|Closure $node
|
2019-07-14 12:54:02 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-07-14 12:54:02 +00:00
|
|
|
{
|
2021-06-14 14:16:03 +00:00
|
|
|
// skip methods with no bodies (e.g interface methods)
|
|
|
|
if ($node->stmts === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-07-16 13:35:07 +00:00
|
|
|
$this->reset();
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasChanged = \false;
|
2021-06-14 14:16:03 +00:00
|
|
|
$newStmts = [];
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
2020-06-18 09:49:37 +00:00
|
|
|
$currentStmtVariableName = $this->resolveCurrentStmtVariableName($stmt);
|
2019-07-16 13:35:07 +00:00
|
|
|
if ($this->shouldAddEmptyLine($currentStmtVariableName, $node, $key)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$hasChanged = \true;
|
2021-06-14 14:16:03 +00:00
|
|
|
// insert newline before stmt
|
2022-06-07 08:22:29 +00:00
|
|
|
$newStmts[] = new Nop();
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
2021-06-14 14:16:03 +00:00
|
|
|
$newStmts[] = $stmt;
|
2019-07-16 13:35:07 +00:00
|
|
|
$this->previousPreviousStmtVariableName = $this->previousStmtVariableName;
|
|
|
|
$this->previousStmtVariableName = $currentStmtVariableName;
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
2021-06-14 14:16:03 +00:00
|
|
|
$node->stmts = $newStmts;
|
2020-05-03 14:16:10 +00:00
|
|
|
return $hasChanged ? $node : null;
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function reset() : void
|
2019-07-16 13:35:07 +00:00
|
|
|
{
|
|
|
|
$this->previousStmtVariableName = null;
|
|
|
|
$this->previousPreviousStmtVariableName = null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveCurrentStmtVariableName(Stmt $stmt) : ?string
|
2019-10-30 09:49:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmt instanceof Expression) {
|
2022-04-28 21:55:05 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$stmtExpr = $stmt->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($stmtExpr instanceof Assign || $stmtExpr instanceof MethodCall) {
|
2022-04-28 21:55:05 +00:00
|
|
|
if ($this->shouldSkipLeftVariable($stmtExpr)) {
|
2020-08-05 20:45:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$stmtExpr->var instanceof MethodCall && !$stmtExpr->var instanceof StaticCall) {
|
2022-04-28 21:55:05 +00:00
|
|
|
return $this->getName($stmtExpr->var);
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2019-07-16 13:35:07 +00:00
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
2019-07-16 13:35:07 +00:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
private function shouldAddEmptyLine(?string $currentStmtVariableName, $node, int $key) : bool
|
2019-07-16 13:35:07 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isNewVariableThanBefore($currentStmtVariableName)) {
|
|
|
|
return \false;
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
|
|
|
// this is already empty line before
|
2021-06-14 14:16:03 +00:00
|
|
|
return !$this->isPrecededByEmptyLine($node, $key);
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Expr\Assign|\PhpParser\Node\Expr\MethodCall $node
|
2020-08-05 20:45:36 +00:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
private function shouldSkipLeftVariable($node) : bool
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->var instanceof Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-03-19 23:11:36 +00:00
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
// local method call
|
2021-03-19 23:11:36 +00:00
|
|
|
return $this->nodeNameResolver->isName($node->var, 'this');
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
private function isNewVariableThanBefore(?string $currentStmtVariableName) : bool
|
2019-07-14 12:54:02 +00:00
|
|
|
{
|
2019-07-16 13:35:07 +00:00
|
|
|
if ($this->previousPreviousStmtVariableName === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
|
|
|
if ($this->previousStmtVariableName === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
|
|
|
if ($currentStmtVariableName === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|
2019-07-16 13:35:07 +00:00
|
|
|
if ($this->previousStmtVariableName !== $this->previousPreviousStmtVariableName) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
|
|
|
return $this->previousStmtVariableName !== $currentStmtVariableName;
|
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $node
|
2019-07-16 13:35:07 +00:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
private function isPrecededByEmptyLine($node, int $key) : bool
|
2019-07-16 13:35:07 +00:00
|
|
|
{
|
|
|
|
if ($node->stmts === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
|
|
|
$previousNode = $node->stmts[$key - 1];
|
|
|
|
$currentNode = $node->stmts[$key];
|
2021-05-09 20:15:43 +00:00
|
|
|
return \abs($currentNode->getLine() - $previousNode->getLine()) >= 2;
|
2019-07-16 13:35:07 +00:00
|
|
|
}
|
2019-07-14 12:54:02 +00:00
|
|
|
}
|