Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

"require-dev": {
"phpunit/phpunit": "~9.5",
"phpstan/phpstan": "~1.10",
"phpstan/phpstan": "~2.0",
"escapestudios/symfony2-coding-standard": "~3.13"
},

Expand Down
9 changes: 4 additions & 5 deletions src/PhpStan/AbstractNoNamedArgumentUsedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node>
Expand Down Expand Up @@ -65,16 +66,14 @@
foreach ($this->forbiddenMethods as $forbiddenClass => $forbiddenMethods) {
$className = $this->getClassName($node, $scope);

if (null !== $className && \is_a($className, $forbiddenClass, true)) {

Check failure on line 69 in src/PhpStan/AbstractNoNamedArgumentUsedRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
$methodName = $this->getMethodName($node);

if ('all' === $forbiddenMethods || \in_array(\strtolower($methodName), $forbiddenMethods, true)) {
if ($this->findNullableName($node)) {
$errors[] = \sprintf(
'The method "%s::%s" is forbidden to call without named arguments.',
$className,
$methodName
);
$errors[] = RuleErrorBuilder::message(\sprintf('The method "%s::%s" is forbidden to call without named arguments.', $className, $methodName))
->identifier('methodCall.withoutNamedArguments.forbidden')
->build();
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/PhpStan/ForbiddenFunctionCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -58,7 +59,11 @@ public function processNode(Node $node, Scope $scope): array
$funcName = $node->name->toString();

if (\in_array($funcName, $this->forbiddenFunctions, true)) {
return [\sprintf('The function "%s" is forbidden for usage.', $funcName)];
return [
RuleErrorBuilder::message(\sprintf('The function "%s" is forbidden for usage.', $funcName))
->identifier('functionCall.forbidden')
->build(),
];
}

return [];
Expand Down
16 changes: 7 additions & 9 deletions src/PhpStan/ForbiddenMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;

/**
Expand Down Expand Up @@ -71,21 +72,18 @@
foreach ($this->forbiddenMethods as $forbiddenClass => $forbiddenMethods) {
$varType = $scope->getType($node->var);

if ($varType instanceof ObjectType) {

Check failure on line 75 in src/PhpStan/ForbiddenMethodCallRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead.
$varClassReflection = $varType->getClassReflection();

if ($varClassReflection && \is_a($varClassReflection->getName(), $forbiddenClass, true)) {

Check failure on line 78 in src/PhpStan/ForbiddenMethodCallRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
if ('all' === $forbiddenMethods) {
$errors[] = \sprintf(
'Any method call from "%s" is forbidden.',
$forbiddenClass
);
$errors[] = RuleErrorBuilder::message(\sprintf('Any method call from "%s" is forbidden.', $forbiddenClass))
->identifier('methodCall.forbidden')
->build();
} elseif (\in_array($node->name->toLowerString(), $forbiddenMethods, true)) {
$errors[] = \sprintf(
'The method "%s::%s" is forbidden to call.',
$varClassReflection->getName(),
$node->name->toString()
);
$errors[] = RuleErrorBuilder::message(\sprintf('The method "%s::%s" is forbidden to call.', $varClassReflection->getName(), $node->name->toString()))
->identifier('methodCall.forbidden')
->build();
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/PhpStan/ForbiddenNodeTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node>
Expand All @@ -40,7 +41,7 @@
*/
public function __construct(string $nodeType, string $message = null)
{
if (!\is_a($nodeType, Node::class, true)) {

Check failure on line 44 in src/PhpStan/ForbiddenNodeTypeRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
throw new \InvalidArgumentException(\sprintf(
'The node type class must implement "%s" interface but "%s" got given.',
Node::class,
Expand All @@ -66,6 +67,10 @@

public function processNode(Node $node, Scope $scope): array
{
return [$this->message];
return [
RuleErrorBuilder::message($this->message)
->identifier('nodeCall.forbidden')
->build(),
];
}
}
7 changes: 6 additions & 1 deletion src/PhpStan/ForbiddenPassArgumentAsReferenceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<Node>
Expand All @@ -34,7 +35,7 @@
*/
public function __construct(string $nodeType)
{
if (!\is_a($nodeType, Node\Stmt\Function_::class, true) && !\is_a($nodeType, Node\Stmt\ClassMethod::class, true)) {

Check failure on line 38 in src/PhpStan/ForbiddenPassArgumentAsReferenceRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.

Check failure on line 38 in src/PhpStan/ForbiddenPassArgumentAsReferenceRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
throw new \InvalidArgumentException(\sprintf(
'Invalid node type "%s". Support only %s or %s.',
$nodeType,
Expand All @@ -60,7 +61,11 @@
{
foreach ($node->getParams() as $param) {
if ($param->byRef) {
return ['Pass arguments by reference is forbidden.'];
return [
RuleErrorBuilder::message('Pass arguments by reference is forbidden.')
->identifier('passArguments.byReference.forbidden')
->build(),
];
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/PhpStan/ForbiddenSuppressErrorRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;

/**
Expand Down Expand Up @@ -93,12 +94,12 @@
if ($node->expr instanceof MethodCall) {
$variableType = $scope->getType($node->expr->var);

if ($variableType instanceof ObjectType) {

Check failure on line 97 in src/PhpStan/ForbiddenSuppressErrorRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Doing instanceof PHPStan\Type\ObjectType is error-prone and deprecated. Use Type::isObject() or Type::getObjectClassNames() instead.
$varClassReflection = $variableType->getClassReflection();

if ($varClassReflection) {
foreach ($this->allowedMethods as $allowedClass => $allowedMethods) {
if (\is_a($varClassReflection->getName(), $allowedClass, true) && \in_array($node->expr->name->toLowerString(), $allowedMethods, true)) {

Check failure on line 102 in src/PhpStan/ForbiddenSuppressErrorRule.php

View workflow job for this annotation

GitHub Actions / Run PHP-Stan

Function is_a() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
return [];
}
}
Expand All @@ -106,6 +107,10 @@
}
}

return ['Suppress error is forbidden.'];
return [
RuleErrorBuilder::message('Suppress error is forbidden.')
->identifier('errorSuppress.forbidden')
->build(),
];
}
}
8 changes: 0 additions & 8 deletions tests/PhpStan/ForbiddenFunctionCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

class ForbiddenFunctionCallRuleTest extends RuleTestCase
{
/**
* @var ForbiddenFunctionCallRule
*/
private ForbiddenFunctionCallRule $rule;

protected function getRule(): ForbiddenFunctionCallRule
Expand All @@ -43,11 +40,6 @@ public function shouldSuccessProcess(array $functions, array $errors): void
$this->analyse([__DIR__.'/Resources/forbidden-functions.php'], [...$errors]);
}

/**
* Provide data for test analyze
*
* @return array
*/
public function provideDataForProcess(): array
{
return [
Expand Down
8 changes: 0 additions & 8 deletions tests/PhpStan/ForbiddenMethodCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

class ForbiddenMethodCallRuleTest extends RuleTestCase
{
/**
* @var ForbiddenMethodCallRule
*/
private ForbiddenMethodCallRule $rule;

protected function getRule(): Rule
Expand All @@ -44,11 +41,6 @@ public function shouldSuccessProcess(array $methods, array $errors): void
$this->analyse([__DIR__.'/Resources/forbidden-methods.php'], [...$errors]);
}

/**
* Provide data for testing
*
* @return array
*/
public function provideDataForTesting(): array
{
return [
Expand Down
3 changes: 0 additions & 3 deletions tests/PhpStan/ForbiddenNodeTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@

class ForbiddenNodeTypeRuleTest extends RuleTestCase
{
/**
* @var ForbiddenNodeTypeRule
*/
private ForbiddenNodeTypeRule $rule;

protected function getRule(): Rule
Expand Down
3 changes: 0 additions & 3 deletions tests/PhpStan/ForbiddenPassArgumentAsReferenceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

class ForbiddenPassArgumentAsReferenceRuleTest extends RuleTestCase
{
/**
* @var ForbiddenPassArgumentAsReferenceRule
*/
private ForbiddenPassArgumentAsReferenceRule $rule;

protected function getRule(): ForbiddenPassArgumentAsReferenceRule
Expand Down
8 changes: 0 additions & 8 deletions tests/PhpStan/ForbiddenSuppressErrorRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

class ForbiddenSuppressErrorRuleTest extends RuleTestCase
{
/**
* @var ForbiddenSuppressErrorRule
*/
private ForbiddenSuppressErrorRule $rule;

protected function getRule(): ForbiddenSuppressErrorRule
Expand All @@ -43,11 +40,6 @@ public function shouldSuccessProcess(array $allowed, array $errors): void
$this->analyse([__DIR__.'/Resources/forbidden-suppress-error.php'], [...$errors]);
}

/**
* Provide data for testing
*
* @return array
*/
public function provideDataForTesting(): array
{
return [
Expand Down
8 changes: 0 additions & 8 deletions tests/PhpStan/NoNamedArgumentUsedConstructorRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

class NoNamedArgumentUsedConstructorRuleTest extends RuleTestCase
{
/**
* @var NoNamedArgumentUsedConstructorRule
*/
private NoNamedArgumentUsedConstructorRule $rule;

protected function getRule(): Rule
Expand All @@ -44,11 +41,6 @@ public function shouldSuccessProcess(array $methods, array $errors): void
$this->analyse([__DIR__.'/Resources/no-named-argument-used-new.php'], [...$errors]);
}

/**
* Provide data for testing
*
* @return array
*/
public function provideDataForTesting(): array
{
return [
Expand Down
8 changes: 0 additions & 8 deletions tests/PhpStan/NoNamedArgumentUsedMethodCallRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@

class NoNamedArgumentUsedMethodCallRuleTest extends RuleTestCase
{
/**
* @var NoNamedArgumentUsedMethodCallRule
*/
private NoNamedArgumentUsedMethodCallRule $rule;

protected function getRule(): Rule
Expand All @@ -44,11 +41,6 @@ public function shouldSuccessProcess(array $methods, array $errors): void
$this->analyse([__DIR__.'/Resources/no-named-argument-used-method-call.php'], [...$errors]);
}

/**
* Provide data for testing
*
* @return array
*/
public function provideDataForTesting(): array
{
return [
Expand Down
Loading