Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ public static function containsItemsSpecificationForTraversable(
);
}

if ($typeNode instanceof ThisTypeNode) {
return $inTraversable;
}

if ($typeNode instanceof ConstTypeNode) {
return $inTraversable;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Sniffs/TypeHints/ReturnTypeHintSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,18 @@ public function testWithNullTrueFalseErrors(): void
self::assertAllFixedInFile($report);
}

public function testWithThisInGenericsNoErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/returnTypeHintWithTraversableNoErrors.php', [
'enableObjectTypeHint' => true,
'enableMixedTypeHint' => true,
'enableUnionTypeHint' => false,
'enableIntersectionTypeHint' => false,
'enableNeverTypeHint' => false,
'enableStandaloneNullTrueFalseTypeHints' => false,
'traversableTypeHints' => ['Generic'],
]);
self::assertNoSniffErrorInFile($report);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php // lint >= 8.0

namespace FooNamespace;

/**
* @template T
* @template U
*/
class Generic
{
}

class Bar
{
}

class Whatever
{

/**
* @return Generic<Bar, $this>
*/
public function withThisTemplateType(): Generic
{
return new Generic();
}

/**
* @return Generic<Bar, self>
*/
public function withSelfTemplateType(): Generic
{
return new Generic();
}

/**
* @return Generic<Bar, static>
*/
public function withStaticTemplateType(): Generic
{
return new Generic();
}

/**
* @return Generic<$this, Bar>
*/
public function withThisAsFirstParam(): Generic
{
return new Generic();
}
}