Skip to content

Commit

Permalink
Allow union types as datatype of parameters in @method annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNunninger committed Oct 26, 2019
1 parent 6e0d18c commit 1ba74be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Annotation/MethodAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ public function parameters() : array
private function parseParametersString(string $parametersString) : ?array
{
$parameterPattern = '/^' .
'((' . self::PATTERN_DATA_TYPE . ') )?' . // optional datatype plus space
'(' . // optional datatype (union type allowed) plus space
'(' .
'(' .
self::PATTERN_DATA_TYPE . preg_quote('|') .
')*' .
self::PATTERN_DATA_TYPE .
') ' .
')?' .
'(' . self::PATTERN_VARIABLE_NAME . ')' . // parameter name
'( = ' . // optional default value
'(' .
Expand All @@ -112,19 +119,19 @@ private function parseParametersString(string $parametersString) : ?array
return null;
}

$parameterName = $matches[6];
$parameterName = $matches[9];
$dataType = $matches[2] ?: null;
$hasDefaultValue = '' !== $matches[8];
$hasDefaultValue = '' !== $matches[11];
$parameters[$parameterName] = [
'dataType' => $dataType,
'hasDefaultValue' => $hasDefaultValue,
];

if ($hasDefaultValue) {
$parameters[$parameterName]['default'] = $matches[8];
$parameters[$parameterName]['default'] = $matches[11];
}

$parametersString = array_key_exists(12, $matches) ? $matches[12] : '';
$parametersString = array_key_exists(15, $matches) ? $matches[15] : '';
}

return $parameters;
Expand Down
29 changes: 29 additions & 0 deletions tests/PhpUnit/Annotation/MethodAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,35 @@ final class MethodAnnotationTest extends AbstractAnnotationTestCase
[],
'int|null',
false,
],
[
'foo(string|null $parameter)',
'foo',
[
'parameter' => [
'dataType' => 'string|null',
'hasDefaultValue' => false,
],
],
null,
false,
],
[
'int|null foo(string|null $parameter = "defaultValue", $secondParameter)',
'foo',
[
'parameter' => [
'dataType' => 'string|null',
'hasDefaultValue' => true,
'default' => '"defaultValue"',
],
'secondParameter' => [
'dataType' => null,
'hasDefaultValue' => false,
]
],
'int|null',
false,
]
];

Expand Down

0 comments on commit 1ba74be

Please sign in to comment.