Skip to content

Commit

Permalink
Disallow empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 9, 2018
1 parent 8d121df commit f06d0ca
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
* Statically declared methods are called statically.
* Disallow `empty()` - it's a very loose comparison (see [manual)(https://secure.php.net/manual/en/function.empty.php)), it's recommended to use more strict one.

Additional rules are coming in subsequent releases!

Expand Down
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rules:
- PHPStan\Rules\BooleansInConditions\BooleanInElseIfConditionRule
- PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule
- PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
- PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule
- PHPStan\Rules\StrictCalls\StrictFunctionCallsRule
- PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule
28 changes: 28 additions & 0 deletions src/Rules/DisallowedConstructs/DisallowedEmptyRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\DisallowedConstructs;

use PhpParser\Node;
use PHPStan\Analyser\Scope;

class DisallowedEmptyRule implements \PHPStan\Rules\Rule
{

public function getNodeType(): string
{
return \PhpParser\Node\Expr\Empty_::class;
}

/**
* @param \PhpParser\Node\Expr\Empty_ $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [
'Construct empty() is not allowed. Use more strict comparison.',
];
}

}
25 changes: 25 additions & 0 deletions tests/Rules/DisallowedConstructs/DisallowedEmptyRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\DisallowedConstructs;

use PHPStan\Rules\Rule;

class DisallowedEmptyRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
return new DisallowedEmptyRule();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/empty.php'], [
[
'Construct empty() is not allowed. Use more strict comparison.',
3,
],
]);
}

}
5 changes: 5 additions & 0 deletions tests/Rules/DisallowedConstructs/data/empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

if (empty($test)) {

}

0 comments on commit f06d0ca

Please sign in to comment.