Skip to content

Commit

Permalink
Fix union-type handling in PublicPropertyFetchCollector (#120)
Browse files Browse the repository at this point in the history
* Fix union-type handling in public-properties

* simplify
  • Loading branch information
staabm authored Jul 15, 2024
1 parent 21592f0 commit da62379
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Collectors/PublicPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,18 @@ public function processNode(Node $node, Scope $scope): ?array
return null;
}

$propertyFetcherType = $scope->getType($node->var);
if (! $propertyFetcherType instanceof TypeWithClassName) {
return null;
}

$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $this->classTypeDetector->isTestClass($classReflection)) {
return null;
}

$className = $propertyFetcherType->getClassName();
$propertyName = $node->name->toString();
$result = [];
$propertyFetcherType = $scope->getType($node->var);
foreach($propertyFetcherType->getObjectClassReflections() as $classReflection) {
$propertyName = $node->name->toString();
$result[] = $classReflection->getName() . '::' . $propertyName;
}

return [$className . '::' . $propertyName];
return $result;
}
}
9 changes: 9 additions & 0 deletions tests/Rules/UnusedPublicPropertyRule/Fixture/UsedInUnionA.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture;

class UsedInUnionA {
public float $amount;
}
9 changes: 9 additions & 0 deletions tests/Rules/UnusedPublicPropertyRule/Fixture/UsedInUnionB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture;

class UsedInUnionB {
public float $amount;
}
13 changes: 13 additions & 0 deletions tests/Rules/UnusedPublicPropertyRule/Source/UsedInUnion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Source;

use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\UsedInUnionA;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\UsedInUnionB;

function doFooBar(UsedInUnionA|UsedInUnionB $aOrB): void {
echo $aOrB->amount;
}

13 changes: 13 additions & 0 deletions tests/Rules/UnusedPublicPropertyRule/Source/UsedInUnionPhpdoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Source;

use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\UsedInUnionA;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\UsedInUnionB;

/** @param UsedInUnionA|UsedInUnionB $aOrB */
function doFoo($aOrB): void {
echo $aOrB->amount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public static function provideData(): Iterator
],
[],
];

yield [[__DIR__ . '/Fixture/UsedInUnionA.php', __DIR__ . '/Fixture/UsedInUnionB.php', __DIR__ . '/Source/UsedInUnion.php'], []];
yield [[__DIR__ . '/Fixture/UsedInUnionA.php', __DIR__ . '/Fixture/UsedInUnionB.php', __DIR__ . '/Source/UsedInUnionPhpdoc.php'], []];

}

/**
Expand Down

0 comments on commit da62379

Please sign in to comment.