Skip to content

Commit fa7d4bc

Browse files
committed
1 parent 450b3a4 commit fa7d4bc

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed

tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,11 @@ public function testTernaryElseReportPhpDoc(): void
537537
]);
538538
}
539539

540+
public function testBug4689(): void
541+
{
542+
$this->checkAlwaysTrueInstanceOf = true;
543+
$this->treatPhpDocTypesAsCertain = false;
544+
$this->analyse([__DIR__ . '/data/bug-4689.php'], []);
545+
}
546+
540547
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Bug4689;
4+
5+
abstract class KeywordList
6+
{
7+
}
8+
9+
abstract class AbstractPlatform
10+
{
11+
/**
12+
* @return KeywordList
13+
*/
14+
final public function getReservedKeywordsList()
15+
{
16+
$class = $this->getReservedKeywordsClass();
17+
$keywords = new $class();
18+
if (! $keywords instanceof KeywordList) {
19+
throw new \Exception();
20+
}
21+
22+
return $keywords;
23+
}
24+
25+
/**
26+
* @throws \Exception If not supported on this platform.
27+
*
28+
* @psalm-return class-string<KeywordList>
29+
*/
30+
protected function getReservedKeywordsClass(): string
31+
{
32+
throw new \Exception();
33+
}
34+
}

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@ class StrictComparisonOfDifferentTypesRuleTest extends RuleTestCase
1515

1616
private bool $checkAlwaysTrueStrictComparison;
1717

18+
private bool $treatPhpDocTypesAsCertain = true;
19+
1820
protected function getRule(): Rule
1921
{
20-
return new StrictComparisonOfDifferentTypesRule($this->checkAlwaysTrueStrictComparison, true);
22+
return new StrictComparisonOfDifferentTypesRule($this->checkAlwaysTrueStrictComparison, $this->treatPhpDocTypesAsCertain);
23+
}
24+
25+
protected function shouldTreatPhpDocTypesAsCertain(): bool
26+
{
27+
return $this->treatPhpDocTypesAsCertain;
2128
}
2229

2330
public function testStrictComparison(): void
@@ -744,4 +751,22 @@ public function testBug3019(): void
744751
$this->analyse([__DIR__ . '/../../Analyser/data/bug-3019.php'], []);
745752
}
746753

754+
public function testBug7578(): void
755+
{
756+
$this->checkAlwaysTrueStrictComparison = true;
757+
$this->treatPhpDocTypesAsCertain = false;
758+
$this->analyse([__DIR__ . '/data/bug-7578.php'], []);
759+
}
760+
761+
public function testBug6260(): void
762+
{
763+
if (PHP_VERSION_ID < 80000) {
764+
$this->markTestSkipped('Test requires PHP 8.0.');
765+
}
766+
767+
$this->checkAlwaysTrueStrictComparison = true;
768+
$this->treatPhpDocTypesAsCertain = false;
769+
$this->analyse([__DIR__ . '/data/bug-6260.php'], []);
770+
}
771+
747772
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug6260;
4+
5+
class Foo{
6+
public function __construct(
7+
/** @var non-empty-array<mixed> */
8+
private array $array
9+
){
10+
if(count($array) === 0){
11+
throw new \InvalidArgumentException();
12+
}
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug7578;
4+
5+
class HelloWorld
6+
{
7+
/**
8+
* @param non-empty-array<mixed> $array
9+
*/
10+
public function foo(array $array): void
11+
{
12+
if ([] === $array) {
13+
throw new \InvalidArgumentException();
14+
}
15+
}
16+
17+
/**
18+
* @param non-empty-array<mixed> $array
19+
*/
20+
public function foo2(array $array): void
21+
{
22+
if (0 === count($array)) {
23+
throw new \InvalidArgumentException();
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)