Merge pull request #26 from weirdan/constants-as-default-values

Use is_optional instead of default_type
This commit is contained in:
Bruce Weirdan 2019-03-06 21:40:12 +02:00 committed by GitHub
commit a85bbacdae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 8 deletions

View File

@ -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);
}
}
}

View File

@ -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<string,array{0:int,1?:string}> */
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<string,array{0:int}> */
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
"""