-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rules] Skip Property fetch on NoIssetOnObjectRule
- Loading branch information
1 parent
9388c31
commit d225589
Showing
7 changed files
with
95 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Guard; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
final class EmptyIssetGuard | ||
{ | ||
public function isLegal(Expr $expr, Scope $scope): bool | ||
{ | ||
if ($expr instanceof ArrayDimFetch) { | ||
return true; | ||
} | ||
|
||
if (! $expr instanceof Variable) { | ||
return true; | ||
} | ||
|
||
if ($expr->name instanceof Expr) { | ||
return true; | ||
} | ||
|
||
if (! $scope->hasVariableType($expr->name)->yes()) { | ||
return true; | ||
} | ||
|
||
$varType = $scope->getType($expr); | ||
$varType = TypeCombinator::removeNull($varType); | ||
return $varType->getObjectClassNames() === []; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
tests/Rules/NoIssetOnObjectRule/Fixture/SkipIssetOnPropertyFetch.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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoIssetOnObjectRule\Fixture; | ||
|
||
use Rector\TypePerfect\Tests\Rules\NoIssetOnObjectRule\Source\Foo; | ||
|
||
class SkipIssetOnPropertyFetch | ||
{ | ||
private Foo $foo; | ||
|
||
public function sayHello(): void | ||
{ | ||
$this->initializeFoo(); | ||
|
||
$this->foo->sayHello(); | ||
} | ||
|
||
private function initializeFoo(): void | ||
{ | ||
if (!isset($this->foo)) | ||
{ | ||
$this->foo = new Foo(); | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypePerfect\Tests\Rules\NoIssetOnObjectRule\Source; | ||
|
||
class Foo | ||
{ | ||
public function sayHello(): void | ||
{ | ||
echo 'Hello'; | ||
} | ||
} |