-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First class callable syntax supported for functions and methods (#279)
This is the syntax supported by PHP 8.1: ```php $func = print_r(...); $func(); ``` or ```php $obj = new Object(); $func = $obj->method(...); $func(); ``` or ```php $func = Class::method(...); $func(); ``` The errors for the disallowed code are reported on lines with `(...)`, not when the callable is called. Ref #275
- Loading branch information
Showing
26 changed files
with
624 additions
and
135 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
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,67 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Calls; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\FunctionCallableNode; | ||
use PHPStan\Rules\IdentifierRuleError; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedFunctionRuleErrors; | ||
|
||
/** | ||
* Reports on first class callable syntax for a disallowed method. | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<FunctionCallableNode> | ||
*/ | ||
class FunctionFirstClassCallables implements Rule | ||
{ | ||
|
||
private DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors; | ||
|
||
/** @var list<DisallowedCall> */ | ||
private array $disallowedCalls; | ||
|
||
|
||
/** | ||
* @param DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors | ||
* @param DisallowedCallFactory $disallowedCallFactory | ||
* @param array $forbiddenCalls | ||
* @phpstan-param ForbiddenCallsConfig $forbiddenCalls | ||
* @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function __construct( | ||
DisallowedFunctionRuleErrors $disallowedFunctionRuleErrors, | ||
DisallowedCallFactory $disallowedCallFactory, | ||
array $forbiddenCalls | ||
) { | ||
$this->disallowedFunctionRuleErrors = $disallowedFunctionRuleErrors; | ||
$this->disallowedCalls = $disallowedCallFactory->createFromConfig($forbiddenCalls); | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return FunctionCallableNode::class; | ||
} | ||
|
||
|
||
/** | ||
* @param FunctionCallableNode $node | ||
* @param Scope $scope | ||
* @return list<IdentifierRuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$originalNode = $node->getOriginalNode(); | ||
return $this->disallowedFunctionRuleErrors->get($originalNode, $scope, $this->disallowedCalls); | ||
} | ||
|
||
} |
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,66 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Calls; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\MethodCallableNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors; | ||
|
||
/** | ||
* Reports on first class callable syntax for a disallowed method. | ||
* | ||
* Static callables have a different rule, <code>StaticFirstClassCallables</code> | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<MethodCallableNode> | ||
*/ | ||
class MethodFirstClassCallables implements Rule | ||
{ | ||
|
||
private DisallowedMethodRuleErrors $disallowedMethodRuleErrors; | ||
|
||
/** @var list<DisallowedCall> */ | ||
private array $disallowedCalls; | ||
|
||
|
||
/** | ||
* @param DisallowedMethodRuleErrors $disallowedMethodRuleErrors | ||
* @param DisallowedCallFactory $disallowedCallFactory | ||
* @param array $forbiddenCalls | ||
* @phpstan-param ForbiddenCallsConfig $forbiddenCalls | ||
* @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function __construct(DisallowedMethodRuleErrors $disallowedMethodRuleErrors, DisallowedCallFactory $disallowedCallFactory, array $forbiddenCalls) | ||
{ | ||
$this->disallowedMethodRuleErrors = $disallowedMethodRuleErrors; | ||
$this->disallowedCalls = $disallowedCallFactory->createFromConfig($forbiddenCalls); | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodCallableNode::class; | ||
} | ||
|
||
|
||
/** | ||
* @param MethodCallableNode $node | ||
* @param Scope $scope | ||
* @return list<RuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$originalNode = $node->getOriginalNode(); | ||
return $this->disallowedMethodRuleErrors->get($originalNode->var, $originalNode, $scope, $this->disallowedCalls); | ||
} | ||
|
||
} |
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,66 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\Calls; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\StaticMethodCallableNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors; | ||
|
||
/** | ||
* Reports on first class callable syntax for a disallowed static method. | ||
* | ||
* Dynamic calls have a different rule, <code>MethodFirstClassCallables</code> | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<StaticMethodCallableNode> | ||
*/ | ||
class StaticFirstClassCallables implements Rule | ||
{ | ||
|
||
private DisallowedMethodRuleErrors $disallowedMethodRuleErrors; | ||
|
||
/** @var list<DisallowedCall> */ | ||
private array $disallowedCalls; | ||
|
||
|
||
/** | ||
* @param DisallowedMethodRuleErrors $disallowedMethodRuleErrors | ||
* @param DisallowedCallFactory $disallowedCallFactory | ||
* @param array $forbiddenCalls | ||
* @phpstan-param ForbiddenCallsConfig $forbiddenCalls | ||
* @noinspection PhpUndefinedClassInspection ForbiddenCallsConfig is a type alias defined in PHPStan config | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function __construct(DisallowedMethodRuleErrors $disallowedMethodRuleErrors, DisallowedCallFactory $disallowedCallFactory, array $forbiddenCalls) | ||
{ | ||
$this->disallowedMethodRuleErrors = $disallowedMethodRuleErrors; | ||
$this->disallowedCalls = $disallowedCallFactory->createFromConfig($forbiddenCalls); | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return StaticMethodCallableNode::class; | ||
} | ||
|
||
|
||
/** | ||
* @param StaticMethodCallableNode $node | ||
* @param Scope $scope | ||
* @return list<RuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$originalNode = $node->getOriginalNode(); | ||
return $this->disallowedMethodRuleErrors->get($originalNode->class, $originalNode, $scope, $this->disallowedCalls); | ||
} | ||
|
||
} |
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 Spaze\PHPStan\Rules\Disallowed; | ||
|
||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* Provides compatibility layer when running on PHPStan 1.x. | ||
* The whole file can be removed when PHPStan 2.x is required in `composer.json`. | ||
* This file is ignored in `phpstan.neon` in the `excludePaths.analyse` entry. | ||
*/ | ||
class PHPStan1Compatibility | ||
{ | ||
|
||
/** | ||
* @see https://github.com/phpstan/phpstan/blob/2.0.x/UPGRADING.md#minor-backward-compatibility-breaks-1:~:text=Rename%20Type%3A%3AisClassStringType()%20to%20Type%3A%3AisClassString() | ||
*/ | ||
public static function isClassString(Type $type): TrinaryLogic | ||
{ | ||
if (method_exists($type, 'isClassStringType')) { | ||
// PHPStan 1.x | ||
return $type->isClassStringType(); | ||
} else { | ||
// PHPStan 2.x | ||
return $type->isClassString(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.