Skip to content

Commit

Permalink
Add typed property, if traits do not duplicate the property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 9, 2025
1 parent 1c39637 commit 1dd1843
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class IncludeTraitAsWell
{
use TraitUsingEntityManager;

private EntityManagerInterface $entityManager;
private \Doctrine\ORM\EntityManagerInterface $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
Expand Down
2 changes: 1 addition & 1 deletion rules/Php74/Guard/PropertyTypeChangeGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function isLegal(

$propertyName = $this->nodeNameResolver->getName($property);

if ($this->propertyManipulator->isUsedByTrait($classReflection, $propertyName)) {
if ($this->propertyManipulator->hasTraitWithSamePropertyOrWritten($classReflection, $propertyName)) {
return false;
}

Expand Down
6 changes: 1 addition & 5 deletions scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ static function (string $filePath, string $prefix, string $content): string {
return $content;
}

return str_replace(
"'" . $prefix . "\\",
"'\\",
$content
);
return str_replace("'" . $prefix . '\\', "'\\", $content);
},

static function (string $filePath, string $prefix, string $content): string {
Expand Down
18 changes: 18 additions & 0 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ public function containsLocalPropertyFetchName(Trait_ $trait, string $propertyNa
);
}

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
22 changes: 22 additions & 0 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ 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;
}

private function isPropertyAssignedOnlyInConstructor(
Class_ $class,
string $propertyName,
Expand Down

0 comments on commit 1dd1843

Please sign in to comment.