Skip to content

Commit d0662b9

Browse files
committed
Fixed nullable params checking.
1 parent 65341ec commit d0662b9

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

src/CheckerCommand.php

+24-7
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ protected function processFile($file)
294294
foreach ($processor->getMethods() as $name => $method) {
295295
if (count($method['params'])) {
296296
foreach ($method['params'] as $param => $type) {
297-
if (!isset($method['docblock']['params'][$param])) {
297+
if (empty($method['docblock']['params'][$param])) {
298298
$warnings = true;
299299
$this->warnings[] = [
300300
'type' => 'param-missing',
@@ -304,8 +304,26 @@ protected function processFile($file)
304304
'line' => $method['line'],
305305
'param' => $param,
306306
];
307-
} elseif (!empty($type) && $method['docblock']['params'][$param] != $type) {
308-
if ($type == 'array' && substr($method['docblock']['params'][$param], -2) == '[]') {
307+
} elseif (is_array($type)) {
308+
$docblockTypes = explode('|', $method['docblock']['params'][$param]);
309+
$normalizedType = $type;
310+
$normalizedType[0] = $docblockTypes[0];
311+
312+
if ($normalizedType !== $docblockTypes) {
313+
$warnings = true;
314+
$this->warnings[] = [
315+
'type' => 'param-mismatch',
316+
'file' => $file,
317+
'class' => $method['class'],
318+
'method' => $method['name'],
319+
'line' => $method['line'],
320+
'param' => $param,
321+
'param-type' => implode('|', $type),
322+
'doc-type' => $method['docblock']['params'][$param],
323+
];
324+
}
325+
} elseif (!empty($type) && $method['docblock']['params'][$param] !== $type) {
326+
if ($type === 'array' && substr($method['docblock']['params'][$param], -2) === '[]') {
309327
// Do nothing because this is fine.
310328
} else {
311329
$warnings = true;
@@ -337,8 +355,7 @@ protected function processFile($file)
337355
];
338356
} elseif (is_array($method['return'])) {
339357
$docblockTypes = explode('|', $method['docblock']['return']);
340-
sort($docblockTypes);
341-
if ($method['return'] != $docblockTypes) {
358+
if ($method['return'] !== $docblockTypes) {
342359
$warnings = true;
343360
$this->warnings[] = [
344361
'type' => 'return-mismatch',
@@ -350,8 +367,8 @@ protected function processFile($file)
350367
'doc-type' => $method['docblock']['return'],
351368
];
352369
}
353-
} elseif ($method['docblock']['return'] != $method['return']) {
354-
if ($method['return'] == 'array' && substr($method['docblock']['return'], -2) == '[]') {
370+
} elseif ($method['docblock']['return'] !== $method['return']) {
371+
if ($method['return'] === 'array' && substr($method['docblock']['return'], -2) === '[]') {
355372
// Do nothing because this is fine.
356373
} else {
357374
$warnings = true;

src/FileProcessor.php

+34-21
Original file line numberDiff line numberDiff line change
@@ -92,55 +92,68 @@ protected function processStatements(array $statements, $prefix = '')
9292

9393
$fullMethodName = $fullClassName . '::' . (string)$method->name;
9494

95-
$type = $method->returnType;
95+
$returnType = $method->returnType;
9696

9797
if (!$method->returnType instanceof NullableType) {
98-
if (!is_null($type)) {
99-
$type = (string)$type;
98+
if (!is_null($returnType)) {
99+
$returnType = (string)$returnType;
100100
}
101101
} else {
102-
$type = (string)$type->type;
102+
$returnType = (string)$returnType->type;
103103
}
104104

105-
if (isset($uses[$type])) {
106-
$type = $uses[$type];
105+
if (isset($uses[$returnType])) {
106+
$returnType = $uses[$returnType];
107107
}
108108

109-
$type = substr($type, 0, 1) == '\\' ? substr($type, 1) : $type;
109+
$returnType = substr($returnType, 0, 1) === '\\' ? substr($returnType, 1) : $returnType;
110110

111111
if ($method->returnType instanceof NullableType) {
112-
$type = ['null', $type];
113-
sort($type);
112+
$returnType = [$returnType, 'null'];
114113
}
115114

116115
$thisMethod = [
117116
'file' => $this->file,
118117
'class' => $fullClassName,
119118
'name' => (string)$method->name,
120119
'line' => $method->getAttribute('startLine'),
121-
'return' => $type,
120+
'return' => $returnType,
122121
'params' => [],
123122
'docblock' => $this->getDocblock($method, $uses),
124123
];
125124

126125
foreach ($method->params as $param) {
127-
$type = $param->type;
126+
$paramType = $param->type;
128127

129-
if (!$type instanceof NullableType) {
130-
if (!is_null($type)) {
131-
$type = (string)$type;
128+
if (!$param->type instanceof NullableType) {
129+
if (!is_null($param->type)) {
130+
$paramType = (string)$paramType;
132131
}
133132
} else {
134-
$type = (string)$type->type;
133+
$paramType = (string)$paramType->type;
135134
}
136135

137-
if (isset($uses[$type])) {
138-
$type = $uses[$type];
136+
if (isset($uses[$paramType])) {
137+
$paramType = $uses[$paramType];
139138
}
140139

141-
$type = substr($type, 0, 1) == '\\' ? substr($type, 1) : $type;
140+
$paramType = substr($paramType, 0, 1) === '\\' ? substr($paramType, 1) : $paramType;
141+
142+
if (
143+
$param->type instanceof NullableType
144+
) {
145+
$paramType = [$paramType, 'null'];
146+
} elseif (!empty($param->default->name->parts[0]) && 'null' === $param->default->name->parts[0]) {
147+
if (!is_null($param->type)) {
148+
$paramType = [$paramType, 'null'];
149+
} else {
150+
$paramType = ['<any>', 'null'];
151+
}
152+
}
153+
154+
var_dump([$param->default->name->parts[0], $paramType]);
142155

143-
$thisMethod['params']['$'.$param->name] = $type;
156+
$thisMethod['params']['$'.$param->name] = $paramType;
144157
}
145158

146159
$this->methods[$fullMethodName] = $thisMethod;
@@ -195,7 +208,7 @@ protected function processDocblock($text, array $uses = [])
195208
if (isset($uses[$tmpType])) {
196209
$tmpType = $uses[$tmpType];
197210
}
198-
$types[] = substr($tmpType, 0, 1) == '\\' ? substr($tmpType, 1) : $tmpType;
211+
$types[] = substr($tmpType, 0, 1) === '\\' ? substr($tmpType, 1) : $tmpType;
199212
}
200213
$rtn['params'][$param['var']] = implode('|', $types);
201214
}
@@ -215,7 +228,7 @@ protected function processDocblock($text, array $uses = [])
215228
if (isset($uses[$tmpType])) {
216229
$tmpType = $uses[$tmpType];
217230
}
218-
$types[] = substr($tmpType, 0, 1) == '\\' ? substr($tmpType, 1) : $tmpType;
231+
$types[] = substr($tmpType, 0, 1) === '\\' ? substr($tmpType, 1) : $tmpType;
219232
}
220233
$rtn['return'] = implode('|', $types);
221234
}

0 commit comments

Comments
 (0)