-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Add a check for usage of the empty() construct (#188)
- Loading branch information
1 parent
6ac1699
commit 62eeea7
Showing
5 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Unleash\Client\PhpstanRules; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\ResourceType; | ||
|
||
final readonly class NoEmptyFunctionRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Empty_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
assert($node instanceof Node\Expr\Empty_); | ||
|
||
$hint = ''; | ||
$expression = $node->expr; | ||
if ($expression instanceof Node\Expr\Variable) { | ||
if (is_string($expression->name)) { | ||
$type = $scope->getVariableType($expression->name); | ||
if ($type->isArray()->yes()) { | ||
$hint = "You're probably looking for `!count(\${$expression->name})`."; | ||
} elseif ($type->isBoolean()->yes()) { | ||
$hint = "You're probably looking for `!\${$expression->name}`."; | ||
} elseif ($type->isFloat()->yes()) { | ||
$hint = "You're probably looking for `\${$expression->name} === 0.0 || \${$expression->name} === -0.0` or simply `!\${$expression->name}`."; | ||
} elseif ($type->isInteger()->yes()) { | ||
$hint = "You're probably looking for `\${$expression->name} === 0` or simply `!\${$expression->name}`."; | ||
} elseif ($type->isString()->yes()) { | ||
$hint = "You're probably looking for `\${$expression->name} === '' || \${$expression->name} === '0'` or simply `!\${$expression->name}`."; | ||
} elseif ($type->isNull()->yes()) { | ||
$hint = "You're probably looking for '\${$expression->name} === null` or simply `!\${$expression->name}`."; | ||
} elseif ($type->isObject()->yes()) { | ||
$hint = "Checking for empty on an object always returns false (except for some internal objects, but you shouldn't rely on this behavior)."; | ||
} elseif ($type instanceof ResourceType) { | ||
$hint = 'Checking for empty on a resource always returns false.'; | ||
} | ||
} | ||
} | ||
|
||
$message = 'Never use empty(), always check specifically for what you want.'; | ||
if ($hint) { | ||
$message .= " {$hint}"; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message($message)->build(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters