Skip to content

Commit d6b5291

Browse files
authored
Merge pull request #98 from orklah/EA
small fixes
2 parents 5b92e1c + 017743f commit d6b5291

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

src/TypeResolver.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use phpDocumentor\Reflection\Types\Object_;
2727
use phpDocumentor\Reflection\Types\String_;
2828
use RuntimeException;
29-
use function array_keys;
29+
use function array_key_exists;
3030
use function array_pop;
3131
use function class_exists;
3232
use function class_implements;
@@ -169,9 +169,11 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
169169
);
170170
}
171171

172-
if ($parserContext !== self::PARSER_IN_COMPOUND
173-
&& $parserContext !== self::PARSER_IN_ARRAY_EXPRESSION
174-
&& $parserContext !== self::PARSER_IN_COLLECTION_EXPRESSION
172+
if (!in_array($parserContext, [
173+
self::PARSER_IN_COMPOUND,
174+
self::PARSER_IN_ARRAY_EXPRESSION,
175+
self::PARSER_IN_COLLECTION_EXPRESSION,
176+
], true)
175177
) {
176178
throw new RuntimeException(
177179
'Unexpected type separator'
@@ -180,9 +182,11 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
180182

181183
$tokens->next();
182184
} elseif ($token === '?') {
183-
if ($parserContext !== self::PARSER_IN_COMPOUND
184-
&& $parserContext !== self::PARSER_IN_ARRAY_EXPRESSION
185-
&& $parserContext !== self::PARSER_IN_COLLECTION_EXPRESSION
185+
if (!in_array($parserContext, [
186+
self::PARSER_IN_COMPOUND,
187+
self::PARSER_IN_ARRAY_EXPRESSION,
188+
self::PARSER_IN_COLLECTION_EXPRESSION,
189+
], true)
186190
) {
187191
throw new RuntimeException(
188192
'Unexpected nullable character'
@@ -284,7 +288,7 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser
284288
*
285289
* @return Type|Array_|Object_
286290
*/
287-
private function resolveSingleType(string $type, Context $context)
291+
private function resolveSingleType(string $type, Context $context) : object
288292
{
289293
switch (true) {
290294
case $this->isKeyword($type):
@@ -347,7 +351,7 @@ private function isTypedArray(string $type) : bool
347351
*/
348352
private function isKeyword(string $type) : bool
349353
{
350-
return in_array(strtolower($type), array_keys($this->keywords), true);
354+
return array_key_exists(strtolower($type), $this->keywords);
351355
}
352356

353357
/**
@@ -504,7 +508,6 @@ private function resolveCollection(ArrayIterator $tokens, Type $classType, Conte
504508
return new Iterable_($valueType, $keyType);
505509
}
506510

507-
/** @psalm-suppress RedundantCondition */
508511
if ($classType instanceof Object_) {
509512
return $this->makeCollectionFromObject($classType, $valueType, $keyType);
510513
}

src/Types/AbstractList.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ public function __construct(?Type $valueType = null, ?Type $keyType = null)
4848
*/
4949
public function getKeyType() : Type
5050
{
51-
if ($this->keyType === null) {
52-
return $this->defaultKeyType;
53-
}
54-
55-
return $this->keyType;
51+
return $this->keyType ?? $this->defaultKeyType;
5652
}
5753

5854
/**

src/Types/Compound.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ArrayIterator;
1717
use IteratorAggregate;
1818
use phpDocumentor\Reflection\Type;
19+
use function array_key_exists;
1920
use function implode;
2021

2122
/**
@@ -63,7 +64,7 @@ public function get(int $index) : ?Type
6364
*/
6465
public function has(int $index) : bool
6566
{
66-
return isset($this->types[$index]);
67+
return array_key_exists($index, $this->types);
6768
}
6869

6970
/**

src/Types/Context.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ final class Context
3636
/** @var string The current namespace. */
3737
private $namespace;
3838

39-
/** @var string[] List of namespace aliases => Fully Qualified Namespace. */
39+
/**
40+
* @var string[] List of namespace aliases => Fully Qualified Namespace.
41+
* @psalm-var array<string, string>
42+
*/
4043
private $namespaceAliases;
4144

4245
/**
@@ -45,6 +48,8 @@ final class Context
4548
*
4649
* @param string $namespace The namespace where this DocBlock resides in.
4750
* @param string[] $namespaceAliases List of namespace aliases => Fully Qualified Namespace.
51+
*
52+
* @psalm-param array<string, string> $namespaceAliases
4853
*/
4954
public function __construct(string $namespace, array $namespaceAliases = [])
5055
{
@@ -80,6 +85,8 @@ public function getNamespace() : string
8085
* the alias for the imported Namespace.
8186
*
8287
* @return string[]
88+
*
89+
* @psalm-return array<string, string>
8390
*/
8491
public function getNamespaceAliases() : array
8592
{

src/Types/ContextFactory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function file_exists;
2727
use function file_get_contents;
2828
use function get_class;
29+
use function in_array;
2930
use function is_string;
3031
use function token_get_all;
3132
use function trim;
@@ -179,8 +180,7 @@ public function createForNamespace(string $namespace, string $fileContents) : Co
179180
while ($tokens->valid() && ($braceLevel > 0 || !$firstBraceFound)) {
180181
$currentToken = $tokens->current();
181182
if ($currentToken === '{'
182-
|| $currentToken[0] === T_CURLY_OPEN
183-
|| $currentToken[0] === T_DOLLAR_OPEN_CURLY_BRACES) {
183+
|| in_array($currentToken[0], [T_CURLY_OPEN, T_DOLLAR_OPEN_CURLY_BRACES], true)) {
184184
if (!$firstBraceFound) {
185185
$firstBraceFound = true;
186186
}
@@ -221,8 +221,7 @@ private function parseNamespace(ArrayIterator $tokens) : string
221221
$this->skipToNextStringOrNamespaceSeparator($tokens);
222222

223223
$name = '';
224-
while ($tokens->valid() && ($tokens->current()[0] === T_STRING || $tokens->current()[0] === T_NS_SEPARATOR)
225-
) {
224+
while ($tokens->valid() && in_array($tokens->current()[0], [T_STRING, T_NS_SEPARATOR], true)) {
226225
$name .= $tokens->current()[1];
227226
$tokens->next();
228227
}
@@ -236,6 +235,8 @@ private function parseNamespace(ArrayIterator $tokens) : string
236235
* @param ArrayIterator<int, string|array{0:int,1:string,2:int}> $tokens
237236
*
238237
* @return string[]
238+
*
239+
* @psalm-return array<string, string>
239240
*/
240241
private function parseUseStatement(ArrayIterator $tokens) : array
241242
{
@@ -267,7 +268,7 @@ private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens) : v
267268
{
268269
while ($tokens->valid()) {
269270
$currentToken = $tokens->current();
270-
if ($currentToken[0] === T_STRING || $currentToken[0] === T_NS_SEPARATOR) {
271+
if (in_array($currentToken[0], [T_STRING, T_NS_SEPARATOR], true)) {
271272
break;
272273
}
273274

@@ -284,6 +285,8 @@ private function skipToNextStringOrNamespaceSeparator(ArrayIterator $tokens) : v
284285
* @return string[]
285286
*
286287
* @psalm-suppress TypeDoesNotContainType
288+
*
289+
* @psalm-return array<string, string>
287290
*/
288291
private function extractUseStatements(ArrayIterator $tokens) : array
289292
{

0 commit comments

Comments
 (0)