diff --git a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php b/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php deleted file mode 100644 index 4d9e16b7fa2..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/AddPropertyTypeDeclarationRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/include_trait_property.php.inc b/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/include_trait_property.php.inc deleted file mode 100644 index 255d846b7dc..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/include_trait_property.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/skip_already_added_type.php.inc b/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/skip_already_added_type.php.inc deleted file mode 100644 index 29f70a91d6f..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Fixture/skip_already_added_type.php.inc +++ /dev/null @@ -1,10 +0,0 @@ - ------ - diff --git a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Source/ParentClassWithProperty.php b/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Source/ParentClassWithProperty.php deleted file mode 100644 index d6aacf92cc8..00000000000 --- a/rules-tests/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector/Source/ParentClassWithProperty.php +++ /dev/null @@ -1,10 +0,0 @@ -ruleWithConfiguration(AddPropertyTypeDeclarationRector::class, [ - new AddPropertyTypeDeclaration(ParentClassWithProperty::class, 'name', new StringType()), - new AddPropertyTypeDeclaration(SomeTraitWithProperty::class, 'value', new IntegerType()), - ]); -}; diff --git a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php index 0788e31324c..c88320988cc 100644 --- a/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php +++ b/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php @@ -6,34 +6,20 @@ use PhpParser\Node; use PhpParser\Node\Stmt\Property; -use PHPStan\Reflection\ClassReflection; use PHPStan\Type\StringType; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; use Rector\Contract\Rector\ConfigurableRectorInterface; use Rector\Exception\ShouldNotHappenException; -use Rector\PHPStan\ScopeFetcher; -use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\ConfiguredCodeSample; use Rector\RuleDoc\RuleDefinition; -use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration; -use Webmozart\Assert\Assert; /** - * @see \Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\AddPropertyTypeDeclarationRectorTest + * @deprecated This rule is deprecated, as too niche and never used, not part of any set. Write a custom rule for the exact project case instead. */ -final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface +final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface { - /** - * @var array - */ - private array $addPropertyTypeDeclarationsByProperty = []; - - public function __construct( - private readonly StaticTypeMapper $staticTypeMapper - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Add type to property by added rules, mostly public/property by parent type', [ @@ -71,41 +57,10 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - // type is already known - if ($node->type !== null) { - return null; - } - - $scope = ScopeFetcher::fetch($node); - $classReflection = $scope->getClassReflection(); - if (! $classReflection instanceof ClassReflection) { - return null; - } - - $propertyName = $this->getName($node); - if ($propertyName === null) { - return null; - } - - foreach ($this->addPropertyTypeDeclarationsByProperty[strtolower($propertyName)] ?? [] as $addPropertyTypeDeclaration) { - if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) { - continue; - } - - $typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode( - $addPropertyTypeDeclaration->getType(), - TypeKind::PROPERTY - ); - if (! $typeNode instanceof Node) { - // invalid configuration - throw new ShouldNotHappenException(); - } - - $node->type = $typeNode; - return $node; - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead', + self::class + )); } /** @@ -113,20 +68,5 @@ public function refactor(Node $node): ?Node */ public function configure(array $configuration): void { - Assert::allIsAOf($configuration, AddPropertyTypeDeclaration::class); - - $this->addPropertyTypeDeclarationsByProperty = []; - foreach ($configuration as $addPropertyTypeDeclaration) { - $this->addPropertyTypeDeclarationsByProperty[strtolower($addPropertyTypeDeclaration->getPropertyName())][] = $addPropertyTypeDeclaration; - } - } - - private function isClassReflectionType(ClassReflection $classReflection, string $type): bool - { - if ($classReflection->hasTraitUse($type)) { - return true; - } - - return $classReflection->is($type); } } diff --git a/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php b/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php index 538f1ea9050..69d4310ac26 100644 --- a/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php +++ b/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php @@ -7,6 +7,9 @@ use PHPStan\Type\Type; use Rector\Validation\RectorAssert; +/** + * @api used in deprecated AddPropertyTypeDeclarationRector configs + */ final readonly class AddPropertyTypeDeclaration { public function __construct(