Skip to content

Commit f9b5c71

Browse files
committed
fix NotShouldPhpdocReturnIfExistTypeHint with multiple errors
1 parent 7bbc9c7 commit f9b5c71

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/Rule/NotShouldPhpdocReturnIfExistTypeHint.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function processNode(Node $node, Scope $scope): array
4444
->getNativeReflection();
4545

4646
$methods = $reflection->getMethods();
47-
47+
$errors = [];
4848
foreach ($methods as $method) {
4949
if (str_starts_with($method->getName(), '__')) {
5050
continue;
@@ -64,6 +64,7 @@ public function processNode(Node $node, Scope $scope): array
6464
$returnTypeName = $returnType->getName();
6565
$tokens = new TokenIterator($this->phpDocLexer->tokenize($doc));
6666
$text = $this->parser->parse($tokens);
67+
6768
foreach ($text->getTags() as $tag) {
6869
if ($tag->name !== '@return') {
6970
continue;
@@ -72,11 +73,12 @@ public function processNode(Node $node, Scope $scope): array
7273
$value = $tag->value->type->name;
7374
if ($value == $returnTypeName
7475
&& $reflection->getName() === $method->getBetterReflection()->getLocatedSource()->getName()) {
75-
return ['PhpDoc attribute @return for method ' . $method->getName() . ' can be remove'];
76+
$errors[] ='PhpDoc attribute @return for method ' . $method->getName() . ' can be remove';
7677
}
7778
}
7879
}
80+
7981
}
80-
return [];
82+
return $errors;
8183
}
8284
}

tests/Fixture/Return/MethodsWithTypeHintAndReturn.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ public function someMethod(): bool
1313
{
1414
return true;
1515
}
16+
17+
/**
18+
* @return int
19+
*/
20+
private function getInt(): int
21+
{
22+
return 0;
23+
}
1624
}

tests/Rules/NotShouldPhpdocReturnIfExistTypeHintTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NotShouldPhpdocReturnIfExistTypeHintTest extends RuleTestCase
1818
/**
1919
* @inheritDoc
2020
*/
21-
protected function getRule(): Rule
21+
public function getRule(): Rule
2222
{
2323
return new NotShouldPhpdocReturnIfExistTypeHint(
2424
$this->createReflectionProvider(),
@@ -33,7 +33,11 @@ public function testWithError(): void
3333
[
3434
'PhpDoc attribute @return for method someMethod can be remove',
3535
7
36-
]
36+
],
37+
[
38+
'PhpDoc attribute @return for method getInt can be remove',
39+
7
40+
],
3741
]);
3842
}
3943
}

0 commit comments

Comments
 (0)