Skip to content

Commit

Permalink
[Php80] Doc mirror var to param improvement on ClassPropertyAssignToC…
Browse files Browse the repository at this point in the history
…onstructorPromotionRector (#1775)

* [Php80] Doc mirror var to param improvement on ClassPropertyAssignToConstructorPromotionRector

* [ci-review] Rector Rectify

* move to constant for re-use dead use detection

* [ci-review] Rector Rectify

* add failing fixture for auto import copy annotation

* implemented copy on auto import

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 8, 2022
1 parent 63425ba commit 50d1978
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
Expand All @@ -18,6 +19,8 @@
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand All @@ -27,6 +30,15 @@

final class PhpDocTypeChanger
{
/**
* @var array<class-string<Node>>
*/
public const ALLOWED_TYPES = [
GenericTypeNode::class,
SpacingAwareArrayTypeNode::class,
SpacingAwareCallableTypeNode::class,
];

public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly TypeComparator $typeComparator,
Expand Down Expand Up @@ -165,7 +177,7 @@ public function copyPropertyDocToParam(Property $property, Param $param): void
return;
}

if (! $varTag->type instanceof GenericTypeNode) {
if (! in_array($varTag->type::class, self::ALLOWED_TYPES, true)) {
return;
}

Expand All @@ -177,6 +189,7 @@ public function copyPropertyDocToParam(Property $property, Param $param): void
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTag, $property);

$this->changeParamType($phpDocInfo, $paramType, $param, $paramVarName);
$this->processKeepComments($property, $param);
}

public function changeVarTypeNode(PhpDocInfo $phpDocInfo, TypeNode $typeNode): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AutoImportTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/auto_import_configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class CopyArrayStringDoc
{
/**
* Comment
* @var string[]
*/
private array $map;

public function __construct(array $map)
{
$this->map = $map;
}
}

?>
-----
<?php declare(strict_types = 1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

final class CopyArrayStringDoc
{
/**
* @param string[] $map
*/
public function __construct(
/**
* Comment
*/
private array $map
)
{
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixtureAutoImport;

use Symfony\Component\Serializer\Annotation\SerializedName;

class CopyAnnotationAndArrayStringDoc
{
/**
* @var string[]
* @SerializedName("owsy/derived-facts")
*/
private array $derivedFacts;

public function __construct(
array $derivedFacts = [],
) {
$this->derivedFacts = $derivedFacts;
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\FixtureAutoImport;

use Symfony\Component\Serializer\Annotation\SerializedName;

class CopyAnnotationAndArrayStringDoc
{
/**
* @param string[] $derivedFacts
*/
public function __construct(
/**
* @SerializedName("owsy/derived-facts")
*/
private array $derivedFacts = []
)
{
}
}

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

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);

$services = $containerConfigurator->services();
$services->set(ClassPropertyAssignToConstructorPromotionRector::class);
};
8 changes: 2 additions & 6 deletions rules/DeadCode/PhpDoc/DeadParamTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\VariadicAwareParamTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand Down Expand Up @@ -56,10 +55,7 @@ public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $funct
return false;
}

if (in_array($paramTagValueNode->type::class, [
GenericTypeNode::class,
SpacingAwareCallableTypeNode::class,
], true)) {
if (in_array($paramTagValueNode->type::class, PhpDocTypeChanger::ALLOWED_TYPES, true)) {
return false;
}

Expand Down
8 changes: 2 additions & 6 deletions rules/DeadCode/PhpDoc/DeadReturnTagValueNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
Expand Down Expand Up @@ -46,10 +45,7 @@ public function isDead(ReturnTagValueNode $returnTagValueNode, FunctionLike $fun
return false;
}

if (in_array($returnTagValueNode->type::class, [
GenericTypeNode::class,
SpacingAwareCallableTypeNode::class,
], true)) {
if (in_array($returnTagValueNode->type::class, PhpDocTypeChanger::ALLOWED_TYPES, true)) {
return false;
}

Expand Down

0 comments on commit 50d1978

Please sign in to comment.