Skip to content

Commit 0bac10b

Browse files
alies-devkukulich
authored andcommitted
Support $this as a valid traversable parameter type
1 parent ddadd06 commit 0bac10b

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ public static function containsItemsSpecificationForTraversable(
227227
);
228228
}
229229

230+
if ($typeNode instanceof ThisTypeNode) {
231+
return $inTraversable;
232+
}
233+
230234
if ($typeNode instanceof ConstTypeNode) {
231235
return $inTraversable;
232236
}

tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,18 @@ public function testWithNullTrueFalseErrors(): void
240240
self::assertAllFixedInFile($report);
241241
}
242242

243+
public function testWithThisInGenericsNoErrors(): void
244+
{
245+
$report = self::checkFile(__DIR__ . '/data/returnTypeHintWithTraversableNoErrors.php', [
246+
'enableObjectTypeHint' => true,
247+
'enableMixedTypeHint' => true,
248+
'enableUnionTypeHint' => false,
249+
'enableIntersectionTypeHint' => false,
250+
'enableNeverTypeHint' => false,
251+
'enableStandaloneNullTrueFalseTypeHints' => false,
252+
'traversableTypeHints' => ['Generic'],
253+
]);
254+
self::assertNoSniffErrorInFile($report);
255+
}
256+
243257
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php // lint >= 8.0
2+
3+
namespace FooNamespace;
4+
5+
/**
6+
* @template T
7+
* @template U
8+
*/
9+
class Generic
10+
{
11+
}
12+
13+
class Bar
14+
{
15+
}
16+
17+
class Whatever
18+
{
19+
20+
/**
21+
* @return Generic<Bar, $this>
22+
*/
23+
public function withThisTemplateType(): Generic
24+
{
25+
return new Generic();
26+
}
27+
28+
/**
29+
* @return Generic<Bar, self>
30+
*/
31+
public function withSelfTemplateType(): Generic
32+
{
33+
return new Generic();
34+
}
35+
36+
/**
37+
* @return Generic<Bar, static>
38+
*/
39+
public function withStaticTemplateType(): Generic
40+
{
41+
return new Generic();
42+
}
43+
44+
/**
45+
* @return Generic<$this, Bar>
46+
*/
47+
public function withThisAsFirstParam(): Generic
48+
{
49+
return new Generic();
50+
}
51+
}

0 commit comments

Comments
 (0)