Skip to content

Commit

Permalink
Support for 7.1's nullable types (#1)
Browse files Browse the repository at this point in the history
Support for 7.1's nullable types
  • Loading branch information
kkarkus authored and Mariusz Gomse committed Feb 27, 2018
1 parent 73febd5 commit 4f6daa8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
15 changes: 15 additions & 0 deletions PhpDocblockChecker/CheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,21 @@ protected function processFile($file)
'method' => $name,
'line' => $method['line'],
];
} elseif (is_array($method['return'])) {
$docblockTypes = explode('|', $method['docblock']['return']);
sort($docblockTypes);
if ($method['return'] != $docblockTypes) {
$warnings = true;
$this->warnings[] = [
'type' => 'return-mismatch',
'file' => $file,
'class' => $name,
'method' => $name,
'line' => $method['line'],
'return-type' => implode('|', $method['return']),
'doc-type' => $method['docblock']['return'],
];
}
} elseif ($method['docblock']['return'] != $method['return']) {
if ($method['return'] == 'array' && substr($method['docblock']['return'], -2) == '[]') {
// Do nothing because this is fine.
Expand Down
39 changes: 27 additions & 12 deletions PhpDocblockChecker/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpDocblockChecker\DocBlockParser;
use PhpParser\Comment\Doc;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -95,8 +96,12 @@ protected function processStatements(array $statements, $prefix = '')

$type = $method->returnType;

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

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

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

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

$thisMethod = [
'file' => $this->file,
Expand Down Expand Up @@ -179,13 +188,16 @@ protected function processDocblock($text, array $uses = [])
$type = (string)$type;
}

if (isset($uses[$type])) {
$type = $uses[$type];
}
$types = [];
foreach (explode('|', $type) as $tmpType) {
if (isset($uses[$tmpType])) {
$tmpType = $uses[$tmpType];
}

$type = substr($type, 0, 1) == '\\' ? substr($type, 1) : $type;
$types[] = substr($tmpType, 0, 1) == '\\' ? substr($tmpType, 1) : $tmpType;
}

$rtn['params'][$param['var']] = $type;
$rtn['params'][$param['var']] = implode('|', $types);
}
}

Expand All @@ -198,13 +210,16 @@ protected function processDocblock($text, array $uses = [])
$type = (string)$type;
}

if (isset($uses[$type])) {
$type = $uses[$type];
}
$types = [];
foreach (explode('|', $type) as $tmpType) {
if (isset($uses[$tmpType])) {
$tmpType = $uses[$tmpType];
}

$type = substr($type, 0, 1) == '\\' ? substr($type, 1) : $type;
$types[] = substr($tmpType, 0, 1) == '\\' ? substr($tmpType, 1) : $tmpType;
}

$rtn['return'] = $type;
$rtn['return'] = implode('|', $types);
}

return $rtn;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

"require": {
"php": ">=5.3.3",
"nikic/php-parser": "3.*",
"nikic/php-parser": "4.*",
"symfony/console": "3.*"
},

Expand Down

0 comments on commit 4f6daa8

Please sign in to comment.