|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\TypeDeclaration\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PhpParser\Node\Name\FullyQualified; |
| 11 | +use PhpParser\Node\NullableType; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PhpParser\Node\Stmt\Property; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 16 | +use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; |
| 17 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 18 | +use Rector\PHPUnit\Enum\BehatClassName; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 22 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 23 | + |
| 24 | +/** |
| 25 | + * @see \Rector\Tests\TypeDeclaration\Rector\Class_\TypedStaticPropertyInBehatContextRectorTest\TypedStaticPropertyInBehatContextRectorTest |
| 26 | + */ |
| 27 | +final class TypedStaticPropertyInBehatContextRector extends AbstractRector |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 31 | + private readonly VarTagRemover $varTagRemover, |
| 32 | + private readonly ValueResolver $valueResolver, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function getRuleDefinition(): RuleDefinition |
| 37 | + { |
| 38 | + return new RuleDefinition('Add known property types to Behat context static properties', [ |
| 39 | + new CodeSample( |
| 40 | + <<<'CODE_SAMPLE' |
| 41 | +use Behat\Behat\Context\Context; |
| 42 | +
|
| 43 | +final class FeatureContext implements Context |
| 44 | +{ |
| 45 | + /** |
| 46 | + * @var SomeObject |
| 47 | + */ |
| 48 | + public static $someStaticProperty; |
| 49 | +} |
| 50 | +CODE_SAMPLE |
| 51 | + , |
| 52 | + <<<'CODE_SAMPLE' |
| 53 | +use Behat\Behat\Context\Context; |
| 54 | +
|
| 55 | +final class FeatureContext implements Context |
| 56 | +{ |
| 57 | + public static ?SomeObject $someStaticProperty = null; |
| 58 | +} |
| 59 | +CODE_SAMPLE |
| 60 | + ), |
| 61 | + ]); |
| 62 | + } |
| 63 | + |
| 64 | + public function getNodeTypes(): array |
| 65 | + { |
| 66 | + return [Class_::class]; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param Class_ $node |
| 71 | + */ |
| 72 | + public function refactor(Node $node): ?Node |
| 73 | + { |
| 74 | + // no parents |
| 75 | + if (! $node->extends instanceof Name && $node->implements === []) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + if (! $this->isObjectType($node, new ObjectType(BehatClassName::CONTEXT))) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + $hasChanged = false; |
| 84 | + |
| 85 | + foreach ($node->getProperties() as $property) { |
| 86 | + if ($property->type instanceof Node) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (! $property->isStatic()) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if ($this->hasNonNullDefault($property)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property); |
| 99 | + |
| 100 | + $varType = $propertyPhpDocInfo->getVarType(); |
| 101 | + if (! $varType instanceof ObjectType) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + if ($varType instanceof ShortenedObjectType) { |
| 106 | + $className = $varType->getFullyQualifiedName(); |
| 107 | + } else { |
| 108 | + $className = $varType->getClassName(); |
| 109 | + } |
| 110 | + |
| 111 | + $property->type = new NullableType(new FullyQualified($className)); |
| 112 | + |
| 113 | + if (! $property->props[0]->default instanceof Node) { |
| 114 | + $property->props[0]->default = $this->nodeFactory->createNull(); |
| 115 | + } |
| 116 | + |
| 117 | + $this->varTagRemover->removeVarTagIfUseless($propertyPhpDocInfo, $property); |
| 118 | + $hasChanged = true; |
| 119 | + } |
| 120 | + |
| 121 | + if (! $hasChanged) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + return $node; |
| 126 | + } |
| 127 | + |
| 128 | + private function hasNonNullDefault(Property $property): bool |
| 129 | + { |
| 130 | + $soleProperty = $property->props[0]; |
| 131 | + if (! $soleProperty->default instanceof Expr) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + return ! $this->valueResolver->isNull($soleProperty->default); |
| 136 | + } |
| 137 | +} |
0 commit comments