Skip to content

Commit 6865741

Browse files
herndlmondrejmirtes
authored andcommitted
Do not call TypeSpecifier::specifyTypesInCondition with a non-null context in case the original context is null
1 parent 1e95392 commit 6865741

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function specifyTypesInCondition(
175175
$exprNode = $expressions[0];
176176
/** @var ConstantScalarType $constantType */
177177
$constantType = $expressions[1];
178-
if ($constantType->getValue() === false) {
178+
if (!$context->null() && $constantType->getValue() === false) {
179179
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
180180
return $types->unionWith($this->specifyTypesInCondition(
181181
$scope,
@@ -185,7 +185,7 @@ public function specifyTypesInCondition(
185185
));
186186
}
187187

188-
if ($constantType->getValue() === true) {
188+
if (!$context->null() && $constantType->getValue() === true) {
189189
$types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr);
190190
return $types->unionWith($this->specifyTypesInCondition(
191191
$scope,
@@ -339,7 +339,7 @@ public function specifyTypesInCondition(
339339
$exprNode = $expressions[0];
340340
/** @var ConstantScalarType $constantType */
341341
$constantType = $expressions[1];
342-
if ($constantType->getValue() === false || $constantType->getValue() === null) {
342+
if (!$context->null() && ($constantType->getValue() === false || $constantType->getValue() === null)) {
343343
return $this->specifyTypesInCondition(
344344
$scope,
345345
$exprNode,
@@ -348,7 +348,7 @@ public function specifyTypesInCondition(
348348
);
349349
}
350350

351-
if ($constantType->getValue() === true) {
351+
if (!$context->null() && $constantType->getValue() === true) {
352352
return $this->specifyTypesInCondition(
353353
$scope,
354354
$exprNode,

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,4 +528,10 @@ public function testBug6939(): void
528528
]);
529529
}
530530

531+
public function testBug7166(): void
532+
{
533+
$this->checkAlwaysTrueStrictComparison = true;
534+
$this->analyse([__DIR__ . '/data/bug-7166.php'], []);
535+
}
536+
531537
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7166;
4+
5+
use function PHPStan\dumpType;
6+
7+
class HelloWorld
8+
{
9+
public static function print(
10+
string $value
11+
): void {
12+
$isSingleLine = strpos($value, "\n") === false;
13+
dumpType($value);
14+
$hasLeadingSpace = $value !== '' && ($value[0] === ' ' || $value[0] === '\t');
15+
}
16+
}

0 commit comments

Comments
 (0)