Skip to content

Commit

Permalink
UselessFunctionReturnValueRule: More precise error message
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Aug 1, 2024
1 parent 7328128 commit 35a2f57
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
13 changes: 10 additions & 3 deletions src/Rules/Functions/UselessFunctionReturnValueRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function array_key_exists;
use function count;
use function in_array;
use function sprintf;

/**
Expand All @@ -20,6 +20,12 @@
class UselessFunctionReturnValueRule implements Rule
{

private const USELESS_FUNCTIONS = [
'var_export' => 'null',
'print_r' => 'true',
'highlight_string' => 'true',
];

public function __construct(private ReflectionProvider $reflectionProvider)
{
}
Expand All @@ -41,7 +47,7 @@ public function processNode(Node $funcCall, Scope $scope): array

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

if (!in_array($functionReflection->getName(), ['var_export', 'print_r', 'highlight_string', 'highlight_file', 'show_source'], true)) {
if (!array_key_exists($functionReflection->getName(), self::USELESS_FUNCTIONS)) {
return [];
}
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
Expand All @@ -64,8 +70,9 @@ public function processNode(Node $funcCall, Scope $scope): array
if (count($reorderedArgs) === 1 || (count($reorderedArgs) >= 2 && $scope->getType($reorderedArgs[1]->value)->isFalse()->yes())) {
return [RuleErrorBuilder::message(
sprintf(
'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.',
'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.',
$functionReflection->getName(),
self::USELESS_FUNCTIONS[$functionReflection->getName()],
2,
$parametersAcceptor->getParameters()[1]->getName(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,13 @@ public function testUselessReturnValue(): void
47,
],
[
'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.',
'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.',
56,
],
[
'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.',
64,
],
[
'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.',
73,
],
]);
}

Expand Down
5 changes: 0 additions & 5 deletions tests/PHPStan/Rules/Functions/data/useless-fn-return.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,4 @@ public function explicitNoReturn(): void
);
}

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

}

0 comments on commit 35a2f57

Please sign in to comment.