|
57 | 57 | use PHPStan\Type\Accessory\HasOffsetValueType; |
58 | 58 | use PHPStan\Type\Accessory\NonEmptyArrayType; |
59 | 59 | use PHPStan\Type\Constant\ConstantArrayType; |
| 60 | +use PHPStan\Type\Constant\ConstantBooleanType; |
60 | 61 | use PHPStan\Type\Constant\ConstantIntegerType; |
61 | 62 | use PHPStan\Type\Constant\ConstantStringType; |
62 | 63 | use PHPStan\Type\ConstantTypeHelper; |
|
65 | 66 | use PHPStan\Type\IntegerType; |
66 | 67 | use PHPStan\Type\MixedType; |
67 | 68 | use PHPStan\Type\NeverType; |
| 69 | +use PHPStan\Type\NullType; |
68 | 70 | use PHPStan\Type\ObjectType; |
69 | 71 | use PHPStan\Type\StaticTypeFactory; |
70 | 72 | use PHPStan\Type\Type; |
@@ -107,6 +109,184 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type |
107 | 109 | return $scope->getType($expr->expr); |
108 | 110 | } |
109 | 111 |
|
| 112 | + public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $expr, TypeSpecifierContext $context): SpecifiedTypes |
| 113 | + { |
| 114 | + if (!$expr instanceof Assign) { |
| 115 | + return $typeSpecifier->specifyDefaultTypes($scope, $expr, $context); |
| 116 | + } |
| 117 | + |
| 118 | + if (!$scope instanceof MutatingScope) { |
| 119 | + throw new ShouldNotHappenException(); |
| 120 | + } |
| 121 | + |
| 122 | + if ($context->null()) { |
| 123 | + $specifiedTypes = $typeSpecifier->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->expr, $context)->setRootExpr($expr); |
| 124 | + $specifiedTypes = $specifiedTypes->removeExpr($this->exprPrinter->printExpr($expr->var)); |
| 125 | + } else { |
| 126 | + $specifiedTypes = $typeSpecifier->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->var, $context)->setRootExpr($expr); |
| 127 | + } |
| 128 | + |
| 129 | + // infer $arr[$key] after $key = array_key_first/last($arr) |
| 130 | + if ( |
| 131 | + $expr->expr instanceof FuncCall |
| 132 | + && $expr->expr->name instanceof Name |
| 133 | + && !$expr->expr->isFirstClassCallable() |
| 134 | + && in_array($expr->expr->name->toLowerString(), ['array_key_first', 'array_key_last'], true) |
| 135 | + && count($expr->expr->getArgs()) >= 1 |
| 136 | + ) { |
| 137 | + $arrayArg = $expr->expr->getArgs()[0]->value; |
| 138 | + $arrayType = $scope->getType($arrayArg); |
| 139 | + |
| 140 | + if ($arrayType->isArray()->yes()) { |
| 141 | + if ($context->true()) { |
| 142 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 143 | + $typeSpecifier->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope), |
| 144 | + ); |
| 145 | + $isNonEmpty = true; |
| 146 | + } else { |
| 147 | + $isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes(); |
| 148 | + } |
| 149 | + |
| 150 | + if ($isNonEmpty) { |
| 151 | + $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
| 152 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 153 | + $typeSpecifier->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope), |
| 154 | + ); |
| 155 | + } elseif ($expr->var instanceof Expr\Variable && is_string($expr->var->name)) { |
| 156 | + $keyType = $scope->getType($expr->expr); |
| 157 | + $nonNullKeyType = TypeCombinator::removeNull($keyType); |
| 158 | + if (!$nonNullKeyType instanceof NeverType) { |
| 159 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 160 | + $typeSpecifier->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $nonNullKeyType, $arrayType->getIterableValueType()), |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // infer $arr[$key] after $key = array_search($needle, $arr) or $key = array_find_key($arr, $callback) |
| 168 | + if ( |
| 169 | + $expr->expr instanceof FuncCall |
| 170 | + && $expr->expr->name instanceof Name |
| 171 | + && !$expr->expr->isFirstClassCallable() |
| 172 | + && count($expr->expr->getArgs()) >= 2 |
| 173 | + ) { |
| 174 | + $funcName = $expr->expr->name->toLowerString(); |
| 175 | + $arrayArg = null; |
| 176 | + $sentinelType = null; |
| 177 | + $isStrictArraySearch = false; |
| 178 | + |
| 179 | + if ($funcName === 'array_search') { |
| 180 | + $arrayArg = $expr->expr->getArgs()[1]->value; |
| 181 | + $sentinelType = new ConstantBooleanType(false); |
| 182 | + $isStrictArraySearch = count($expr->expr->getArgs()) >= 3 && $scope->getType($expr->expr->getArgs()[2]->value)->isTrue()->yes(); |
| 183 | + } elseif ($funcName === 'array_find_key') { |
| 184 | + $arrayArg = $expr->expr->getArgs()[0]->value; |
| 185 | + $sentinelType = new NullType(); |
| 186 | + } |
| 187 | + |
| 188 | + if ($arrayArg !== null) { |
| 189 | + $arrayType = $scope->getType($arrayArg); |
| 190 | + |
| 191 | + if ($arrayType->isArray()->yes()) { |
| 192 | + if ($context->true()) { |
| 193 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 194 | + $typeSpecifier->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope), |
| 195 | + ); |
| 196 | + |
| 197 | + $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
| 198 | + |
| 199 | + if ($isStrictArraySearch) { |
| 200 | + $needleType = $scope->getType($expr->expr->getArgs()[0]->value); |
| 201 | + $dimFetchType = TypeCombinator::intersect($needleType, $arrayType->getIterableValueType()); |
| 202 | + } else { |
| 203 | + $dimFetchType = $arrayType->getIterableValueType(); |
| 204 | + } |
| 205 | + |
| 206 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 207 | + $typeSpecifier->create($dimFetch, $dimFetchType, TypeSpecifierContext::createTrue(), $scope), |
| 208 | + ); |
| 209 | + } elseif ($expr->var instanceof Expr\Variable && is_string($expr->var->name)) { |
| 210 | + $keyType = $scope->getType($expr->expr); |
| 211 | + $narrowedKeyType = TypeCombinator::remove($keyType, $sentinelType); |
| 212 | + if (!$narrowedKeyType instanceof NeverType) { |
| 213 | + if ($isStrictArraySearch) { |
| 214 | + $needleType = $scope->getType($expr->expr->getArgs()[0]->value); |
| 215 | + $dimFetchType = TypeCombinator::intersect($needleType, $arrayType->getIterableValueType()); |
| 216 | + } else { |
| 217 | + $dimFetchType = $arrayType->getIterableValueType(); |
| 218 | + } |
| 219 | + $specifiedTypes = $specifiedTypes->unionWith( |
| 220 | + $typeSpecifier->createArrayDimFetchConditionalExpressionHolder($expr->var, $arrayArg, $narrowedKeyType, $dimFetchType), |
| 221 | + ); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if ($context->null()) { |
| 229 | + // infer $arr[$key] after $key = array_rand($arr) |
| 230 | + if ( |
| 231 | + $expr->expr instanceof FuncCall |
| 232 | + && $expr->expr->name instanceof Name |
| 233 | + && !$expr->expr->isFirstClassCallable() |
| 234 | + && in_array($expr->expr->name->toLowerString(), ['array_rand'], true) |
| 235 | + && count($expr->expr->getArgs()) >= 1 |
| 236 | + ) { |
| 237 | + $numArg = null; |
| 238 | + $args = $expr->expr->getArgs(); |
| 239 | + $arrayArg = $args[0]->value; |
| 240 | + if (count($args) > 1) { |
| 241 | + $numArg = $args[1]->value; |
| 242 | + } |
| 243 | + $one = new ConstantIntegerType(1); |
| 244 | + $arrayType = $scope->getType($arrayArg); |
| 245 | + |
| 246 | + if ( |
| 247 | + $arrayType->isArray()->yes() |
| 248 | + && $arrayType->isIterableAtLeastOnce()->yes() |
| 249 | + && ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg))->yes()) |
| 250 | + ) { |
| 251 | + $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
| 252 | + |
| 253 | + return $specifiedTypes->unionWith( |
| 254 | + $typeSpecifier->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope), |
| 255 | + ); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + // infer $list[$count] after $count = count($list) - 1 |
| 260 | + if ( |
| 261 | + $expr->expr instanceof Expr\BinaryOp\Minus |
| 262 | + && $expr->expr->left instanceof FuncCall |
| 263 | + && $expr->expr->left->name instanceof Name |
| 264 | + && !$expr->expr->left->isFirstClassCallable() |
| 265 | + && $expr->expr->right instanceof Node\Scalar\Int_ |
| 266 | + && $expr->expr->right->value === 1 |
| 267 | + && in_array($expr->expr->left->name->toLowerString(), ['count', 'sizeof'], true) |
| 268 | + && count($expr->expr->left->getArgs()) >= 1 |
| 269 | + ) { |
| 270 | + $arrayArg = $expr->expr->left->getArgs()[0]->value; |
| 271 | + $arrayType = $scope->getType($arrayArg); |
| 272 | + if ( |
| 273 | + $arrayType->isList()->yes() |
| 274 | + && $arrayType->isIterableAtLeastOnce()->yes() |
| 275 | + ) { |
| 276 | + $dimFetch = new ArrayDimFetch($arrayArg, $expr->var); |
| 277 | + |
| 278 | + return $specifiedTypes->unionWith( |
| 279 | + $typeSpecifier->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope), |
| 280 | + ); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + return $specifiedTypes; |
| 285 | + } |
| 286 | + |
| 287 | + return $specifiedTypes; |
| 288 | + } |
| 289 | + |
110 | 290 | public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult |
111 | 291 | { |
112 | 292 | $result = $this->processAssignVar( |
|
0 commit comments