Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 18, 2021
1 parent a0fc384 commit cb04a6e
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ public function testBug4513(): void
$this->assertCount(0, $errors);
}

public function testBug1871(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-1871.php');
$this->assertCount(0, $errors);
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/data/bug-1871.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug1871;

interface I {}

class A implements I {}

function(): void {
$objects = [
new A()
];

foreach($objects as $object) {
var_dump(is_subclass_of($object, '\C'));
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,9 @@ public function testBug4432(): void
$this->analyse([__DIR__ . '/data/bug-4432.php'], []);
}

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

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-1664.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug1664;

class A
{
public function a()
{
$responses = [
'foo',
42,
'bar',
];

return $responses[array_rand($responses)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,11 @@ public function testBug3994(): void
$this->analyse([__DIR__ . '/data/bug-3994.php'], []);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -438,4 +438,16 @@ public function testBug2675(): void
$this->analyse([__DIR__ . '/data/bug-2675.php'], []);
}

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

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

}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-1613.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Bug1613;

class TestClass
{
public function test(string $index)
{
$array = [
"123" => "test"
];
return array_key_exists($index, $array);
}
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-1707.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Bug1707;

class Test
{
public function foo(): void
{
$values = ['a' => 1, 'b' => 2];
$keys = ['a', 'b', 'c', 'd'];

foreach ($keys as $key) {
if(array_key_exists($key, $values)){
unset($values[$key]);
}

if(0 === \count($values)) {
break;
}
}

}
}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-2220.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Bug2220;

class HelloWorld
{
/**
* @var string
*/
private $privateModule;

public function sayHello(): void
{
$resource = $this->getResource();

if ($resource === "{$this->privateModule}:abcdef") {
$this->abc();
} elseif ($resource === "{$this->privateModule}:xyz") {
$this->abc();
}
}

private function abc(): void {}

private function getResource(): string { return 'string'; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,10 @@ public function testBug2268(): void
$this->analyse([__DIR__ . '/data/bug-2268.php'], []);
}

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

}
24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-2434.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug2434;

function foo(int $param): void
{
//do something
}

function fooWithoutVoid(int $param)
{

}

function (): void {
register_shutdown_function('Bug2434\\foo', 1);
register_shutdown_function('Bug2434\\fooWithoutVoid', 1);

$parameter = new \stdClass();

$shutdown = static function (\stdClass $parameter): void {};

register_shutdown_function($shutdown, $parameter);
};
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1794,4 +1794,20 @@ public function testBug2298(): void
$this->analyse([__DIR__ . '/data/bug-2298.php'], []);
}

public function testBug1661(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-1661.php'], []);
}

public function testBug1656(): void
{
$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-1656.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-1656.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Bug1656;

class HelloWorld
{
public function test(): void
{
return;
}

public function testVoidResult(): void
{
true or $this->test();
}
}
42 changes: 42 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-1661.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Bug1661;

interface I1 {
public function someMethod();
}

interface I2 {
public function someOtherMethod();
}

class Foo {
/**
* @return I1&static
*/
public function bar() {
if ($this instanceof I1) {
return $this;
}

throw new \Exception('bad');
}

/**
* @return I2&static
*/
public function bat() {
if ($this instanceof I2) {
return $this;
}

throw new \Exception('bad');
}
}

function (): void {
$a = (new Foo)->bar();
$b = $a->bat();
$b->someMethod();
$b->someOtherMethod();
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,14 @@ public function testRule(): void
]);
}

public function testBug2682(): void
{
$this->analyse([__DIR__ . '/data/bug-2682.php'], [
[
'Anonymous function should have native return typehint "void".',
9,
],
]);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Missing/data/bug-2682.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug2682;

class HelloWorld
{
public function sayHello(): void
{
function(array $array) {
function(): string {
return 'abc';
};
};
}
}

0 comments on commit cb04a6e

Please sign in to comment.