Skip to content

Commit

Permalink
Correct VerbosityLevel in TooWide*ReturnType rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 29, 2021
1 parent 8a538de commit 838d1e4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function processNode(Node $node, Scope $scope): array

$messages[] = RuleErrorBuilder::message(sprintf(
'Anonymous function never returns %s so it can be removed from the return typehint.',
$type->describe(VerbosityLevel::typeOnly())
$type->describe(VerbosityLevel::getRecommendedLevelByType($type))
))->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function processNode(Node $node, Scope $scope): array

$messages[] = RuleErrorBuilder::message(sprintf(
'Anonymous function never returns %s so it can be removed from the return typehint.',
$type->describe(VerbosityLevel::typeOnly())
$type->describe(VerbosityLevel::getRecommendedLevelByType($type))
))->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf(
'Function %s() never returns %s so it can be removed from the return typehint.',
$function->getName(),
$type->describe(VerbosityLevel::typeOnly())
$type->describe(VerbosityLevel::getRecommendedLevelByType($type))
))->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function processNode(Node $node, Scope $scope): array
'Method %s::%s() never returns %s so it can be removed from the return typehint.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
$type->describe(VerbosityLevel::typeOnly())
$type->describe(VerbosityLevel::getRecommendedLevelByType($type))
))->build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ public function testPublicProtectedWithInheritance(): void
]);
}

public function testBug5095(): void
{
$this->analyse([__DIR__ . '/data/bug-5095.php'], [
[
'Method Bug5095\Parser::unaryOperatorFor() never returns \'not\' so it can be removed from the return typehint.',
21,
],
]);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/TooWideTypehints/data/bug-5095.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug5095;

final class UnaryOperator
{
const PLUS = '+';
const MINUS = '-';
const DIVIDE = '/';
const NOT = 'not';
}

class Parser
{
/**
* Returns the unary operator corresponding to $character, or `null` if
* the character is not a unary operator.
*
* @phpstan-return UnaryOperator::*|null
*/
private function unaryOperatorFor(string $character): ?string
{
switch ($character) {
case '+':
return UnaryOperator::PLUS;

case '-':
return UnaryOperator::MINUS;

case '/':
return UnaryOperator::DIVIDE;

default:
return null;
}
}
}

0 comments on commit 838d1e4

Please sign in to comment.