Skip to content

Commit

Permalink
Fix nullable params (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkarkus authored and Mariusz Gomse committed Mar 23, 2018
1 parent a521e8d commit 9eb01c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
18 changes: 17 additions & 1 deletion PhpDocblockChecker/CheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,23 @@ protected function processFile($file)
'param' => $param,
];
} elseif (!empty($type) && $method['docblock']['params'][$param] != $type) {
if ($type == 'array' && substr($method['docblock']['params'][$param], -2) == '[]') {
if (is_array($type)) {
$docblockTypes = explode('|', $method['docblock']['params'][$param]);
sort($docblockTypes);
if ($type != $docblockTypes) {
$warnings = true;
$this->warnings[] = [
'type' => 'param-mismatch',
'file' => $file,
'class' => $name,
'method' => $name,
'line' => $method['line'],
'param' => $param,
'param-type' => implode('|', $type),
'doc-type' => $method['docblock']['params'][$param],
];
}
} elseif ($type == 'array' && substr($method['docblock']['params'][$param], -2) == '[]') {
// Do nothing because this is fine.
} else {
$warnings = true;
Expand Down
13 changes: 11 additions & 2 deletions PhpDocblockChecker/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ protected function processStatements(array $statements, $prefix = '')
foreach ($method->params as $param) {
$type = $param->type;

if (!is_null($type)) {
$type = (string)$type;
if (!$param->type instanceof NullableType) {
if (!is_null($type)) {
$type = (string)$type;
}
} else {
$type = (string) $type->type;
}

if (isset($uses[$type])) {
Expand All @@ -138,6 +142,11 @@ protected function processStatements(array $statements, $prefix = '')

$type = substr($type, 0, 1) == '\\' ? substr($type, 1) : $type;

if ($param->type instanceof NullableType) {
$type = ['null', $type];
sort($type);
}

$thisMethod['params']['$'.$param->name] = $type;
}

Expand Down

0 comments on commit 9eb01c6

Please sign in to comment.