-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d460ac
commit 9697760
Showing
5 changed files
with
139 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\ClassPropertyNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\CallableType; | ||
use PHPStan\Type\IntersectionType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeTraverser; | ||
use PHPStan\Type\UnionType; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<ClassPropertyNode> | ||
*/ | ||
class InvalidCallablePropertyTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassPropertyNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $node->getClassReflection(); | ||
$propertyReflection = $classReflection->getNativeProperty($node->getName()); | ||
|
||
if (!$propertyReflection->hasNativeType()) { | ||
return []; | ||
} | ||
|
||
$nativeType = $propertyReflection->getNativeType(); | ||
$callableTypes = []; | ||
|
||
TypeTraverser::map($nativeType, static function (Type $type, callable $traverse) use (&$callableTypes): Type { | ||
if ($type instanceof UnionType || $type instanceof IntersectionType) { | ||
return $traverse($type); | ||
} | ||
|
||
if ($type instanceof CallableType) { | ||
$callableTypes[] = $type; | ||
} | ||
|
||
return $type; | ||
}); | ||
|
||
if ($callableTypes === []) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Property %s::$%s cannot have callable in its type declaration.', | ||
$classReflection->getDisplayName(), | ||
$node->getName(), | ||
))->identifier('property.callableType')->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
tests/PHPStan/Rules/Properties/InvalidCallablePropertyTypeRuleTest.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,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<InvalidCallablePropertyTypeRule> | ||
*/ | ||
class InvalidCallablePropertyTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new InvalidCallablePropertyTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/invalid-callable-property-type.php'], [ | ||
[ | ||
'Property InvalidCallablePropertyType\HelloWorld::$a cannot have callable in its type declaration.', | ||
9, | ||
], | ||
[ | ||
'Property InvalidCallablePropertyType\HelloWorld::$b cannot have callable in its type declaration.', | ||
12, | ||
], | ||
[ | ||
'Property InvalidCallablePropertyType\HelloWorld::$c cannot have callable in its type declaration.', | ||
15, | ||
], | ||
[ | ||
'Property InvalidCallablePropertyType\HelloWorld::$callback cannot have callable in its type declaration.', | ||
23, | ||
], | ||
]); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
tests/PHPStan/Rules/Properties/data/invalid-callable-property-type.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,31 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace InvalidCallablePropertyType; | ||
|
||
class HelloWorld | ||
{ | ||
|
||
/** @var callable(): void */ | ||
public callable $a; | ||
|
||
/** @var callable(): void|null */ | ||
public ?callable $b; | ||
|
||
/** @var callable(): void|string */ | ||
public callable|string $c; | ||
|
||
/** | ||
* @param \Closure(): void $closure | ||
* @param callable(): void $callback | ||
*/ | ||
public function __construct( | ||
\Closure $closure, | ||
public callable $callback | ||
) | ||
{ | ||
$this->a = $closure; | ||
$this->b = $closure; | ||
$this->c = $closure; | ||
} | ||
|
||
} |