Repository navigation
AddTypeToConst: allow adding type to parent class if child does not override #5979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7565bde
AddTypeToConst allow ignoring inheritance
ndench ee0ad91
rename ignoreInheritance to allowHasChild
ndench 75dabd3
Add failing test for inheritance
ndench 364ba53
Only type constants without child overrides
ndench b6c49a5
Undo Dockerfile changes
ndench 9742ede
CI run
ndench da761bd
Update documentation for allow_has_child
ndench 092bf6c
Rename IgnoreInheritance tests to AllowHasChild
ndench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/AllowHasChildRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class AllowHasChildRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/FixtureAllowHasChild'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/allow_has_child.php'; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
...ypeToConstRector/FixtureAllowHasChild/apply_to_public_const_when_no_child_classes.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
|
||
| class ApplyTypeWhenNoChildren | ||
| { | ||
| private const A = 1000; | ||
| public const B = 'foo'; | ||
| } | ||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
|
||
| class ApplyTypeWhenNoChildren | ||
| { | ||
| private const int A = 1000; | ||
| public const string B = 'foo'; | ||
| } | ||
| ?> |
39 changes: 39 additions & 0 deletions
39
...eToConstRector/FixtureAllowHasChild/apply_type_to_class_const_when_not_overridden.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
|
||
| class ApplyTypeWhenNotOverridden | ||
| { | ||
| public const STRING = 'some_type'; | ||
| public const INT = 1; | ||
| protected const bool = true; | ||
| protected const array = []; | ||
| } | ||
|
|
||
| class Child extends ApplyTypeWhenNotOverridden | ||
| { | ||
| public const STRING = 'some_type'; | ||
| protected const bool = true; | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture; | ||
|
|
||
| class ApplyTypeWhenNotOverridden | ||
| { | ||
| public const STRING = 'some_type'; | ||
| public const int INT = 1; | ||
| protected const bool = true; | ||
| protected const array array = []; | ||
| } | ||
|
|
||
| class Child extends ApplyTypeWhenNotOverridden | ||
| { | ||
| public const STRING = 'some_type'; | ||
| protected const bool = true; | ||
| } | ||
|
|
||
| ?> |
15 changes: 15 additions & 0 deletions
15
rules-tests/Php83/Rector/ClassConst/AddTypeToConstRector/config/allow_has_child.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\Php83\Rector\ClassConst\AddTypeToConstRector; | ||
| use Rector\ValueObject\PhpVersion; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->ruleWithConfiguration(AddTypeToConstRector::class, [ | ||
| AddTypeToConstRector::ALLOW_HAS_CHILD => true, | ||
| ]); | ||
|
|
||
| $rectorConfig->phpVersion(PhpVersion::PHP_83); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,29 +20,43 @@ | |
| use PhpParser\Node\Stmt\ClassConst; | ||
| use PHPStan\Reflection\ClassReflection; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
| use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer; | ||
| use Rector\PhpParser\AstResolver; | ||
| use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; | ||
| use Rector\Rector\AbstractRector; | ||
| use Rector\StaticTypeMapper\StaticTypeMapper; | ||
| use Rector\ValueObject\PhpVersionFeature; | ||
| use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\AddTypeToConstRectorTest | ||
| */ | ||
| final class AddTypeToConstRector extends AbstractRector implements MinPhpVersionInterface | ||
| final class AddTypeToConstRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface | ||
| { | ||
| /** | ||
| * @api | ||
| * @var string | ||
| */ | ||
| final public const ALLOW_HAS_CHILD = 'allow_has_child'; | ||
|
|
||
| private bool $allowHasChild = false; | ||
|
|
||
| public function __construct( | ||
| private readonly ReflectionProvider $reflectionProvider, | ||
| private readonly StaticTypeMapper $staticTypeMapper | ||
| private readonly StaticTypeMapper $staticTypeMapper, | ||
| private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer, | ||
| private readonly AstResolver $astResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Add type to constants', [ | ||
| new CodeSample( | ||
| new ConfiguredCodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| final class SomeClass | ||
| { | ||
|
|
@@ -57,6 +71,40 @@ final class SomeClass | |
| } | ||
| CODE_SAMPLE | ||
| , | ||
| [ | ||
| AddTypeToConstRector::ALLOW_HAS_CHILD => false, | ||
| ] | ||
| ), | ||
| new ConfiguredCodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class ParentClass extends ChildClass | ||
| { | ||
| public const TYPE = 'some_type'; | ||
| public const OTHER_TYPE = 'other_type'; | ||
| } | ||
|
|
||
| class ChildClass | ||
| { | ||
| public const OTHER_TYPE = 'other_type'; | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class ParentClass extends ChildClass | ||
| { | ||
| public const string TYPE = 'some_type'; | ||
| public const OTHER_TYPE = 'other_type'; | ||
| } | ||
|
|
||
| class ChildClass | ||
| { | ||
| public const OTHER_TYPE = 'other_type'; | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| [ | ||
| AddTypeToConstRector::ALLOW_HAS_CHILD => true, | ||
| ] | ||
| ), | ||
| ]); | ||
| } | ||
|
|
@@ -86,6 +134,7 @@ public function refactor(Node $node): ?Class_ | |
| } | ||
|
|
||
| $parentClassReflections = $this->getParentReflections($className); | ||
| $childClassReflections = $this->getChildClassReflections($className); | ||
|
|
||
| $hasChanged = false; | ||
|
|
||
|
|
@@ -103,7 +152,11 @@ public function refactor(Node $node): ?Class_ | |
| continue; | ||
| } | ||
|
|
||
| if ($this->canBeInherited($classConst, $node)) { | ||
| if (! $this->allowHasChild && $this->canBeInherited($classConst, $node)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($this->allowHasChild && $this->isConstGuardedByChildren($constNode, $childClassReflections)) { | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -139,6 +192,16 @@ public function refactor(Node $node): ?Class_ | |
| return $node; | ||
| } | ||
|
|
||
| /** | ||
| * @param mixed[] $configuration | ||
| */ | ||
| public function configure(array $configuration): void | ||
| { | ||
| $ignoreInheritance = $configuration[self::ALLOW_HAS_CHILD] ?? (bool) current($configuration); | ||
| Assert::boolean($ignoreInheritance); | ||
| $this->allowHasChild = $ignoreInheritance; | ||
| } | ||
|
|
||
| public function provideMinPhpVersion(): int | ||
| { | ||
| return PhpVersionFeature::TYPED_CLASS_CONSTANTS; | ||
|
|
@@ -160,6 +223,29 @@ public function isConstGuardedByParents(Const_ $const, array $parentClassReflect | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param ClassReflection[] $childClassReflections | ||
| */ | ||
| private function isConstGuardedByChildren(Const_ $const, array $childClassReflections): bool | ||
| { | ||
| foreach ($childClassReflections as $childClassReflection) { | ||
| $classLike = $this->astResolver->resolveClassFromClassReflection($childClassReflection); | ||
| if (! $classLike instanceof Class_) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($classLike->getConstants() as $childClassConstants) { | ||
| foreach ($childClassConstants->consts as $childConst) { | ||
| if ($this->nodeComparator->areNodesEqual($childConst, $const)) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function findValueType(Expr $expr): ?Identifier | ||
| { | ||
| if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) { | ||
|
|
@@ -220,6 +306,20 @@ private function getParentReflections(string $className): array | |
| $currentClassReflection !== $classReflection); | ||
| } | ||
|
|
||
| /** | ||
| * @return ClassReflection[] | ||
| */ | ||
| private function getChildClassReflections(string $className): array | ||
| { | ||
| if (! $this->reflectionProvider->hasClass($className)) { | ||
| return []; | ||
| } | ||
|
|
||
| $currentClassReflection = $this->reflectionProvider->getClass($className); | ||
|
|
||
| return $this->familyRelationsAnalyzer->getChildrenOfClassReflection($currentClassReflection); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the method removed at PR: so this change no longer valid, Please rebase. You may can revert to original |
||
| } | ||
|
|
||
| private function canBeInherited(ClassConst $classConst, Class_ $class): bool | ||
| { | ||
| return ! $class->isFinal() && ! $classConst->isPrivate() && ! $classConst->isFinal(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.