Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 13, 2023
1 parent 6652811 commit af65434
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php
Expand Up @@ -537,4 +537,11 @@ public function testTernaryElseReportPhpDoc(): void
]);
}

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

}
34 changes: 34 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-4689.php
@@ -0,0 +1,34 @@
<?php

namespace Bug4689;

abstract class KeywordList
{
}

abstract class AbstractPlatform
{
/**
* @return KeywordList
*/
final public function getReservedKeywordsList()
{
$class = $this->getReservedKeywordsClass();
$keywords = new $class();
if (! $keywords instanceof KeywordList) {
throw new \Exception();
}

return $keywords;
}

/**
* @throws \Exception If not supported on this platform.
*
* @psalm-return class-string<KeywordList>
*/
protected function getReservedKeywordsClass(): string
{
throw new \Exception();
}
}
Expand Up @@ -15,9 +15,16 @@ class StrictComparisonOfDifferentTypesRuleTest extends RuleTestCase

private bool $checkAlwaysTrueStrictComparison;

private bool $treatPhpDocTypesAsCertain = true;

protected function getRule(): Rule
{
return new StrictComparisonOfDifferentTypesRule($this->checkAlwaysTrueStrictComparison, true);
return new StrictComparisonOfDifferentTypesRule($this->checkAlwaysTrueStrictComparison, $this->treatPhpDocTypesAsCertain);
}

protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return $this->treatPhpDocTypesAsCertain;
}

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

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

public function testBug6260(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->checkAlwaysTrueStrictComparison = true;
$this->treatPhpDocTypesAsCertain = false;
$this->analyse([__DIR__ . '/data/bug-6260.php'], []);
}

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-6260.php
@@ -0,0 +1,14 @@
<?php // lint >= 8.0

namespace Bug6260;

class Foo{
public function __construct(
/** @var non-empty-array<mixed> */
private array $array
){
if(count($array) === 0){
throw new \InvalidArgumentException();
}
}
}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-7578.php
@@ -0,0 +1,26 @@
<?php

namespace Bug7578;

class HelloWorld
{
/**
* @param non-empty-array<mixed> $array
*/
public function foo(array $array): void
{
if ([] === $array) {
throw new \InvalidArgumentException();
}
}

/**
* @param non-empty-array<mixed> $array
*/
public function foo2(array $array): void
{
if (0 === count($array)) {
throw new \InvalidArgumentException();
}
}
}

0 comments on commit af65434

Please sign in to comment.