|
173 | 173 | use PHPStan\Type\Constant\ConstantArrayTypeBuilder; |
174 | 174 | use PHPStan\Type\Constant\ConstantIntegerType; |
175 | 175 | use PHPStan\Type\Constant\ConstantStringType; |
| 176 | +use PHPStan\Type\ConstantTypeHelper; |
176 | 177 | use PHPStan\Type\FileTypeMapper; |
177 | 178 | use PHPStan\Type\GeneralizePrecision; |
178 | 179 | use PHPStan\Type\Generic\TemplateTypeHelper; |
@@ -6108,6 +6109,35 @@ private function processAssignVar( |
6108 | 6109 | $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType); |
6109 | 6110 | } |
6110 | 6111 |
|
| 6112 | + foreach ([null, false, 0, 0.0, '', '0', []] as $falseyScalar) { |
| 6113 | + $falseyType = ConstantTypeHelper::getTypeFromValue($falseyScalar); |
| 6114 | + $withoutFalseyType = TypeCombinator::remove($type, $falseyType); |
| 6115 | + if ( |
| 6116 | + $withoutFalseyType->equals($type) |
| 6117 | + || $withoutFalseyType->equals($truthyType) |
| 6118 | + ) { |
| 6119 | + continue; |
| 6120 | + } |
| 6121 | + |
| 6122 | + $astNode = match ($falseyScalar) { |
| 6123 | + null => new ConstFetch(new Name('null')), |
| 6124 | + false => new ConstFetch(new Name('false')), |
| 6125 | + 0 => new Node\Scalar\Int_($falseyScalar), |
| 6126 | + 0.0 => new Node\Scalar\Float_($falseyScalar), |
| 6127 | + '', '0' => new Node\Scalar\String_($falseyScalar), |
| 6128 | + [] => new Node\Expr\Array_($falseyScalar), |
| 6129 | + }; |
| 6130 | + $notConditionExpr = new Expr\BinaryOp\NotIdentical($assignedExpr, $astNode); |
| 6131 | + $notSpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $notConditionExpr, TypeSpecifierContext::createTrue()); |
| 6132 | + $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notSpecifiedTypes, $withoutFalseyType); |
| 6133 | + $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notSpecifiedTypes, $withoutFalseyType); |
| 6134 | + |
| 6135 | + $conditionExpr = new Expr\BinaryOp\Identical($assignedExpr, $astNode); |
| 6136 | + $specifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $conditionExpr, TypeSpecifierContext::createTrue()); |
| 6137 | + $conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $specifiedTypes, $falseyType); |
| 6138 | + $conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $specifiedTypes, $falseyType); |
| 6139 | + } |
| 6140 | + |
6111 | 6141 | $this->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval, $storage); |
6112 | 6142 | $scope = $scope->assignVariable($var->name, $type, $scope->getNativeType($assignedExpr), TrinaryLogic::createYes()); |
6113 | 6143 | foreach ($conditionalExpressions as $exprString => $holders) { |
|
0 commit comments