Skip to content

Commit 8e792e2

Browse files
committed
Small TypeHintHelper refactoring
1 parent b9b40de commit 8e792e2

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

SlevomatCodingStandard/Helpers/TypeHintHelper.php

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,14 @@
55
class TypeHintHelper
66
{
77

8-
/** @var string[] */
9-
public static $simpleTypeHints = [
10-
'int',
11-
'integer',
12-
'float',
13-
'string',
14-
'bool',
15-
'boolean',
16-
'callable',
17-
'self',
18-
'array',
19-
'iterable',
20-
'void',
21-
];
22-
23-
/** @var string[] */
24-
public static $simpleIterableTypeHints = [
25-
'array',
26-
'iterable',
27-
];
28-
29-
/** @var string[] */
30-
public static $simpleUnofficialTypeHints = [
31-
'null',
32-
'mixed',
33-
'true',
34-
'false',
35-
'resource',
36-
'object',
37-
'static',
38-
'$this',
39-
];
40-
418
public static function isSimpleTypeHint(string $typeHint): bool
429
{
43-
return in_array($typeHint, self::$simpleTypeHints, true);
10+
return in_array($typeHint, self::getSimpleTypeHints(), true);
4411
}
4512

4613
public static function isSimpleIterableTypeHint(string $typeHint): bool
4714
{
48-
return in_array($typeHint, self::$simpleIterableTypeHints, true);
15+
return in_array($typeHint, self::getSimpleIterableTypeHints(), true);
4916
}
5017

5118
public static function convertLongSimpleTypeHintToShort(string $typeHint): string
@@ -67,4 +34,52 @@ public static function getFullyQualifiedTypeHint(\PHP_CodeSniffer_File $phpcsFil
6734
return NamespaceHelper::resolveName($phpcsFile, $typeHint, $useStatements, $pointer);
6835
}
6936

37+
/**
38+
* @return string[]
39+
*/
40+
public static function getSimpleTypeHints(): array
41+
{
42+
return [
43+
'int',
44+
'integer',
45+
'float',
46+
'string',
47+
'bool',
48+
'boolean',
49+
'callable',
50+
'self',
51+
'array',
52+
'iterable',
53+
'void',
54+
];
55+
}
56+
57+
/**
58+
* @return string[]
59+
*/
60+
public static function getSimpleIterableTypeHints(): array
61+
{
62+
return [
63+
'array',
64+
'iterable',
65+
];
66+
}
67+
68+
/**
69+
* @return string[]
70+
*/
71+
public static function getSimpleUnofficialTypeHints(): array
72+
{
73+
return [
74+
'null',
75+
'mixed',
76+
'true',
77+
'false',
78+
'resource',
79+
'object',
80+
'static',
81+
'$this',
82+
];
83+
}
84+
7085
}

SlevomatCodingStandard/Sniffs/Namespaces/FullyQualifiedClassNameInAnnotationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function process(\PHP_CodeSniffer_File $phpcsFile, $annotationTagPointer)
5151
foreach ($typeHints as $typeHint) {
5252
$typeHint = preg_replace('~(\[\])+$~', '', $typeHint);
5353
$lowercasedTypeHint = strtolower($typeHint);
54-
if (TypeHintHelper::isSimpleTypeHint($lowercasedTypeHint) || in_array($lowercasedTypeHint, TypeHintHelper::$simpleUnofficialTypeHints, true)) {
54+
if (TypeHintHelper::isSimpleTypeHint($lowercasedTypeHint) || in_array($lowercasedTypeHint, TypeHintHelper::getSimpleUnofficialTypeHints(), true)) {
5555
continue;
5656
}
5757

SlevomatCodingStandard/Sniffs/TypeHints/TypeHintDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ private function getSniffName(string $sniffName): string
719719

720720
private function isValidTypeHint(string $typeHint): bool
721721
{
722-
return TypeHintHelper::isSimpleTypeHint($typeHint) || !in_array($typeHint, TypeHintHelper::$simpleUnofficialTypeHints, true);
722+
return TypeHintHelper::isSimpleTypeHint($typeHint) || !in_array($typeHint, TypeHintHelper::getSimpleUnofficialTypeHints(), true);
723723
}
724724

725725
private function definitionContainsNullTypeHint(string $typeHintDefinition): bool

0 commit comments

Comments
 (0)