|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) 2023 CodeIgniter Foundation <admin@codeigniter.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 CodeIgniter\PHPStan\Rules\Superglobals; |
| 15 | + |
| 16 | +use CodeIgniter\Superglobals; |
| 17 | +use PhpParser\Node; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\Rules\Rule; |
| 20 | +use PHPStan\Rules\RuleError; |
| 21 | +use PHPStan\Rules\RuleErrorBuilder; |
| 22 | +use PHPStan\Type\VerbosityLevel; |
| 23 | + |
| 24 | +/** |
| 25 | + * @implements Rule<Node\Expr\Assign> |
| 26 | + */ |
| 27 | +final class SuperglobalAssignRule implements Rule |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private readonly SuperglobalRuleHelper $superglobalRuleHelper |
| 31 | + ) {} |
| 32 | + |
| 33 | + public function getNodeType(): string |
| 34 | + { |
| 35 | + return Node\Expr\Assign::class; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param Node\Expr\Assign $node |
| 40 | + */ |
| 41 | + public function processNode(Node $node, Scope $scope): array |
| 42 | + { |
| 43 | + if ($node->var instanceof Node\Expr\ArrayDimFetch) { |
| 44 | + return $this->processArrayDimFetch($node, $scope); |
| 45 | + } |
| 46 | + |
| 47 | + if ($node->var instanceof Node\Expr\Variable) { |
| 48 | + return $this->processVariableExpr($node, $scope); |
| 49 | + } |
| 50 | + |
| 51 | + return []; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param Node\Expr\Assign $node |
| 56 | + * |
| 57 | + * @return RuleError[] |
| 58 | + */ |
| 59 | + private function processArrayDimFetch(Node $node, Scope $scope): array |
| 60 | + { |
| 61 | + assert($node->var instanceof Node\Expr\ArrayDimFetch); |
| 62 | + |
| 63 | + $arrayDimFetch = $node->var; |
| 64 | + |
| 65 | + if ($arrayDimFetch->dim === null) { |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + $dimType = $scope->getType($arrayDimFetch->dim); |
| 70 | + |
| 71 | + if ($dimType->isString()->no()) { |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + if (! $arrayDimFetch->var instanceof Node\Expr\Variable) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + $name = $arrayDimFetch->var->name; |
| 80 | + |
| 81 | + if (! is_string($name)) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $this->superglobalRuleHelper->isHandledSuperglobal($name)) { |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + if ($scope->isInClass() && $scope->getClassReflection()->getName() === Superglobals::class) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + $exprType = $scope->getType($node->expr); |
| 94 | + |
| 95 | + $expr = $exprType->describe(VerbosityLevel::precise()); |
| 96 | + $dim = $dimType->describe(VerbosityLevel::precise()); |
| 97 | + $method = $this->superglobalRuleHelper->getSuperglobalMethodSetter($name); |
| 98 | + |
| 99 | + $addTip = static function (RuleErrorBuilder $ruleErrorBuilder) use ($method, $dimType, $exprType): RuleErrorBuilder { |
| 100 | + if ($dimType->getConstantStrings() !== [] && $exprType->getConstantStrings() !== []) { |
| 101 | + foreach ($dimType->getConstantStrings() as $dimString) { |
| 102 | + foreach ($exprType->getConstantStrings() as $exprString) { |
| 103 | + $ruleErrorBuilder->addTip(sprintf( |
| 104 | + 'Use \\Config\\Services::superglobals()->%s(%s, %s) instead.', |
| 105 | + $method, |
| 106 | + $dimString->describe(VerbosityLevel::precise()), |
| 107 | + $exprString->describe(VerbosityLevel::precise()) |
| 108 | + )); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return $ruleErrorBuilder; |
| 113 | + } |
| 114 | + |
| 115 | + return $ruleErrorBuilder->tip(sprintf('Use \\Config\\Services::superglobals()->%s() instead.', $method)); |
| 116 | + }; |
| 117 | + |
| 118 | + return [ |
| 119 | + $addTip(RuleErrorBuilder::message(sprintf('Assigning %s directly on offset %s of $%s is discouraged.', $expr, $dim, $name))) |
| 120 | + ->identifier('codeigniter.superglobalAccessAssign') |
| 121 | + ->build(), |
| 122 | + ]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @param Node\Expr\Assign $node |
| 127 | + * |
| 128 | + * @return RuleError[] |
| 129 | + */ |
| 130 | + private function processVariableExpr(Node $node, Scope $scope): array |
| 131 | + { |
| 132 | + assert($node->var instanceof Node\Expr\Variable); |
| 133 | + |
| 134 | + $name = $node->var->name; |
| 135 | + |
| 136 | + if (! is_string($name)) { |
| 137 | + return []; |
| 138 | + } |
| 139 | + |
| 140 | + if (! $this->superglobalRuleHelper->isHandledSuperglobal($name)) { |
| 141 | + return []; |
| 142 | + } |
| 143 | + |
| 144 | + if ($name !== '_GET') { |
| 145 | + return []; |
| 146 | + } |
| 147 | + |
| 148 | + $exprType = $scope->getType($node->expr); |
| 149 | + |
| 150 | + if (! $exprType->isArray()->yes()) { |
| 151 | + return [ |
| 152 | + RuleErrorBuilder::message(sprintf('Cannot re-assign non-arrays to $_GET, got %s.', $exprType->describe(VerbosityLevel::typeOnly()))) |
| 153 | + ->identifier('codeigniter.getReassignNonarray') |
| 154 | + ->build(), |
| 155 | + ]; |
| 156 | + } |
| 157 | + |
| 158 | + return [ |
| 159 | + RuleErrorBuilder::message('Re-assigning arrays to $_GET directly is discouraged.') |
| 160 | + ->tip('Use \\Config\\Services::superglobals()->setGetArray() instead.') |
| 161 | + ->identifier('codeigniter.getReassignArray') |
| 162 | + ->build(), |
| 163 | + ]; |
| 164 | + } |
| 165 | +} |
0 commit comments