Skip to content

Commit 868b36c

Browse files
committed
Fixes for UnionType.
1 parent 884b335 commit 868b36c

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

src/CheckerCommand.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,15 @@ protected function processFile(string $file): array
314314
'param' => $param,
315315
];
316316
} elseif (\is_array($type)) {
317-
$docblockTypes = \explode('|', $method['docblock']['params'][$param]);
317+
$docblockTypes = (array)\explode('|', $method['docblock']['params'][$param]);
318318
$normalizedType = $type;
319-
$normalizedType[0] = $docblockTypes[0];
319+
320+
if (!$type && $docblockTypes) {
321+
continue;
322+
}
323+
324+
\sort($docblockTypes, SORT_STRING);
325+
\sort($normalizedType, SORT_STRING);
320326

321327
if ($normalizedType !== $docblockTypes) {
322328
$warnings = true;
@@ -370,7 +376,11 @@ protected function processFile(string $file): array
370376
'line' => $method['line'],
371377
];
372378
} elseif (\is_array($method['return'])) {
373-
$docblockTypes = \explode('|', $method['docblock']['return']);
379+
$docblockTypes = (array)\explode('|', $method['docblock']['return']);
380+
381+
\sort($docblockTypes, SORT_STRING);
382+
\sort($method['return'], SORT_STRING);
383+
374384
if ($method['return'] !== $docblockTypes) {
375385
$warnings = true;
376386
$this->warnings[] = [

src/FileProcessor.php

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpParser\Node\Stmt\ClassMethod;
1212
use PhpParser\Node\Stmt\Namespace_;
1313
use PhpParser\Node\Stmt\Use_;
14+
use PhpParser\Node\UnionType;
1415
use PhpParser\ParserFactory;
1516

1617
/**
@@ -74,15 +75,14 @@ public function getMethods(): array
7475
protected function processStatements(array $statements, string $prefix = '')
7576
{
7677
$uses = [];
77-
7878
foreach ($statements as $statement) {
7979
if ($statement instanceof Namespace_) {
8080
return $this->processStatements($statement->stmts, (string)$statement->name);
8181
}
8282

8383
if ($statement instanceof Use_) {
8484
foreach ($statement->uses as $use) {
85-
$uses[(string) $use->alias] = (string)$use->name;
85+
$uses[(string)$use->alias] = (string)$use->name;
8686
}
8787
}
8888

@@ -102,26 +102,37 @@ protected function processStatements(array $statements, string $prefix = '')
102102
continue;
103103
}
104104

105-
$fullMethodName = $fullClassName . '::' . (string)$method->name;
105+
$fullMethodName = $fullClassName . '::' . $method->name;
106106

107107
$returnType = $method->returnType;
108-
109-
if (!$method->returnType instanceof NullableType) {
108+
if ($method->returnType instanceof NullableType) {
109+
$returnType = [$returnType->type, 'null'];
110+
} elseif ($method->returnType instanceof UnionType) {
111+
$returnType = $returnType->types;
112+
} else {
110113
if (!\is_null($returnType)) {
111114
$returnType = (string)$returnType;
112115
}
113-
} else {
114-
$returnType = (string)$returnType->type;
115116
}
116117

117-
if (isset($uses[$returnType])) {
118-
$returnType = $uses[$returnType];
119-
}
118+
$returnType = (array)$returnType;
119+
foreach ($returnType as &$returnTypeItem) {
120+
if (isset($uses[(string)$returnTypeItem])) {
121+
$returnTypeItem = $uses[(string)$returnTypeItem];
122+
}
120123

121-
$returnType = \substr((string)$returnType, 0, 1) === '\\' ? \substr((string)$returnType, 1) : $returnType;
124+
$returnTypeItem = \substr((string)$returnTypeItem, 0, 1) === '\\'
125+
? \substr((string)$returnTypeItem, 1)
126+
: $returnTypeItem;
122127

123-
if ($method->returnType instanceof NullableType) {
124-
$returnType = [$returnType, 'null'];
128+
$returnTypeItem = (null !== $returnTypeItem)
129+
? (string)$returnTypeItem
130+
: null;
131+
}
132+
unset($returnTypeItem);
133+
134+
if (1 === \count($returnType)) {
135+
$returnType = (string)$returnType[0];
125136
}
126137

127138
$thisMethod = [
@@ -136,32 +147,40 @@ protected function processStatements(array $statements, string $prefix = '')
136147

137148
foreach ($method->params as $param) {
138149
$paramType = $param->type;
139-
140-
if (!$param->type instanceof NullableType) {
150+
if ($param->type instanceof NullableType) {
151+
$paramType = [$paramType->type, 'null'];
152+
} elseif (
153+
!empty($param->default->name->parts[0]) &&
154+
'null' === $param->default->name->parts[0]
155+
) {
141156
if (!\is_null($param->type)) {
142-
$paramType = (string)$paramType;
157+
$paramType = [$paramType, 'null'];
158+
} else {
159+
$paramType = ['<any>', 'null'];
143160
}
161+
} elseif ($param->type instanceof UnionType) {
162+
$paramType = $paramType->types;
144163
} else {
145-
$paramType = (string)$paramType->type;
164+
if (!\is_null($paramType)) {
165+
$paramType = (string)$paramType;
166+
}
146167
}
147168

148-
if (isset($uses[$paramType])) {
149-
$paramType = $uses[$paramType];
150-
}
169+
$paramType = (array)$paramType;
170+
foreach ($paramType as &$paramTypeItem) {
171+
if (isset($uses[(string)$paramTypeItem])) {
172+
$paramTypeItem = $uses[(string)$paramTypeItem];
173+
}
151174

152-
$paramType = \substr((string)$paramType, 0, 1) === '\\' ? \substr((string)$paramType, 1) : $paramType;
175+
$paramTypeItem = \substr((string)$paramTypeItem, 0, 1) === '\\'
176+
? \substr((string)$paramTypeItem, 1)
177+
: $paramTypeItem;
153178

154-
if (
155-
$param->type instanceof NullableType
156-
) {
157-
$paramType = [$paramType, 'null'];
158-
} elseif (!empty($param->default->name->parts[0]) && 'null' === $param->default->name->parts[0]) {
159-
if (!\is_null($param->type)) {
160-
$paramType = [$paramType, 'null'];
161-
} else {
162-
$paramType = ['<any>', 'null'];
163-
}
179+
$paramTypeItem = (null !== $paramTypeItem)
180+
? (string)$paramTypeItem
181+
: null;
164182
}
183+
unset($paramTypeItem);
165184

166185
$thisMethod['params']['$'.$param->var->name] = $paramType;
167186
}

0 commit comments

Comments
 (0)