-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rules] Enable NoEmptyOnObjectRule and make same logic on possible un…
…defined variable (#15)
- Loading branch information
1 parent
539b542
commit 5d4f437
Showing
7 changed files
with
133 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoEmptyOnObjectRule\Fixture; | ||
|
||
use stdClass; | ||
|
||
final class EmptyOnObject | ||
{ | ||
public function run() | ||
{ | ||
$object = null; | ||
|
||
if (mt_rand(0, 100)) { | ||
$object = new stdClass(); | ||
} | ||
|
||
if (!empty($object)) { | ||
return $object; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rules/NoEmptyOnObjectRule/Fixture/SkipEmptyOnArray.php
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoEmptyOnObjectRule\Fixture; | ||
|
||
final class SkipEmptyOnArray | ||
{ | ||
public function run(array $values) | ||
{ | ||
if (!empty($values[9])) { | ||
return $values[9]; | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Rules/NoEmptyOnObjectRule/Fixture/SkipEmptyOnArrayNestedOnObject.php
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoEmptyOnObjectRule\Fixture; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
|
||
final class SkipEmptyOnArrayNestedOnObject | ||
{ | ||
public function run(MethodCall $methodCall) | ||
{ | ||
if (!empty($methodCall->args[9])) { | ||
return $methodCall->args[9]; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Rules/NoEmptyOnObjectRule/Fixture/SkipPossibleUndefinedVariable.php
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoIssetOnObjectRule\Fixture; | ||
|
||
final class SkipPossibleUndefinedVariable | ||
{ | ||
public function run(bool $condition) | ||
{ | ||
if ($condition) { | ||
$object = new \stdClass(); | ||
} | ||
|
||
if (!empty($object)) { | ||
$object->foo = 'bar'; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/Rules/NoEmptyOnObjectRule/NoEmptyOnObjectRuleTest.php
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoEmptyOnObjectRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\TypePerfect\Rules\NoEmptyOnObjectRule; | ||
|
||
final class NoEmptyOnObjectRuleTest extends RuleTestCase | ||
{ | ||
/** | ||
* @param mixed[] $expectedErrorMessagesWithLines | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield [__DIR__ . '/Fixture/EmptyOnObject.php', [[NoEmptyOnObjectRule::ERROR_MESSAGE, 19]]]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipEmptyOnArray.php', []]; | ||
yield [__DIR__ . '/Fixture/SkipEmptyOnArrayNestedOnObject.php', []]; | ||
yield [__DIR__ . '/Fixture/SkipPossibleUndefinedVariable.php', []]; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [__DIR__ . '/../../../config/extension.neon']; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(NoEmptyOnObjectRule::class); | ||
} | ||
} |