Skip to content

Commit 0f96768

Browse files
committed
improve error message
1 parent 82966af commit 0f96768

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/Rules/Functions/UselessFunctionReturnValueRule.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Rules\RuleErrorBuilder;
1313
use function count;
1414
use function in_array;
15+
use function sprintf;
1516

1617
/**
1718
* @implements Rule<Node\Expr\FuncCall>
@@ -61,7 +62,14 @@ public function processNode(Node $funcCall, Scope $scope): array
6162
$reorderedArgs = $reorderedFuncCall->getArgs();
6263

6364
if (count($reorderedArgs) === 1 || (count($reorderedArgs) >= 2 && $scope->getType($reorderedArgs[1]->value)->isFalse()->yes())) {
64-
return [RuleErrorBuilder::message('Return value of call to function ' . $functionReflection->getName() . ' is useless.')
65+
return [RuleErrorBuilder::message(
66+
sprintf(
67+
'Return value of function %s is always true and the result is printed instead of being returned. Pass in true as parameter #%d $%s to return the output instead.',
68+
$functionReflection->getName(),
69+
2,
70+
$parametersAcceptor->getParameters()[1]->getName(),
71+
),
72+
)
6573
->identifier('function.uselessReturnValue')
6674
->line($funcCall->getStartLine())
6775
->build(),

tests/PHPStan/Rules/Functions/UselessFunctionReturnValueRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public function testUselessReturnValue(): void
2323
{
2424
$this->analyse([__DIR__ . '/data/useless-fn-return.php'], [
2525
[
26-
'Return value of call to function print_r is useless.',
26+
'Return value of function print_r is always true and the result is printed instead of being returned. Pass in true as parameter #2 $return to return the output instead.',
2727
47,
2828
],
2929
[
30-
'Return value of call to function var_export is useless.',
30+
'Return value of function var_export is always true and the result is printed instead of being returned. Pass in true as parameter #2 $return to return the output instead.',
3131
56,
3232
],
3333
[
34-
'Return value of call to function print_r is useless.',
34+
'Return value of function print_r is always true and the result is printed instead of being returned. Pass in true as parameter #2 $return to return the output instead.',
3535
64,
3636
],
3737
]);
@@ -45,7 +45,7 @@ public function testUselessReturnValuePhp8(): void
4545

4646
$this->analyse([__DIR__ . '/data/useless-fn-return-php8.php'], [
4747
[
48-
'Return value of call to function print_r is useless.',
48+
'Return value of function print_r is always true and the result is printed instead of being returned. Pass in true as parameter #2 $return to return the output instead.',
4949
18,
5050
],
5151
]);

0 commit comments

Comments
 (0)