1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus framework.
7: *
8: * (c) John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\PHPStan\Rules\CleanCode;
15:
16: use PhpParser\Node;
17: use PHPStan\Analyser\Scope;
18: use PHPStan\Rules\Rule;
19: use PHPStan\Rules\RuleErrorBuilder;
20: use PHPStan\Type\Constant\ConstantIntegerType;
21:
22: /**
23: * @implements Rule<Node\Expr\ErrorSuppress>
24: */
25: final class DisallowedErrorSuppressionOperatorRule implements Rule
26: {
27: #[\Override]
28: public function getNodeType(): string
29: {
30: return Node\Expr\ErrorSuppress::class;
31: }
32:
33: #[\Override]
34: public function processNode(Node $node, Scope $scope): array
35: {
36: if (
37: $node->expr instanceof Node\Expr\FuncCall
38: && $node->expr->name instanceof Node\Name
39: && 'trigger_error' === $node->expr->name->name
40: ) {
41: $arguments = $node->expr->getArgs();
42:
43: if (\count($arguments) > 1) {
44: $errorType = $scope->getType($arguments[1]->value);
45:
46: if ($errorType instanceof ConstantIntegerType) {
47: $errorLevel = $errorType->getValue();
48:
49: if (E_USER_DEPRECATED === $errorLevel) {
50: return [];
51: }
52: }
53: }
54: }
55:
56: return [
57: RuleErrorBuilder::message('Use of the error control operator to suppress errors is not allowed.')
58: ->identifier('nexus.errorSuppress')
59: ->addTip('If you need to get the result and error message, use `Silencer::box()` instead.')
60: ->addTip('If you need only the result, use `Silencer::suppress()` instead.')
61: ->build(),
62: ];
63: }
64: }
65: