Skip to content
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

Fix union-type handling in PublicStaticPropertyFetchCollector #121

Merged
merged 4 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/Collectors/PublicStaticPropertyFetchCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\Reflection\ClassReflection;
use TomasVotruba\UnusedPublic\ClassTypeDetector;
use TomasVotruba\UnusedPublic\Configuration;

/**
Expand All @@ -18,7 +20,8 @@
final readonly class PublicStaticPropertyFetchCollector implements Collector
{
public function __construct(
private Configuration $configuration
private Configuration $configuration,
private ClassTypeDetector $classTypeDetector,
) {
}

Expand All @@ -33,26 +36,38 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): ?array
{
if (! $this->configuration->isUnusedPropertyEnabled()) {
if (!$this->configuration->isUnusedPropertyEnabled()) {
return null;
}

if (! $node->class instanceof Name) {
if (!$node->name instanceof Identifier) {
return null;
}

if (! $node->name instanceof Identifier) {
if (
$node->class instanceof Name
&& ($node->class->toString() === 'self' || $node->class->toString() === 'static')
) {
// self fetch is allowed
return null;
}

if ($node->class->toString() === 'self') {
// self fetch is allowed
$classReflection = $scope->getClassReflection();
if ($classReflection instanceof ClassReflection && $this->classTypeDetector->isTestClass($classReflection)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this collector was missing the "test-only" detection part

return null;
}

$className = $node->class->toString();
$propertyName = $node->name->toString();
if ($node->class instanceof Name) {
$classType = $scope->resolveTypeByName($node->class);
} else {
$classType = $scope->getType($node->class);
}
$result = [];
foreach($classType->getObjectClassNames() as $className) {
$propertyName = $node->name->toString();
$result[] = $className . '::' . $propertyName;
}

return [$className . '::' . $propertyName];
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rules\UnusedPublicPropertyRule\Fixture;

final class StaticUsedInTestCaseOnly
{
static public $property = 'public static';
}
4 changes: 4 additions & 0 deletions tests/Rules/UnusedPublicPropertyRule/Source/TestCaseUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Source;

use PHPUnit\Framework\TestCase;
use Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInTestCaseOnly;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture\UsedInTestCaseOnly;

final class TestCaseUser extends TestCase
Expand All @@ -13,5 +14,8 @@ private function go()
{
$o = new UsedInTestCaseOnly();
$o->property = 'a value';

$o = new StaticUsedInTestCaseOnly();
$o::$property = 'a value';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInTestCaseOnly;
use TomasVotruba\UnusedPublic\Collectors\PublicPropertyCollector;
use TomasVotruba\UnusedPublic\Collectors\PublicPropertyFetchCollector;
use TomasVotruba\UnusedPublic\Collectors\PublicStaticPropertyFetchCollector;
Expand Down Expand Up @@ -99,6 +100,12 @@
[[$errorMessage1, 7, RuleTips::SOLUTION_MESSAGE]],
];

$errorMessage1 = sprintf(UnusedPublicPropertyRule::ERROR_MESSAGE, StaticUsedInTestCaseOnly::class, 'property');

Check failure on line 103 in tests/Rules/UnusedPublicPropertyRule/UnusedPublicPropertyRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan

Class Rules\UnusedPublicPropertyRule\Fixture\StaticUsedInTestCaseOnly not found.
yield [
[__DIR__ . '/Fixture/StaticUsedInTestCaseOnly.php', __DIR__ . '/Source/TestCaseUser.php'],
[[$errorMessage1, 7, RuleTips::SOLUTION_MESSAGE]],
];

yield [[__DIR__ . '/Fixture/plain.php', __DIR__ . '/Source/PublicPropertyClass.php'], []];

yield [
Expand Down
Loading