rectorphp / rector

Instant Upgrades and Automated Refactoring of any PHP 5.3+ code
https://getrector.com
MIT License
8.8k stars 688 forks source link

[code-quality] Error processing of trait - RemoveAlwaysTrueConditionSetInConstructorRector #2162

Closed lapetr closed 5 years ago

lapetr commented 5 years ago
Subject Details
Rector version v0.5.16, (0.6.x-dev@a7adee6)
PHP version PHP 7.3.10, (7.2.19, 7.4.0RC3)
Full Command /rector/vendor/bin/rector process --set code-quality --debug /tmp/trait-err/

Current Behaviour

PHP Fatal error

$ ls /tmp/trait-err 
FooTrait.php
$ /rector/vendor/bin/rector process  --set code-quality --debug  /tmp/trait-err/
Rector v0.5.16

[parsing] /tmp/trait-err/FooTrait.php
[refactoring] /tmp/trait-err/FooTrait.php
[applying] Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Rector\PhpParser\Node\Manipulator\ClassManipulator::getProperty() must be an instance of PhpParser\Node\Stmt\Class_, instance of PhpParser\Node\Stmt\Trait_ given, called in /rector/vendor/rector/rector/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php on line 138 and defined in /rector/vendor/rector/rector/src/PhpParser/Node/Manipulator/ClassManipulator.php:198
Stack trace:
#0 /rector/vendor/rector/rector/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php(138): Rector\PhpParser\Node\Manipulator\ClassManipulator->getProperty(Object(PhpParser\Node\Stmt\Trait_), 'x')
#1 /rector/vendor/rector/rector/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php(204): Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector->resolvePropertyFetchTypes(Object(PhpParser\Node\Expr\PropertyFetch))
#2 /var in /rector/vendor/rector/rector/src/PhpParser/Node/Manipulator/ClassManipulator.php on line 198

Minimal PHP Code Causing Issue

```php x ) {} } } ```
TomasVotruba commented 5 years ago

Could you try last dev-master?

lapetr commented 5 years ago

fb41b38f0

Rector 0.6.x-dev@a7adee6

[parsing] trait-err/FooTrait.php
[refactoring] trait-err/FooTrait.php
    [applying] Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to Rector\PhpParser\Node\Manipulator\ClassManipulator::getProperty() must be an instance of PhpParser\Node\Stmt\Class_, instance of PhpParser\Node\Stmt\Trait_ given, called in /packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php on line 140 and defined in /src/PhpParser/Node/Manipulator/ClassManipulator.php:200
Stack trace:
#0 /packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php(140): Rector\PhpParser\Node\Manipulator\ClassManipulator->getProperty(Object(PhpParser\Node\Stmt\Trait_), 'x')
#1 /packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php(206): Rector\CodeQuality\Rector\If_\RemoveAlwaysTrueConditionSetInConstructorRector->resolvePropertyFetchTypes(Object(PhpParser\Node\Expr\PropertyFetch))
#2 in /src/PhpParser/Node/Manipulator/ClassManipulator.php on line 200

this suppresses the error:

diff --git a/src/PhpParser/Node/Manipulator/ClassManipulator.php b/src/PhpParser/Node/Manipulator/ClassManipulator.php
index 7807ea32e..738c246bf 100644
--- a/src/PhpParser/Node/Manipulator/ClassManipulator.php
+++ b/src/PhpParser/Node/Manipulator/ClassManipulator.php
@@ -197,9 +197,9 @@ final class ClassManipulator
         return $usedTraits;
     }

-    public function getProperty(Class_ $class, string $name): ?Property
+    public function getProperty(ClassLike $classLike, string $name): ?Property
     {
-        foreach ($class->getProperties() as $property) {
+        foreach ($classLike->getProperties() as $property) {

phpunit: OK

TomasVotruba commented 5 years ago

this suppresses the error:

That's right place. Could you send PR with fix?