-
Notifications
You must be signed in to change notification settings - Fork 462
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - check too wide private property type
- Loading branch information
1 parent
9488c63
commit 7453f4f
Showing
11 changed files
with
342 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Node\Property; | ||
|
||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\PropertyAssignNode; | ||
|
||
/** | ||
* @api | ||
*/ | ||
final class PropertyAssign | ||
{ | ||
|
||
public function __construct( | ||
private PropertyAssignNode $assign, | ||
private Scope $scope, | ||
) | ||
{ | ||
} | ||
|
||
public function getAssign(): PropertyAssignNode | ||
{ | ||
return $this->assign; | ||
} | ||
|
||
public function getScope(): Scope | ||
{ | ||
return $this->scope; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\TooWideTypehints; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\ClassPropertiesNode; | ||
use PHPStan\Reflection\PropertyReflection; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function count; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<ClassPropertiesNode> | ||
*/ | ||
final class TooWidePropertyTypeRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private ReadWritePropertiesExtensionProvider $extensionProvider, | ||
private PropertyReflectionFinder $propertyReflectionFinder, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassPropertiesNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$errors = []; | ||
$classReflection = $node->getClassReflection(); | ||
|
||
foreach ($node->getProperties() as $property) { | ||
if (!$property->isPrivate()) { | ||
continue; | ||
} | ||
if ($property->isDeclaredInTrait()) { | ||
continue; | ||
} | ||
if ($property->isPromoted()) { | ||
continue; | ||
} | ||
$propertyName = $property->getName(); | ||
if (!$classReflection->hasNativeProperty($propertyName)) { | ||
continue; | ||
} | ||
|
||
$propertyReflection = $classReflection->getNativeProperty($propertyName); | ||
$propertyType = $propertyReflection->getWritableType(); | ||
if (!$propertyType instanceof UnionType) { | ||
continue; | ||
} | ||
foreach ($this->extensionProvider->getExtensions() as $extension) { | ||
if ($extension->isAlwaysWritten($propertyReflection, $propertyName)) { | ||
continue 2; | ||
} | ||
if ($extension->isInitialized($propertyReflection, $propertyName)) { | ||
continue 2; | ||
} | ||
} | ||
|
||
$assignedTypes = []; | ||
foreach ($node->getPropertyAssigns() as $assign) { | ||
$assignNode = $assign->getAssign(); | ||
$assignPropertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($assignNode->getPropertyFetch(), $assign->getScope()); | ||
foreach ($assignPropertyReflections as $assignPropertyReflection) { | ||
if ($propertyName !== $assignPropertyReflection->getName()) { | ||
continue; | ||
} | ||
if ($propertyReflection->getDeclaringClass()->getName() !== $assignPropertyReflection->getDeclaringClass()->getName()) { | ||
continue; | ||
} | ||
|
||
$assignedTypes[] = $assignPropertyReflection->getScope()->getType($assignNode->getAssignedExpr()); | ||
} | ||
} | ||
|
||
if ($property->getDefault() !== null) { | ||
$assignedTypes[] = $scope->getType($property->getDefault()); | ||
} | ||
|
||
if (count($assignedTypes) === 0) { | ||
continue; | ||
} | ||
|
||
$assignedType = TypeCombinator::union(...$assignedTypes); | ||
$propertyDescription = $this->describePropertyByName($propertyReflection, $propertyName); | ||
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($propertyType, $assignedType); | ||
foreach ($propertyType->getTypes() as $type) { | ||
if (!$type->isSuperTypeOf($assignedType)->no()) { | ||
continue; | ||
} | ||
|
||
if ($property->getNativeType() === null && (new NullType())->isSuperTypeOf($type)->yes()) { | ||
continue; | ||
} | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'%s (%s) is never assigned %s so it can be removed from the property type.', | ||
$propertyDescription, | ||
$propertyType->describe($verbosityLevel), | ||
$type->describe($verbosityLevel), | ||
)) | ||
->identifier('property.unusedType') | ||
->line($property->getStartLine()) | ||
->build(); | ||
} | ||
|
||
} | ||
return $errors; | ||
} | ||
|
||
private function describePropertyByName(PropertyReflection $property, string $propertyName): string | ||
{ | ||
if (!$property->isStatic()) { | ||
return sprintf('Property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName); | ||
} | ||
|
||
return sprintf('Static property %s::$%s', $property->getDeclaringClass()->getDisplayName(), $propertyName); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
tests/PHPStan/Rules/TooWideTypehints/TooWidePropertyTypeRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\TooWideTypehints; | ||
|
||
use PHPStan\Rules\Properties\DirectReadWritePropertiesExtensionProvider; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<TooWidePropertyTypeRule> | ||
*/ | ||
class TooWidePropertyTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new TooWidePropertyTypeRule( | ||
new DirectReadWritePropertiesExtensionProvider([]), | ||
new PropertyReflectionFinder(), | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
self::markTestSkipped('Test requires PHP 8.0.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/too-wide-property-type.php'], [ | ||
[ | ||
'Property TooWidePropertyType\Foo::$foo (int|string) is never assigned string so it can be removed from the property type.', | ||
9, | ||
], | ||
/*[ | ||
'Property TooWidePropertyType\Foo::$barr (int|null) is never assigned null so it can be removed from the property type.', | ||
15, | ||
], | ||
[ | ||
'Property TooWidePropertyType\Foo::$barrr (int|null) is never assigned null so it can be removed from the property type.', | ||
18, | ||
],*/ | ||
[ | ||
'Property TooWidePropertyType\Foo::$baz (int|null) is never assigned null so it can be removed from the property type.', | ||
20, | ||
], | ||
[ | ||
'Property TooWidePropertyType\Bar::$c (int|null) is never assigned int so it can be removed from the property type.', | ||
45, | ||
], | ||
[ | ||
'Property TooWidePropertyType\Bar::$d (int|null) is never assigned null so it can be removed from the property type.', | ||
47, | ||
], | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.