Skip to content
Open
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
18 changes: 18 additions & 0 deletions src/Analyser/ExpressionTypeHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr;
use PHPStan\TrinaryLogic;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand Down Expand Up @@ -50,6 +51,23 @@ public function equals(self $other): bool
return $this->type === $other->type || $this->type->equals($other->type);
}

public function isSuperTypeOf(self $other): IsSuperTypeOfResult
{
if ($this === $other) {
return IsSuperTypeOfResult::createYes();
}

if (!$this->certainty->equals($other->certainty)) {
return IsSuperTypeOfResult::createNo();
}

if ($this->type === $other->type) {
return IsSuperTypeOfResult::createYes();
}

return $this->type->isSuperTypeOf($other->type);
}

public function and(self $other): self
{
if ($this->type === $other->type || $this->type->equals($other->type)) {
Expand Down
35 changes: 31 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3216,15 +3216,42 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self
if (array_key_exists($conditionalExprString, $conditions)) {
continue;
}

$exactMatches = [];
$superTypeMatches = [];
foreach ($conditionalExpressions as $conditionalExpression) {
$allExact = true;
$allSuperType = true;
foreach ($conditionalExpression->getConditionExpressionTypeHolders() as $holderExprString => $conditionalTypeHolder) {
if (!array_key_exists($holderExprString, $specifiedExpressions) || !$specifiedExpressions[$holderExprString]->equals($conditionalTypeHolder)) {
continue 2;
if (!array_key_exists($holderExprString, $specifiedExpressions)) {
$allExact = false;
$allSuperType = false;
break;
}
if ($conditionalTypeHolder->equals($specifiedExpressions[$holderExprString])) {
continue;
}

$allExact = false;
if (!$conditionalTypeHolder->isSuperTypeOf($specifiedExpressions[$holderExprString])->yes()) {
$allSuperType = false;
break;
}
}
if ($allExact) {
$exactMatches[] = $conditionalExpression;
} elseif ($allSuperType) {
$superTypeMatches[] = $conditionalExpression;
}
}

$conditions[$conditionalExprString][] = $conditionalExpression;
$specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder();
if (count($exactMatches) > 0) {
foreach ($exactMatches as $exactMatch) {
$conditions[$conditionalExprString][] = $exactMatch;
$specifiedExpressions[$conditionalExprString] = $exactMatch->getTypeHolder();
}
} elseif (count($superTypeMatches) === 1 && count($conditionalExpressions) >= 2) {
$conditions[$conditionalExprString][] = $superTypeMatches[0];
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-10422.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug10422;

use stdClass;
use function PHPStan\Testing\assertType;

class Foo
{

public function something(): bool
{
return true;
}

public function test(): void
{
}

}

function createOrNotObject(): ?Foo
{
return new Foo();
}

function testSimple(): void
{
$test = createOrNotObject();

$error = '';

if (!$test) {
$error = 'missing test';
} else if ($test->something()) {
$error = 'another';
}
if ($error) {
die('Done');
}
assertType(Foo::class, $test);
}
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4090-regression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types = 1);

namespace Bug4090Regression;

use function PHPStan\Testing\assertType;

class AggregationParser
{
/**
* @param array<string, mixed> $aggregation
*/
private function parseAggregation(array $aggregation): ?string
{
$type = $aggregation['type'] ?? null;
if (!\is_string($type) || empty($type) || is_numeric($type)) {
return null;
}

if (empty($aggregation['field']) && $type !== 'filter') {
return null;
}

$field = '';
if ($type !== 'filter') {
$field = self::buildFieldName();
}

assertType('non-falsy-string', $type);

return $field;
}

private static function buildFieldName(): string
{
return 'field';
}
}
50 changes: 50 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-4090.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types = 1);

namespace Bug4090;

use function PHPStan\Testing\assertType;

/** @param string[] $a */
function foo(array $a): void
{
if (count($a) > 1) {
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
} elseif (count($a) === 1) {
assertType('non-empty-array<string>', $a);
echo trim(current($a));
}
}


/** @param string[] $a */
function bar(array $a): void
{
$count = count($a);
if ($count > 1) {
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
} elseif ($count === 1) {
assertType('non-empty-array<string>', $a);
echo trim(current($a));
}
}


/** @param string[] $a */
function qux(array $a): void
{
switch (count($a)) {
case 0:
assertType('array{}', $a);
break;
case 1:
assertType('non-empty-array<string>', $a);
echo trim(current($a));
break;
default:
assertType('non-empty-array<string>', $a);
echo implode(',', $a);
break;
}
}
14 changes: 7 additions & 7 deletions tests/PHPStan/Analyser/nsrt/bug-5051.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,35 @@ public function testWithBooleans($data): void
assertType('bool', $update);
} else {
assertType('1|2', $data);
assertType('bool', $update);
assertType('false', $update);
}

if ($data === 1) {
assertType('bool', $update);
assertType('bool', $foo);
assertType('false', $update);
assertType('false', $foo);
} else {
assertType('bool', $update);
assertType('bool', $foo);
}

if ($data === 2) {
assertType('bool', $update);
assertType('bool', $foo);
assertType('false', $update);
assertType('false', $foo);
} else {
assertType('bool', $update);
assertType('bool', $foo);
}

if ($data === 3) {
assertType('bool', $update);
assertType('false', $update);
assertType('true', $foo);
} else {
assertType('bool', $update);
assertType('bool', $foo);
}

if ($data === 1 || $data === 2) {
assertType('bool', $update);
assertType('false', $update);
assertType('false', $foo);
} else {
assertType('bool', $update);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,11 @@ public function testBug10527(): void
$this->analyse([__DIR__ . '/data/bug-10527.php'], []);
}

public function testBug10055(): void
{
$this->analyse([__DIR__ . '/data/bug-10055.php'], []);
}

public function testBug10626(): void
{
$this->analyse([__DIR__ . '/data/bug-10626.php'], [
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-10055.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Bug10055;

function expectInt(int $param): void
{
}

function expectBool(bool $param): void
{
}

/**
* @param 'value1'|'value2'|'value3' $param1
* @param ($param1 is 'value3' ? bool : int) $param2
*/
function test(string $param1, int|bool $param2): void
{
match ($param1) {
'value1' => expectInt($param2),
'value2' => expectInt($param2),
'value3' => expectBool($param2),
};
}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,25 @@ public function testBug14227(): void
$this->analyse([__DIR__ . '/data/bug-14227.php'], []);
}

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

#[RequiresPhp('>= 8.0')]
public function testBug12597NonFinite(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;
$this->analyse([__DIR__ . '/data/bug-12597-non-finite.php'], []);
}

public function testBug14117(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand All @@ -1499,4 +1518,14 @@ public function testBug14117(): void
]);
}

public function testBug11218(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

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

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

namespace Bug11218;

function doFoo() {
$level = 'test';

for ($i = 0 ; $i <= 3 ; $i++) {
if ($i === 0) {
$test[$level] = 'this is a';
} else {
$test[$level] .= ' test';
}
}
}

function doBar() {
$level = 'test';

$test = [];

for ($i = 0 ; $i <= 3 ; $i++) {
if ($i === 0) {
$test[$level] = 'this is a';
} else {
$test[$level] .= ' test';
}
}
}
Loading
Loading