Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,6 @@ parameters:
count: 1
path: src/Reflection/ClassReflection.php

-
rawMessage: 'Method PHPStan\Reflection\ClassReflection::getCacheKey() should return string but returns string|null.'
identifier: return.type
count: 1
path: src/Reflection/ClassReflection.php

-
rawMessage: Binary operation "&" between bool|float|int|string|null and bool|float|int|string|null results in an error.
identifier: binaryOp.invalid
Expand Down
36 changes: 36 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantTypeHelper;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Generic\TemplateTypeHelper;
Expand Down Expand Up @@ -6108,6 +6109,41 @@ private function processAssignVar(
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType);
}

foreach ([null, false, 0, 0.0, '', '0', []] as $falseyScalar) {
$falseyType = ConstantTypeHelper::getTypeFromValue($falseyScalar);
$withoutFalseyType = TypeCombinator::remove($type, $falseyType);
if (
$withoutFalseyType->equals($type)
|| $withoutFalseyType->equals($truthyType)
) {
continue;
}

if ($falseyScalar === null) {
$astNode = new ConstFetch(new Name('null'));
} elseif ($falseyScalar === false) {
$astNode = new ConstFetch(new Name('false'));
} elseif ($falseyScalar === 0) {
$astNode = new Node\Scalar\Int_($falseyScalar);
} elseif ($falseyScalar === 0.0) {
$astNode = new Node\Scalar\Float_($falseyScalar);
} elseif (in_array($falseyScalar, ['', '0'], true)) {
$astNode = new Node\Scalar\String_($falseyScalar);
} elseif ($falseyScalar === []) {
$astNode = new Node\Expr\Array_($falseyScalar);
}

$notIdenticalConditionExpr = new Expr\BinaryOp\NotIdentical($assignedExpr, $astNode);
$notIdenticalSpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $notIdenticalConditionExpr, TypeSpecifierContext::createTrue());
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notIdenticalSpecifiedTypes, $withoutFalseyType);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notIdenticalSpecifiedTypes, $withoutFalseyType);

$identicalConditionExpr = new Expr\BinaryOp\Identical($assignedExpr, $astNode);
$identicalSpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $identicalConditionExpr, TypeSpecifierContext::createTrue());
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $identicalSpecifiedTypes, $falseyType);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $identicalSpecifiedTypes, $falseyType);
}

$this->callNodeCallback($nodeCallback, new VariableAssignNode($var, $assignedExpr), $scopeBeforeAssignEval, $storage);
$scope = $scope->assignVariable($var->name, $type, $scope->getNativeType($assignedExpr), TrinaryLogic::createYes());
foreach ($conditionalExpressions as $exprString => $holders) {
Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10482.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug10482;

use function PHPStan\Testing\assertType;

class Test
{
public ?int $id;
}

$test = new Test();
if (rand(0, 1)) {
$test = null;
}

$testId = $test?->id;
if (null !== $testId) {
assertType('Bug10482\Test', $test);
}

if ($testId) {
assertType('Bug10482\Test', $test);
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-13546.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ function mixedLast($mixed): void
function firstInCondition(array $array)
{
if (($key = array_key_first($array)) !== null) {
assertType('list<string>', $array); // could be 'non-empty-list<string>'
assertType('non-empty-list<string>', $array);
return $array[$key];
}
assertType('list<string>', $array);
assertType('array{}', $array);
return null;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13709.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug13709;

use function PHPStan\Testing\assertType;

function doFOo(string $shortName): void
{
$pos = strpos($shortName, '\\');
if ($pos === false) {
return;
}

assertType('non-falsy-string', $shortName);
}


4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-14081.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function firstNotNull(array $array): mixed
{
if (($key = array_key_first($array)) !== null) {
assertType('int<0, max>', $key);
assertType('list<string>', $array); // could be non-empty-list<string>
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
Expand All @@ -60,7 +60,7 @@ function lastNotNull(array $array): mixed
{
if (($key = array_key_last($array)) !== null) {
assertType('int<0, max>', $key);
assertType('list<string>', $array); // could be non-empty-list<string>
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ public function testArrayDimFetchAfterArraySearch(): void

$this->analyse([__DIR__ . '/data/array-dim-after-array-search.php'], [
[
'Offset int|string might not exist on array.',
'Offset int|string might not exist on non-empty-array.',
20,
],
]);
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3891,4 +3891,13 @@ public function testBug9820(): void
]);
}

#[RequiresPhp('>= 8.1')]
public function testBug6120(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-6120.php'], []);
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-6120.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php // lint >= 8.0

declare(strict_types=1);

namespace Bug6120;

class Clazz
{

public int $foo = 0;

public function bar(?Clazz $clazz): void
{
$result = $clazz?->foo;
if ($result !== null) {
$clazz->bar(null);
}
}

}
Loading