Skip to content
Closed
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
17 changes: 17 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6108,6 +6108,23 @@ private function processAssignVar(
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $falseySpecifiedTypes, $falseyType);
}

$nonNullType = TypeCombinator::removeNull($type);
if (
!$nonNullType->equals($type)
&& !$nonNullType->equals($truthyType)
) {
$notNullConditionExpr = new Expr\BinaryOp\NotIdentical($assignedExpr, new ConstFetch(new Name('null')));
$notNullSpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $notNullConditionExpr, TypeSpecifierContext::createTrue());
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notNullSpecifiedTypes, $nonNullType);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $notNullSpecifiedTypes, $nonNullType);

$nullType = new NullType();
$nullConditionExpr = new Expr\BinaryOp\Identical($assignedExpr, new ConstFetch(new Name('null')));
$nullSpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($scope, $nullConditionExpr, TypeSpecifierContext::createTrue());
$conditionalExpressions = $this->processSureTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $nullSpecifiedTypes, $nullType);
$conditionalExpressions = $this->processSureNotTypesForConditionalExpressionsAfterAssign($scope, $var->name, $conditionalExpressions, $nullSpecifiedTypes, $nullType);
}

$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
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
55 changes: 55 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-6120.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,58 @@ public function sayHello(): void
}
}
}

final class Clazz
{

public int $foo = 0;

public function bar(?Clazz $clazz): void
{
$result = $clazz?->foo;
assertType('int|null', $result);
if ($result !== null) {
assertType('Bug6120\Clazz', $clazz);
assertType('int', $result);
$clazz->bar(null);
}
}

public function baz(?Clazz $clazz): void
{
$result = $clazz?->foo;
if ($result === null) {
assertType('Bug6120\Clazz|null', $clazz);
assertType('null', $result);
} else {
assertType('Bug6120\Clazz', $clazz);
assertType('int', $result);
}
}

public function withNullableProperty(?Clazz $clazz): void
{
$result = $clazz?->nullableProperty;
if ($result !== null) {
assertType('Bug6120\Clazz', $clazz);
assertType('string', $result);
}
}

public ?string $nullableProperty = null;

public function withMethodCall(?Clazz $clazz): void
{
$result = $clazz?->getFoo();
if ($result !== null) {
assertType('Bug6120\Clazz', $clazz);
assertType('int', $result);
}
}

public function getFoo(): int
{
return $this->foo;
}

}
Loading