From 12cfb4d1ebc6b9164a40440367c51e6238d4400b Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Wed, 6 Mar 2019 21:00:09 +0200 Subject: [PATCH] Use is_optional instead of default_type Refs psalm/phpunit-psalm-plugin#25 --- hooks/TestCaseHandler.php | 15 +++++----- tests/acceptance/TestCase.feature | 46 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/hooks/TestCaseHandler.php b/hooks/TestCaseHandler.php index a7fb860..62d56ea 100644 --- a/hooks/TestCaseHandler.php +++ b/hooks/TestCaseHandler.php @@ -232,13 +232,12 @@ class TestCaseHandler implements $checkParam = /** - * @param null|Type\Union $param_default_type * @return void */ function ( Type\Union $potential_argument_type, Type\Union $param_type, - $param_default_type, + bool $is_optional, int $param_offset ) use ( $codebase, @@ -248,7 +247,7 @@ class TestCaseHandler implements $provider_docblock_location ) { $param_type = clone $param_type; - if ($param_default_type) { + if ($is_optional) { $param_type->possibly_undefined = true; } if (self::isTypeContainedByType($codebase, $potential_argument_type, $param_type)) { @@ -261,7 +260,7 @@ class TestCaseHandler implements . ' by ' . $provider_method_id . '():(' . $provider_return_type_string . ')', $provider_docblock_location )); - } elseif ($potential_argument_type->possibly_undefined && !$param_default_type) { + } elseif ($potential_argument_type->possibly_undefined && !$is_optional) { IssueBuffer::accepts(new Issue\InvalidArgument( 'Argument ' . ($param_offset + 1) . ' of ' . $method_name . ' has no default value, but possibly undefined ' @@ -288,7 +287,7 @@ class TestCaseHandler implements $potential_argument_type = $dataset_type->type_params[1]; foreach ($method_storage->params as $param_offset => $param) { assert(null !== $param->type); - $checkParam($potential_argument_type, $param->type, $param->default_type, $param_offset); + $checkParam($potential_argument_type, $param->type, $param->is_optional, $param_offset); } } else { // iterate over all params checking if corresponding value type is acceptable @@ -304,7 +303,7 @@ class TestCaseHandler implements } // reached default params, so it's fine, but let's continue // because MisplacedRequiredParam could be suppressed - if ($param->default_type) { + if ($param->is_optional) { continue; } @@ -333,14 +332,14 @@ class TestCaseHandler implements $checkParam( $potential_argument_type, $variadic_param_type, - $variadic_param_type, + true, $param_offset ); } break; } - $checkParam($potential_argument_type, $param->type, $param->default_type, $param_offset); + $checkParam($potential_argument_type, $param->type, $param->is_optional, $param_offset); } } } diff --git a/tests/acceptance/TestCase.feature b/tests/acceptance/TestCase.feature index ebf2ee9..f7a072d 100644 --- a/tests/acceptance/TestCase.feature +++ b/tests/acceptance/TestCase.feature @@ -582,6 +582,52 @@ Feature: TestCase | UnusedClass | Class NS\UtilityClass is never used | And I see no other errors + Scenario: Provider returning optional offsets is fine when test method has defaults for those params (specified as constants) + Given I have the following code + """ + class MyTestCase extends TestCase + { + /** @var string */ + const S = "s"; + /** @return iterable */ + public function provide() { + yield "data set name" => rand(0,1) ? [1] : [1, "ss"]; + } + /** + * @return void + * @dataProvider provide + */ + public function testSomething(int $int, string $_str = self::S) { + $this->assertEquals(1, $int); + } + } + """ + When I run Psalm + Then I see no errors + + Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants) + Given I have the following code + """ + class MyTestCase extends TestCase + { + /** @var string */ + const S = "s"; + /** @return iterable */ + public function provide() { + yield "data set name" => rand(0,1) ? [1] : [1, "ss"]; + } + /** + * @return void + * @dataProvider provide + */ + public function testSomething(int $int, string $_str = self::S) { + $this->assertEquals(1, $int); + } + } + """ + When I run Psalm + Then I see no errors + Scenario: Provider returning possibly undefined offset is fine when test method has default for that param Given I have the following code """