Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/set/phpstan/phpstan.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
services:
Rector\PHPStan\Rector\Cast\RecastingRemovalRector: ~
Rector\PHPStan\Rector\Assign\PHPStormVarAnnotationRector: ~
Rector\PHPStan\Rector\Node\RemoveNonExistingVarAnnotationRector: ~
1 change: 1 addition & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ parameters:

Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
# tough logic
- 'packages/PHPStan/src/Rector/Node/RemoveNonExistingVarAnnotationRector.php'
- 'packages/Architecture/src/Rector/Class_/ConstructorInjectionToActionInjectionRector.php'
- 'src/PhpParser/Node/Commander/NodeRemovingCommander.php'
- 'packages/BetterPhpDocParser/src/*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function getNodeTypes(): array
}

/**
* @param Node\Stmt\Class_ $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ final class RemoveRepositoryFromEntityAnnotationRectorTest extends AbstractRecto
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/skip_done.php.inc',
]);
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/skip_done.php.inc']);
}

protected function getRectorClass(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function refactor(Node $node): ?Node
return null;
}

$node->args[2] = new Node\Arg($this->createTrue());
$node->args[2] = new Arg($this->createTrue());

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Rector\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -76,7 +78,7 @@ public function refactor(Node $node): ?Node
$variable = $node->right->args[0]->value;
}

/** @var Node\Expr $variable */
return new Identical($variable, new Node\Scalar\String_(''));
/** @var Expr $variable */
return new Identical($variable, new String_(''));
}
}
1 change: 0 additions & 1 deletion packages/ContributorTools/src/Command/DumpNodesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

/** @var Node $node */
$contructorReflection = $nodeClassReflection->getConstructor();

if ($contructorReflection->getNumberOfRequiredParameters() === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

_Source_

/**
* @see \Rector\_Package_\Tests\Rector\_Category_\_Name_\_Name_Test
*/
final class _Name_ extends AbstractRector
{
public function getDefinition(): RectorDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ private function resolveFullyQualifiedName(Node $nameNode, string $name): ?strin
return $class;
}

// @todo add "parent"

/** @var Name|null $name */
/** @var Name|null $resolvedNameNode */
$resolvedNameNode = $nameNode->getAttribute(AttributeKey::RESOLVED_NAME);
if ($resolvedNameNode instanceof Name) {
return $resolvedNameNode->toString();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php declare(strict_types=1);

namespace Rector\PHPStan\Rector\Node;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\While_;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\AttributeAwareVarTagValueNode;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\RemoveNonExistingVarAnnotationRectorTest;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see RemoveNonExistingVarAnnotationRectorTest
* @see https://github.com/phpstan/phpstan/commit/d17e459fd9b45129c5deafe12bca56f30ea5ee99#diff-9f3541876405623b0d18631259763dc1
*/
final class RemoveNonExistingVarAnnotationRector extends AbstractRector
{
/**
* @var DocBlockManipulator
*/
private $docBlockManipulator;

public function __construct(DocBlockManipulator $docBlockManipulator)
{
$this->docBlockManipulator = $docBlockManipulator;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes non-existing @var annotations above the code', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function get()
{
/** @var Training[] $trainings */
return $this->getData();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function get()
{
return $this->getData();
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Node::class];
}

/**
* @param Node $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}

$variableName = $this->getVarTagVariableName($node);
if ($variableName === null) {
return null;
}

// it's there
if (Strings::match($this->print($node), '#' . preg_quote($variableName, '#') . '\b#')) {
return null;
}

$this->docBlockManipulator->removeTagFromNode($node, 'var');

return $node;
}

private function shouldSkip(Node $node): bool
{
return ! $node instanceof Assign
&& ! $node instanceof AssignRef
&& ! $node instanceof Foreach_
&& ! $node instanceof Static_
&& ! $node instanceof Echo_
&& ! $node instanceof Return_
&& ! $node instanceof Expression
&& ! $node instanceof Throw_
&& ! $node instanceof If_
&& ! $node instanceof While_
&& ! $node instanceof Switch_
&& ! $node instanceof Nop;
}

private function getVarTagVariableName(Node $node): ?string
{
if (! $this->docBlockManipulator->hasTag($node, 'var')) {
return null;
}

$varTag = $this->docBlockManipulator->getTagByName($node, 'var');

/** @var AttributeAwareVarTagValueNode $varTagValue */
$varTagValue = $varTag->value;

return $varTagValue->variableName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class SomeClass
{
public function get()
{
/** @var Training[] $trainings */
return $this->getData();
}
}

?>
-----
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class SomeClass
{
public function get()
{
return $this->getData();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class IfCase
{
public function run()
{
/** @var ClassMethod $constructorMethod */
if ($constructorMethod !== null) {
}

/** @var ClassMethod $notHere */
if ($constructorMethod !== null) {
}
}
}

?>
-----
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class IfCase
{
public function run()
{
/** @var ClassMethod $constructorMethod */
if ($constructorMethod !== null) {
}

if ($constructorMethod !== null) {
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class Keep
{
/**
* @var string
*/
public const SPLIT_LINE = '#-----\n#';

public function get()
{
/** @var int $trainings */
$trainings = $this->getAnotherData();

return $trainings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class Subcontent
{
public function get()
{
/** @var int $trainings */
$trainingsLonger = $this->getAnotherData();

return $trainings;
}
}

?>
-----
<?php

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector\Fixture;

class Subcontent
{
public function get()
{
$trainingsLonger = $this->getAnotherData();

return $trainings;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Rector\PHPStan\Tests\Rector\Node\RemoveNonExistingVarAnnotationRector;

use Rector\PHPStan\Rector\Node\RemoveNonExistingVarAnnotationRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveNonExistingVarAnnotationRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/subcontent.php.inc',
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/if_case.php.inc',
__DIR__ . '/Fixture/keep.php.inc',
]);
}

protected function getRectorClass(): string
{
return RemoveNonExistingVarAnnotationRector::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function refactor(Node $node): ?Node
return null;
}

$node->name = new Node\Name('time');
$node->name = new Name('time');

return $node;
}
Expand Down
Loading