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
428 changes: 159 additions & 269 deletions src/Analyser/MutatingScope.php

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3982,19 +3982,30 @@ static function (): void {
&& $stmt->valueVar instanceof Variable && is_string($stmt->valueVar->name)
&& $stmt->keyVar instanceof Variable && is_string($stmt->keyVar->name)
) {
$conditionalHolders = [];
$valueConditionalHolders = [];
$arrayDimFetchConditionalHolders = [];
foreach ($iterateeType->getKeyTypes() as $i => $keyType) {
$valueType = $iterateeType->getValueTypes()[$i];
$holder = new ConditionalExpressionHolder([
'$' . $stmt->keyVar->name => $keyType,
], new ExpressionTypeHolder($stmt->valueVar, $valueType, TrinaryLogic::createYes()));
$conditionalHolders[$holder->getKey()] = $holder;
$valueConditionalHolders[$holder->getKey()] = $holder;
$arrayDimFetchHolder = new ConditionalExpressionHolder([
'$' . $stmt->keyVar->name => $keyType,
], new ExpressionTypeHolder(new ArrayDimFetch($stmt->expr, $stmt->keyVar), $valueType, TrinaryLogic::createYes()));
$arrayDimFetchConditionalHolders[$arrayDimFetchHolder->getKey()] = $arrayDimFetchHolder;
}

$scope = $scope->addConditionalExpressions(
'$' . $stmt->valueVar->name,
$conditionalHolders,
$valueConditionalHolders,
);
if ($stmt->expr instanceof Variable && is_string($stmt->expr->name)) {
$scope = $scope->addConditionalExpressions(
sprintf('$%s[$%s]', $stmt->expr->name, $stmt->keyVar->name),
$arrayDimFetchConditionalHolders,
);
}
}

return $this->processVarAnnotation($scope, $vars, $stmt);
Expand Down
11 changes: 3 additions & 8 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,9 @@ private static function unionWithSubtractedType(
}

if ($type instanceof SubtractableType) {
if ($type->getSubtractedType() === null) {
return $type;
}

$subtractedType = self::union(
$type->getSubtractedType(),
$subtractedType,
);
$subtractedType = $type->getSubtractedType() === null
? $subtractedType
: self::union($type->getSubtractedType(), $subtractedType);
if ($subtractedType instanceof NeverType) {
$subtractedType = null;
}
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/static-has-method.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mixed-to-number.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-8113.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/phpunit-integration.php');
}

/**
Expand Down
6 changes: 2 additions & 4 deletions tests/PHPStan/Analyser/data/bug-3677.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public function sayGoodbye(): void
{
[$first, $second] = $this->getValue();
if ($first || $second) {
// this assert passes but the ternary breaks the next assert
// assertType(Field::class, $first ?: $second);
assertType(Field::class, $first ?: $second);
assertType(Field::class, $first ?? $second);
}
}
Expand All @@ -71,8 +70,7 @@ public function sayGoodbye2(): void
{
[$first, $second] = $this->getValue();
if ($first || $second) {
// this assert passes but the ternary breaks the next assert
// assertType(Field::class, $first ? $first : $second);
assertType(Field::class, $first ? $first : $second);
assertType(Field::class, $first ?? $second);
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/bug-5843.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function doFoo(object $object): void
assertType(\DateTime::class, $object);
break;
case \Throwable::class:
assertType(\Throwable::class, $object);
assertType('Throwable~DateTime', $object);
break;
}
}
Expand All @@ -29,7 +29,7 @@ function doFoo(object $object): void
{
match ($object::class) {
\DateTime::class => assertType(\DateTime::class, $object),
\Throwable::class => assertType(\Throwable::class, $object),
\Throwable::class => assertType('Throwable~DateTime', $object),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function (bool $a, bool $b) {

assertVariableCertainty(TrinaryLogic::createMaybe(), $foo);
if ($b) {
assertVariableCertainty(TrinaryLogic::createMaybe(), $foo); // could be Yes
assertVariableCertainty(TrinaryLogic::createYes(), $foo);
}

if ($a) {
Expand Down
62 changes: 62 additions & 0 deletions tests/PHPStan/Analyser/data/phpunit-integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace PHPUnitIntegration;

use Exception;
use function libxml_get_errors;
use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;
use function sprintf;
use DOMDocument;

final class Loader
{
/**
* @throws Exception
*/
public function load(string $actual, bool $isHtml = false, string $filename = '', bool $strict = false): DOMDocument
{
$document = new DOMDocument;
$message = '';

if ($isHtml) {
$loaded = $document->loadHTML($actual);
} else {
$loaded = $document->loadXML($actual);
}

foreach (libxml_get_errors() as $error) {
$message .= "\n" . $error->message;
}

if ($loaded === false || ($strict && $message !== '')) {
assertType('string', $message);
assertNativeType('string', $message);
if ($filename !== '') {
assertType('string', $message);
assertNativeType('string', $message);
throw new Exception(
sprintf(
'Could not load "%s".%s',
$filename,
$message !== '' ? "\n" . $message : ''
)
);
}

assertType('string', $message);
assertNativeType('string', $message);

if ($message === '') {
$message = 'Could not load XML for unknown reason';
}

assertType('non-empty-string', $message);
assertNativeType('non-empty-string', $message);

throw new Exception($message);
}

return $document;
}
}
1 change: 1 addition & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-6000.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function (): void {
assertType('array<int<0, max>|string, array<string>|string>', $data[$key]);
if ($key === 'classmap') {
assertType('list<string>', $data[$key]);
assertType('list<string>', $value);
echo implode(', ', $value); // not working :(
echo implode(', ', $data[$key]); // this works though?!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,10 @@ public function testBug8158(): void
$this->analyse([__DIR__ . '/data/bug-8158.php'], []);
}

public function testPhpUnitIntegration(): void
{
$this->checkAlwaysTrueStrictComparison = true;
$this->analyse([__DIR__ . '/../../Analyser/data/phpunit-integration.php'], []);
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2666,4 +2666,13 @@ public function testArrayCastListTypes(): void
$this->analyse([__DIR__ . '/data/array-cast-list-types.php'], []);
}

public function testBug5623(): void
{
$this->checkThisOnly = false;
$this->checkNullables = false;
$this->checkUnionTypes = true;
$this->checkExplicitMixed = false;
$this->analyse([__DIR__ . '/data/bug-5623.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-5623.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug5623;

use DateTimeInterface;

class HelloWorld
{
public function sayHello(?DateTimeInterface $from = null, ?DateTimeInterface $to = null,): void
{
if ($from || $to) {
$operator = $from ? 'notBefore' : 'notAfter';
$date = ($from ?? $to)->format(DateTimeInterface::ATOM);
}
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,4 +886,22 @@ public function testBug8142(): void
$this->analyse([__DIR__ . '/data/bug-8142.php'], []);
}

public function testBug5401(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-5401.php'], []);
}

public function testBug8212(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = true;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-8212.php'], []);
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Variables/IssetRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,12 @@ public function testBug6008(): void
$this->analyse([__DIR__ . '/data/bug-6008.php'], []);
}

public function testBug7292(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = true;

$this->analyse([__DIR__ . '/data/bug-7292.php'], []);
}

}
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-5401.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Bug5401;

function test1(bool $foo, bool $bar, bool $baz): void
{
if ($foo) {
$qux = $baz;
}

if ($foo && $bar) {
// ...
}

if ($foo && $qux) {
// ...
}
}

function test2(bool $foo, bool $bar, bool $baz): void
{
if ($foo) {
$qux = $baz;
}

if ($foo && $bar) {
// ...
}

if ($foo) {
echo $qux;
}
}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-7292.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1); // lint >= 7.4

namespace Bug7292;

class MyMetadata {
/** @var class-string */
public string $fqcn;
}

class MyClass {
/** @var array<class-string, array<string, mixed>> */
private $myMap = [];

public function doSomething(MyMetadata $class): void
{
unset($this->myMap[$class->fqcn]['foo']);

if (isset($this->myMap[$class->fqcn]) && ! $this->myMap[$class->fqcn]) {
unset($this->myMap[$class->fqcn]);
}
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-8212.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace Bug8212;

function test(): void
{

$foo = rand();
if ($foo) {
$var = rand();
}

// 200 lines later:

if ($foo) {
echo $var; // Variable $var might not be defined.
}
}