Merge pull request #20 from weirdan/fix-19

Fix psalm/phpunit-psalm-plugin#19
This commit is contained in:
Bruce Weirdan 2019-02-27 19:20:53 +02:00 committed by GitHub
commit f3e0aae3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 0 deletions

View File

@ -49,10 +49,34 @@ class TestCaseHandler implements
$meta = (array) ($storage->custom_metadata[__NAMESPACE__] ?? []);
if ($codebase->classExtends($name, TestCase::class) && ($meta['hasInitializers'] ?? false)) {
$storage->suppressed_issues[] = 'MissingConstructor';
foreach (self::getDescendants($codebase, $name) as $dependent_name) {
$dependent_storage = $codebase->classlike_storage_provider->get($dependent_name);
$dependent_storage->suppressed_issues[] = 'MissingConstructor';
}
}
}
}
/** @return string[] */
private static function getDescendants(Codebase $codebase, string $name): array
{
if (!$codebase->classlike_storage_provider->has($name)) {
return [];
}
$storage = $codebase->classlike_storage_provider->get($name);
$ret = [];
foreach ($storage->dependent_classlikes as $dependent => $_) {
if ($codebase->classExtends($dependent, $name)) {
$ret[] = $dependent;
$ret = array_merge($ret, self::getDescendants($codebase, $dependent));
}
}
return $ret;
}
/**
* {@inheritDoc}
*/

View File

@ -680,3 +680,70 @@ Feature: TestCase
"""
When I run Psalm
Then I see no errors
Scenario: Descendant of a test that has setUp produces no MissingConstructor
Given I have the following code
"""
use Prophecy\Prophecy\ObjectProphecy;
interface I { public function work(): int; }
class BaseTestCase extends TestCase {
/** @var ObjectProphecy<I> */
protected $i;
/** @return void */
public function setUp() {
$this->i = $this->prophesize(I::class);
}
}
class Intermediate extends BaseTestCase {}
class MyTestCase extends Intermediate
{
/** @return void */
public function testSomething() {
$this->i->work()->willReturn(1);;
$i = $this->i->reveal();
$this->assertEquals(1, $i->work());
}
}
"""
When I run Psalm
Then I see no errors
Scenario: Descendant of a test that has @before produces no MissingConstructor
Given I have the following code
"""
use Prophecy\Prophecy\ObjectProphecy;
interface I { public function work(): int; }
class BaseTestCase extends TestCase {
/** @var ObjectProphecy<I> */
protected $i;
/**
* @before
* @return void
*/
public function myInit() {
$this->i = $this->prophesize(I::class);
}
}
class Intermediate extends BaseTestCase {}
class MyTestCase extends Intermediate
{
/** @return void */
public function testSomething() {
$this->i->work()->willReturn(1);;
$i = $this->i->reveal();
$this->assertEquals(1, $i->work());
}
}
"""
When I run Psalm
Then I see no errors