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
9 changes: 9 additions & 0 deletions src/Type/Constant/ConstantFloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public function toArrayKey(): Type
return new ConstantIntegerType((int) $this->value);
}

public function getFiniteTypes(): array
{
if (is_nan($this->value)) {
return [];
}

return [$this];
}

public function generalize(GeneralizePrecision $precision): Type
{
return new FloatType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1161,4 +1161,15 @@ public function testPossiblyImpureTip(): void
]);
}

public function testBug11054(): void
{
$this->analyse([__DIR__ . '/data/bug-11054.php'], [
[
'Strict comparison using === between mixed and array{INF} will always evaluate to false.',
47,
'Type array{INF} has already been eliminated from mixed.',
],
]);
}

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

namespace Bug11054;

class XXX {
public const DEF_VALUE = [NAN];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Add another test using INF instead of NAN

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Both background tasks confirmed complete. All done — the commit with INF test cases has been pushed to the PR branch.

public const NO_VALUE = [NAN];
}

class YYY
{
public const LOSSLESS = [NAN];

/**
* @param string|int|mixed $substituteChar
*/
public static function convertEncoding (
string $str,
string $sourceEncoding,
string $targetEncoding,
$substituteChar = XXX::DEF_VALUE,
$defValue = XXX::NO_VALUE
): string {
if ($substituteChar === XXX::DEF_VALUE) { // no error expected
return mb_convert_encoding($str, $targetEncoding, $sourceEncoding);
}

if ($substituteChar === self::LOSSLESS) { // no error expected
return $str;
}

return $str;
}
}

class InfTest {
public const DEF_VALUE = [INF];
public const NO_VALUE = [INF];

/** @param mixed $v */
public static function test($v): void
{
if ($v === self::DEF_VALUE) { // no error expected
return;
}

if ($v === self::NO_VALUE) {} // error expected - INF === INF is true, so narrowing is correct
}
}

class SimpleTest {
/** @param mixed $v */
public static function test($v): void
{
if ($v === [NAN]) {} // no error expected
if ($v === [INF]) {} // no error expected
}
}
Loading