-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
DisallowedErrorSuppressionOperatorRule
- Loading branch information
1 parent
53a71ac
commit 021857f
Showing
7 changed files
with
167 additions
and
3 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
src/Nexus/PHPStan/Rules/CleanCode/DisallowedErrorSuppressionOperatorRule.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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the Nexus framework. | ||
* | ||
* (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexus\PHPStan\Rules\CleanCode; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\ErrorSuppress> | ||
*/ | ||
final class DisallowedErrorSuppressionOperatorRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\ErrorSuppress::class; | ||
} | ||
|
||
/** | ||
* @param Node\Expr\ErrorSuppress $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ( | ||
$node->expr instanceof Node\Expr\FuncCall | ||
&& $node->expr->name instanceof Node\Name | ||
&& 'trigger_error' === $node->expr->name->name | ||
) { | ||
$arguments = $node->expr->getArgs(); | ||
|
||
if (\count($arguments) > 1) { | ||
$errorType = $scope->getType($arguments[1]->value); | ||
|
||
if ($errorType instanceof ConstantIntegerType) { | ||
$errorLevel = $errorType->getValue(); | ||
|
||
if (E_USER_DEPRECATED === $errorLevel) { | ||
return []; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Use of the error control operator to suppress errors is not allowed.') | ||
->identifier('nexus.errorSuppress') | ||
->addTip('If you need to get the result and error message, use `Silencer::box()` instead.') | ||
->addTip('If you need only the result, use `Silencer::suppress()` instead.') | ||
->build(), | ||
]; | ||
} | ||
} |
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
69 changes: 69 additions & 0 deletions
69
tests/PHPStan/Rules/CleanCode/DisallowedErrorSuppressionOperatorRuleTest.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,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of the Nexus framework. | ||
* | ||
* (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexus\Tests\PHPStan\Rules\CleanCode; | ||
|
||
use Nexus\PHPStan\Rules\CleanCode\DisallowedErrorSuppressionOperatorRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Group; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @extends RuleTestCase<DisallowedErrorSuppressionOperatorRule> | ||
*/ | ||
#[CoversClass(DisallowedErrorSuppressionOperatorRule::class)] | ||
#[Group('unit-test')] | ||
final class DisallowedErrorSuppressionOperatorRuleTest extends RuleTestCase | ||
{ | ||
public function testRule(): void | ||
{ | ||
$tip = implode("\n", array_map( | ||
static fn(string $tip): string => \sprintf('• %s', $tip), | ||
[ | ||
'If you need to get the result and error message, use `Silencer::box()` instead.', | ||
'If you need only the result, use `Silencer::suppress()` instead.', | ||
], | ||
)); | ||
|
||
$this->analyse([__DIR__.'/data/disallowed-error-suppression-operator.php'], [ | ||
[ | ||
'Use of the error control operator to suppress errors is not allowed.', | ||
7, | ||
$tip, | ||
], | ||
[ | ||
'Use of the error control operator to suppress errors is not allowed.', | ||
10, | ||
$tip, | ||
], | ||
[ | ||
'Use of the error control operator to suppress errors is not allowed.', | ||
11, | ||
$tip, | ||
], | ||
[ | ||
'Use of the error control operator to suppress errors is not allowed.', | ||
12, | ||
$tip, | ||
], | ||
]); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DisallowedErrorSuppressionOperatorRule(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/PHPStan/Rules/CleanCode/data/disallowed-error-suppression-operator.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nexus\Tests\PHPStan\Rules\CleanCode; | ||
|
||
@trigger_error('Test', E_USER_WARNING); | ||
@trigger_error('Test 2', E_USER_DEPRECATED); | ||
|
||
$a = @$x; | ||
@mkdir(__DIR__); | ||
@file_get_contents(__FILE__); |
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