Skip to content

Commit

Permalink
Detect public properties used via Subclass (#123)
Browse files Browse the repository at this point in the history
* Detect public property used via Subclass

* add similar test for static properties

* fix

* fix wrong namespaces
  • Loading branch information
staabm committed Jul 22, 2024
1 parent f8c1349 commit e96d688
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/Collectors/PublicPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ public function processNode(Node $node, Scope $scope): ?array
$propertyFetcherType = $scope->getType($node->var);
foreach($propertyFetcherType->getObjectClassReflections() as $classReflection) {
$propertyName = $node->name->toString();
$result[] = $classReflection->getName() . '::' . $propertyName;

if (!$classReflection->hasProperty($propertyName)) {
continue;
}
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
$result[] = $propertyReflection->getDeclaringClass()->getName() . '::' . $propertyName;
}

return $result;
Expand Down
9 changes: 7 additions & 2 deletions src/Collectors/PublicStaticPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ public function processNode(Node $node, Scope $scope): ?array
$classType = $scope->getType($node->class);
}
$result = [];
foreach($classType->getObjectClassNames() as $className) {
foreach($classType->getObjectClassReflections() as $classReflection) {
$propertyName = $node->name->toString();
$result[] = $className . '::' . $propertyName;

if (!$classReflection->hasProperty($propertyName)) {
continue;
}
$propertyReflection = $classReflection->getProperty($propertyName, $scope);
$result[] = $propertyReflection->getDeclaringClass()->getName() . '::' . $propertyName;
}

return $result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

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

class PropertyUsedViaSubClass
{
private SubClass $prop;

protected function doFoo() {
$this->prop->x = 1;
}
}

class SubClass extends BaseClass
{
}

class BaseClass
{
public int $x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

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

class StaticPropertyUsedViaSubClass
{
private SubClass $prop;

protected function doFoo() {
$this->prop::$x = 1;
}
}

class SubClass extends BaseClass
{
}

class BaseClass
{
static public int $x;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

class StaticUsedInUnionA {
static public float $amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

class StaticUsedInUnionB {
static public float $amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

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

use Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInUnionA;
use Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInUnionB;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInUnionA;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInUnionB;

function doFooBar(StaticUsedInUnionA|StaticUsedInUnionB $aOrB): void {
echo $aOrB::$amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function testRule(array $filePaths, array $expectedErrorMessagesWithLines

public static function provideData(): Iterator
{
yield [[__DIR__ . '/Fixture/PropertyUsedViaSubClass.php'], []];
yield [[__DIR__ . '/Fixture/StaticPropertyUsedViaSubClass.php'], []];

$errorMessage = sprintf(UnusedPublicPropertyRule::ERROR_MESSAGE, LocalyUsedPublicProperty::class, 'name');
yield [[__DIR__ . '/Fixture/LocalyUsedPublicProperty.php'],
[[$errorMessage, 7, RuleTips::SOLUTION_MESSAGE]], ];
Expand Down

0 comments on commit e96d688

Please sign in to comment.