Skip to content

Commit

Permalink
Updated Rector to commit 8da1bfe2590771077da7ccd297f5df245d3e55c1
Browse files Browse the repository at this point in the history
rectorphp/rector-src@8da1bfe [type-declaration] Add typed property, if traits do not duplicate the property (#6663)
  • Loading branch information
TomasVotruba committed Jan 9, 2025
1 parent f3e9cbe commit e936f2a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion rules/Php74/Guard/PropertyTypeChangeGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function isLegal(Property $property, ClassReflection $classReflection, bo
return \false;
}
$propertyName = $this->nodeNameResolver->getName($property);
if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
if ($this->propertyManipulator->hasTraitWithSamePropertyOrWritten($classReflection, $propertyName)) {
return \false;
}
if ($this->propertyAnalyzer->hasForbiddenType($property)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'c59c83a99fdb883dbcf6fead62b7cd9961fae034';
public const PACKAGE_VERSION = '8da1bfe2590771077da7ccd297f5df245d3e55c1';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2025-01-07 23:22:40';
public const RELEASE_DATE = '2025-01-09 21:26:38';
/**
* @var int
*/
Expand Down
12 changes: 12 additions & 0 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ public function containsLocalPropertyFetchName(Trait_ $trait, string $propertyNa
}
return (bool) $this->betterNodeFinder->findFirst($trait, fn(Node $node): bool => $this->isLocalPropertyFetchName($node, $propertyName));
}
public function containsWrittenPropertyFetchName(Trait_ $trait, string $propertyName) : bool
{
if ($trait->getProperty($propertyName) instanceof Property) {
return \true;
}
return (bool) $this->betterNodeFinder->findFirst($trait, function (Node $node) use($propertyName) : bool {
if (!$node instanceof Assign) {
return \false;
}
return $this->isLocalPropertyFetchName($node->var, $propertyName);
});
}
/**
* @phpstan-assert-if-true PropertyFetch|StaticPropertyFetch $node
*/
Expand Down
17 changes: 17 additions & 0 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ public function isUsedByTrait(ClassReflection $classReflection, string $property
}
return \false;
}
public function hasTraitWithSamePropertyOrWritten(ClassReflection $classReflection, string $propertyName) : bool
{
foreach ($classReflection->getTraits() as $traitUse) {
if ($traitUse->hasProperty($propertyName)) {
return \true;
}
$trait = $this->astResolver->resolveClassFromClassReflection($traitUse);
if (!$trait instanceof Trait_) {
continue;
}
// is property written to
if ($this->propertyFetchAnalyzer->containsWrittenPropertyFetchName($trait, $propertyName)) {
return \true;
}
}
return \false;
}
/**
* @param \PhpParser\Node\Expr\StaticPropertyFetch|\PhpParser\Node\Expr\PropertyFetch $propertyFetch
*/
Expand Down

0 comments on commit e936f2a

Please sign in to comment.