Skip to content

Commit 41970f3

Browse files
committed
Fix #1768
* Correctly handle all current and future string types * fix non-empty-array not correctly set as native type but set with hyphens
1 parent b4f9f02 commit 41970f3

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,17 @@ public static function getTypeHintFromOneType(
307307
return 'int';
308308
}
309309

310-
if (in_array(
311-
strtolower($typeNode->name),
312-
['class-string', 'trait-string', 'callable-string', 'numeric-string', 'non-empty-string', 'non-falsy-string', 'literal-string'],
313-
true,
314-
)) {
310+
// 'class-string', 'trait-string', 'callable-string', 'numeric-string', 'non-empty-string', 'non-falsy-string', 'literal-string',...
311+
// see https://psalm.dev/docs/annotating_code/type_syntax/scalar_types/#class-string-interface-string
312+
if (preg_match('/-string$/', strtolower($typeNode->name))) {
315313
return 'string';
316314
}
317315

316+
// here when used literally, e.g. non-empty-array|null
317+
if (in_array(strtolower($typeNode->name), ['non-empty-array', 'list', 'non-empty-list'], true)) {
318+
return 'array';
319+
}
320+
318321
return $typeNode->name;
319322
}
320323

tests/Helpers/TypeHintHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ public static function dataTypeHintEqualsAnnotation(): array
379379
return [
380380
['scalar', true],
381381
['unionIsNotIntersection', false],
382+
['fooFunctionWithReturnAnnotationComplexString', true],
383+
['fooFunctionWithReturnAnnotationSimpleHyphenedIterable', true],
382384
];
383385
}
384386

tests/Helpers/data/typeHintEqualsAnnotation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ function scalar(): int|bool|float|string
1414
function unionIsNotIntersection(): Foo|Bar
1515
{
1616
}
17+
18+
/**
19+
* @return non-empty-lowercase-string
20+
*/
21+
function fooFunctionWithReturnAnnotationComplexString(): string
22+
{
23+
24+
}
25+
26+
/**
27+
* @return non-empty-array|null
28+
*/
29+
function fooFunctionWithReturnAnnotationSimpleHyphenedIterable(): ?array
30+
{
31+
32+
}

0 commit comments

Comments
 (0)