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
7 changes: 5 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ private static function intersectWithSubtractedType(
Type $b,
): Type
{
if ($a->getSubtractedType() === null) {
if ($a->getSubtractedType() === null || $b instanceof NeverType) {
return $a;
}

Expand Down Expand Up @@ -621,7 +621,10 @@ private static function intersectWithSubtractedType(
} elseif ($isBAlreadySubtracted->yes()) {
$subtractedType = self::remove($a->getSubtractedType(), $b);

if ($subtractedType instanceof NeverType) {
if (
$subtractedType instanceof NeverType
|| !$subtractedType->isSuperTypeOf($b)->no()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case checks if we "didn't remove" enough

) {
$subtractedType = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,18 @@ public function testBug10394(): void
$this->analyse([__DIR__ . '/data/bug-10394.php'], []);
}

public function testBug12930(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-12930.php'], []);
}

public function testBug13628(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-13628.php'], []);
}

public function testBug9666(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-12930.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Bug12930;

/** @return mixed */
function getMixed() {return \stdClass::class;}

$val = getMixed();
if (is_string($val) && !is_a($val, \stdClass::class, true)) {
throw new \Exception();
}

echo is_string($val) ? 'string' : 'something else';
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-13628.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace Bug13628;

/**
* @param mixed $param
* @return string
*/
function test($param) {

$a = is_array($param) ? array_filter($param) : $param;
if ($a && is_array($a)) {
return 'array';
}
else {
return 'not-array';
}

}
8 changes: 8 additions & 0 deletions tests/PHPStan/Type/TypeCombinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,14 @@ public static function dataUnion(): iterable
MixedType::class,
'mixed=implicit',
],
[
[
new ArrayType(new MixedType(), new StringType()),
new MixedType(subtractedType: new ArrayType(new MixedType(), new MixedType())),
],
MixedType::class,
'mixed=implicit',
],
];

if (PHP_VERSION_ID < 80100) {
Expand Down
Loading