Skip to content

Commit 35a2f57

Browse files
authored
UselessFunctionReturnValueRule: More precise error message
1 parent 7328128 commit 35a2f57

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

src/Rules/Functions/UselessFunctionReturnValueRule.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use PHPStan\Reflection\ReflectionProvider;
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
13+
use function array_key_exists;
1314
use function count;
14-
use function in_array;
1515
use function sprintf;
1616

1717
/**
@@ -20,6 +20,12 @@
2020
class UselessFunctionReturnValueRule implements Rule
2121
{
2222

23+
private const USELESS_FUNCTIONS = [
24+
'var_export' => 'null',
25+
'print_r' => 'true',
26+
'highlight_string' => 'true',
27+
];
28+
2329
public function __construct(private ReflectionProvider $reflectionProvider)
2430
{
2531
}
@@ -41,7 +47,7 @@ public function processNode(Node $funcCall, Scope $scope): array
4147

4248
$functionReflection = $this->reflectionProvider->getFunction($funcCall->name, $scope);
4349

44-
if (!in_array($functionReflection->getName(), ['var_export', 'print_r', 'highlight_string', 'highlight_file', 'show_source'], true)) {
50+
if (!array_key_exists($functionReflection->getName(), self::USELESS_FUNCTIONS)) {
4551
return [];
4652
}
4753
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
@@ -64,8 +70,9 @@ public function processNode(Node $funcCall, Scope $scope): array
6470
if (count($reorderedArgs) === 1 || (count($reorderedArgs) >= 2 && $scope->getType($reorderedArgs[1]->value)->isFalse()->yes())) {
6571
return [RuleErrorBuilder::message(
6672
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.',
73+
'Return value of function %s() is always %s and the result is printed instead of being returned. Pass in true as parameter #%d $%s to return the output instead.',
6874
$functionReflection->getName(),
75+
self::USELESS_FUNCTIONS[$functionReflection->getName()],
6976
2,
7077
$parametersAcceptor->getParameters()[1]->getName(),
7178
),

tests/PHPStan/Rules/Functions/UselessFunctionReturnValueRuleTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,13 @@ public function testUselessReturnValue(): void
2727
47,
2828
],
2929
[
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.',
30+
'Return value of function var_export() is always null 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
[
3434
'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
],
37-
[
38-
'Return value of function show_source() is always true and the result is printed instead of being returned. Pass in true as parameter #2 $return to return the output instead.',
39-
73,
40-
],
4137
]);
4238
}
4339

tests/PHPStan/Rules/Functions/data/useless-fn-return.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,4 @@ public function explicitNoReturn(): void
6868
);
6969
}
7070

71-
public function showSource(string $s): void
72-
{
73-
error_log("Email-Template couldn't be found by parameters:" . show_source($s));
74-
}
75-
7671
}

0 commit comments

Comments
 (0)