-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect first class callable syntax calls
This is the syntax supported by PHP 8.1: ``` $callable = print_r(...); $callable(42); ``` or ``` $blade = new \Waldo\Quux\Blade(); $callable = $blade->runner(...); $callable(303); ``` The errors for the disallowed code are reported on lines with `(...)`, not when the callable is called. Ref #275
- Loading branch information
Showing
23 changed files
with
586 additions
and
132 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
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); | ||
} | ||
|
||
} |
Oops, something went wrong.