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: public function getNodeType(): string
28: {
29: return Node\Expr\ErrorSuppress::class;
30: }
31:
32: public function processNode(Node $node, Scope $scope): array
33: {
34: if (
35: $node->expr instanceof Node\Expr\FuncCall
36: && $node->expr->name instanceof Node\Name
37: && 'trigger_error' === $node->expr->name->name
38: ) {
39: $arguments = $node->expr->getArgs();
40:
41: if (\count($arguments) > 1) {
42: $errorType = $scope->getType($arguments[1]->value);
43:
44: if ($errorType instanceof ConstantIntegerType) {
45: $errorLevel = $errorType->getValue();
46:
47: if (E_USER_DEPRECATED === $errorLevel) {
48: return [];
49: }
50: }
51: }
52: }
53:
54: return [
55: RuleErrorBuilder::message('Use of the error control operator to suppress errors is not allowed.')
56: ->identifier('nexus.errorSuppress')
57: ->addTip('If you need to get the result and error message, use `Silencer::box()` instead.')
58: ->addTip('If you need only the result, use `Silencer::suppress()` instead.')
59: ->build(),
60: ];
61: }
62: }
63: