Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downgrade became buggy between 0.12.5 and 0.12.8 #6878

Closed
ondrejmirtes opened this issue Dec 19, 2021 · 5 comments · Fixed by rectorphp/rector-src#1530
Closed

Downgrade became buggy between 0.12.5 and 0.12.8 #6878

ondrejmirtes opened this issue Dec 19, 2021 · 5 comments · Fixed by rectorphp/rector-src#1530
Labels

Comments

@ondrejmirtes
Copy link
Contributor

Bug Report

Subject Details
Rector version 0.12.8

This is a diff how the downgrade processed differently between Rector 0.12.5 and 0.12.8. This ISN'T a diff of non-downgraded and downgraded code. The "before" state of the diff is code downgraded by 0.12.5 and the "after" state of the diff is code downgraded by 0.12.8:

diff --git a/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php b/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
index 6e6a9d50..1a329605 100644
--- a/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
+++ b/src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
@@ -369,7 +369,7 @@ final class ReflectionSourceStubber implements SourceStubber
                 }
             }

-            $propertyType = $propertyReflection->getType();
+            $propertyType = null;

             assert($propertyType instanceof CoreReflectionNamedType || $propertyType instanceof CoreReflectionUnionType || $propertyType instanceof CoreReflectionIntersectionType || $propertyType === null);

diff --git a/test/unit/Reflection/Adapter/ReflectionClassConstantTest.php b/test/unit/Reflection/Adapter/ReflectionClassConstantTest.php
index bac2c7ee..a12c9386 100644
--- a/test/unit/Reflection/Adapter/ReflectionClassConstantTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionClassConstantTest.php
@@ -165,7 +165,7 @@ class ReflectionClassConstantTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionClassConstantAdapter = new ReflectionClassConstantAdapter($betterReflectionClassConstant);
-        $attributes                     = $reflectionClassConstantAdapter->getAttributes();
+        $attributes                     = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -195,7 +195,7 @@ class ReflectionClassConstantTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionClassAdapter = new ReflectionClassConstantAdapter($betterReflectionClassConstant);
-        $attributes             = $reflectionClassAdapter->getAttributes('SomeAttribute');
+        $attributes             = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -290,9 +290,9 @@ class ReflectionClassConstantTest extends TestCase

         $reflectionClassConstantAdapter = new ReflectionClassConstantAdapter($betterReflectionClassConstant);

-        self::assertCount(1, $reflectionClassConstantAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionClassConstantAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionClassConstantAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -301,7 +301,7 @@ class ReflectionClassConstantTest extends TestCase
         $reflectionClassConstantAdapter = new ReflectionClassConstantAdapter($betterReflectionClassConstant);

         self::expectException(Error::class);
-        $reflectionClassConstantAdapter->getAttributes(null, 123);
+        [];
     }

     public function dataIsEnumCase(): array
diff --git a/test/unit/Reflection/Adapter/ReflectionClassTest.php b/test/unit/Reflection/Adapter/ReflectionClassTest.php
index d5fa96b0..632cf4a3 100644
--- a/test/unit/Reflection/Adapter/ReflectionClassTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionClassTest.php
@@ -802,7 +802,7 @@ class ReflectionClassTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);
-        $attributes             = $reflectionClassAdapter->getAttributes();
+        $attributes             = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -832,7 +832,7 @@ class ReflectionClassTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);
-        $attributes             = $reflectionClassAdapter->getAttributes('SomeAttribute');
+        $attributes             = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -927,9 +927,9 @@ class ReflectionClassTest extends TestCase

         $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

-        self::assertCount(1, $reflectionClassAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionClassAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionClassAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -938,7 +938,7 @@ class ReflectionClassTest extends TestCase
         $reflectionClassAdapter = new ReflectionClassAdapter($betterReflectionClass);

         self::expectException(Error::class);
-        $reflectionClassAdapter->getAttributes(null, 123);
+        [];
     }

     public function testHasConstantWithEnumCase(): void
diff --git a/test/unit/Reflection/Adapter/ReflectionFunctionTest.php b/test/unit/Reflection/Adapter/ReflectionFunctionTest.php
index 7b2f95f8..ee13e5b1 100644
--- a/test/unit/Reflection/Adapter/ReflectionFunctionTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionFunctionTest.php
@@ -251,7 +251,7 @@ class ReflectionFunctionTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionFunctionAdapter = new ReflectionFunctionAdapter($betterReflectionFunction);
-        $attributes                = $reflectionFunctionAdapter->getAttributes();
+        $attributes                = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -281,7 +281,7 @@ class ReflectionFunctionTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionFunctionAdapter = new ReflectionFunctionAdapter($betterReflectionFunction);
-        $attributes                = $reflectionFunctionAdapter->getAttributes('SomeAttribute');
+        $attributes                = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -376,9 +376,9 @@ class ReflectionFunctionTest extends TestCase

         $reflectionFunctionAdapter = new ReflectionFunctionAdapter($betterReflectionFunction);

-        self::assertCount(1, $reflectionFunctionAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionFunctionAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionFunctionAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -387,6 +387,6 @@ class ReflectionFunctionTest extends TestCase
         $reflectionFunctionAdapter = new ReflectionFunctionAdapter($betterReflectionFunction);

         self::expectException(Error::class);
-        $reflectionFunctionAdapter->getAttributes(null, 123);
+        [];
     }
 }
diff --git a/test/unit/Reflection/Adapter/ReflectionMethodTest.php b/test/unit/Reflection/Adapter/ReflectionMethodTest.php
index 59bd0b77..5d3fcab3 100644
--- a/test/unit/Reflection/Adapter/ReflectionMethodTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionMethodTest.php
@@ -406,7 +406,7 @@ class ReflectionMethodTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionMethodAdapter = new ReflectionMethodAdapter($betterReflectionMethod);
-        $attributes              = $reflectionMethodAdapter->getAttributes();
+        $attributes              = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -436,7 +436,7 @@ class ReflectionMethodTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionMethodAdapter = new ReflectionMethodAdapter($betterReflectionMethod);
-        $attributes              = $reflectionMethodAdapter->getAttributes('SomeAttribute');
+        $attributes              = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -531,9 +531,9 @@ class ReflectionMethodTest extends TestCase

         $reflectionMethodAdapter = new ReflectionMethodAdapter($betterReflectionMethod);

-        self::assertCount(1, $reflectionMethodAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionMethodAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionMethodAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -542,6 +542,6 @@ class ReflectionMethodTest extends TestCase
         $reflectionMethodAdapter = new ReflectionMethodAdapter($betterReflectionMethod);

         self::expectException(Error::class);
-        $reflectionMethodAdapter->getAttributes(null, 123);
+        [];
     }
 }
diff --git a/test/unit/Reflection/Adapter/ReflectionObjectTest.php b/test/unit/Reflection/Adapter/ReflectionObjectTest.php
index fd2b3cd4..2297f32a 100644
--- a/test/unit/Reflection/Adapter/ReflectionObjectTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionObjectTest.php
@@ -678,7 +678,7 @@ class ReflectionObjectTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject);
-        $attributes              = $reflectionObjectAdapter->getAttributes();
+        $attributes              = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -716,7 +716,7 @@ class ReflectionObjectTest extends TestCase

         $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject);

-        $attributes = $reflectionObjectAdapter->getAttributes('SomeAttribute');
+        $attributes = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -818,9 +818,9 @@ class ReflectionObjectTest extends TestCase

         $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject);

-        self::assertCount(1, $reflectionObjectAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionObjectAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionObjectAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -829,6 +829,6 @@ class ReflectionObjectTest extends TestCase
         $reflectionObjectAdapter = new ReflectionObjectAdapter($betterReflectionObject);

         self::expectException(Error::class);
-        $reflectionObjectAdapter->getAttributes(null, 123);
+        [];
     }
 }
diff --git a/test/unit/Reflection/Adapter/ReflectionParameterTest.php b/test/unit/Reflection/Adapter/ReflectionParameterTest.php
index 775d2bf3..59d5055e 100644
--- a/test/unit/Reflection/Adapter/ReflectionParameterTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionParameterTest.php
@@ -145,7 +145,7 @@ class ReflectionParameterTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionParameterAdapter = new ReflectionParameterAdapter($betterReflectionParameter);
-        $attributes                 = $reflectionParameterAdapter->getAttributes();
+        $attributes                 = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -175,7 +175,7 @@ class ReflectionParameterTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionParameterAdapter = new ReflectionParameterAdapter($betterReflectionParameter);
-        $attributes                 = $reflectionParameterAdapter->getAttributes('SomeAttribute');
+        $attributes                 = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -270,9 +270,9 @@ class ReflectionParameterTest extends TestCase

         $reflectionParameterAdapter = new ReflectionParameterAdapter($betterReflectionParameter);

-        self::assertCount(1, $reflectionParameterAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionParameterAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionParameterAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -281,6 +281,6 @@ class ReflectionParameterTest extends TestCase
         $reflectionParameterAdapter = new ReflectionParameterAdapter($betterReflectionParameter);

         self::expectException(Error::class);
-        $reflectionParameterAdapter->getAttributes(null, 123);
+        [];
     }
 }
diff --git a/test/unit/Reflection/Adapter/ReflectionPropertyTest.php b/test/unit/Reflection/Adapter/ReflectionPropertyTest.php
index 08abd273..87f6b588 100644
--- a/test/unit/Reflection/Adapter/ReflectionPropertyTest.php
+++ b/test/unit/Reflection/Adapter/ReflectionPropertyTest.php
@@ -345,7 +345,7 @@ class ReflectionPropertyTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionPropertyAdapter = new ReflectionPropertyAdapter($betterReflectionProperty);
-        $attributes                = $reflectionPropertyAdapter->getAttributes();
+        $attributes                = [];

         self::assertCount(2, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -375,7 +375,7 @@ class ReflectionPropertyTest extends TestCase
             ->willReturn($betterReflectionAttributes);

         $reflectionPropertyAdapter = new ReflectionPropertyAdapter($betterReflectionProperty);
-        $attributes                = $reflectionPropertyAdapter->getAttributes('SomeAttribute');
+        $attributes                = [];

         self::assertCount(1, $attributes);
         self::assertSame('SomeAttribute', $attributes[0]->getName());
@@ -470,9 +470,9 @@ class ReflectionPropertyTest extends TestCase

         $reflectionPropertyAdapter = new ReflectionPropertyAdapter($betterReflectionProperty);

-        self::assertCount(1, $reflectionPropertyAdapter->getAttributes('ClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionPropertyAdapter->getAttributes('ParentClassName', ReflectionAttributeAdapter::IS_INSTANCEOF));
-        self::assertCount(2, $reflectionPropertyAdapter->getAttributes('InterfaceName', ReflectionAttributeAdapter::IS_INSTANCEOF));
+        self::assertCount(1, []);
+        self::assertCount(2, []);
+        self::assertCount(2, []);
     }

     public function testGetAttributesThrowsExceptionForInvalidFlags(): void
@@ -481,6 +481,6 @@ class ReflectionPropertyTest extends TestCase
         $reflectionPropertyAdapter = new ReflectionPropertyAdapter($betterReflectionProperty);

         self::expectException(Error::class);
-        $reflectionPropertyAdapter->getAttributes(null, 123);
+        [];
     }
 }

I don't understand why Rector does any of these changes, and I don't know how to debug what's responsible for them.

The configuration is:

<?php declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $parameters = $containerConfigurator->parameters();

    $parameters->set(Option::SKIP, [
        '*/test/unit/Fixture/*',
        'src/Reflection/Adapter/ReflectionEnum*'
    ]);
    $containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_73);
};
@TomasVotruba
Copy link
Member

The getAttributes() is due to missing method completely on PHP 7.4 and lower.
Reported in deprecated-packages/symplify#3801

The responsible Rector rule: rectorphp/rector-src#1407


The getType() should be resolved better. Similar issue, this method does not exist on PHP 7.3 and lower.

Responsible Rector rule: rectorphp/rector-src#1406

@ondrejmirtes
Copy link
Contributor Author

Can I exclude these rules while still using $containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_73)? I'm using the downgrade in BetterReflection which is kind of special because these methods are actually always defined there thanks to these classes https://github.com/ondrejmirtes/BetterReflection/tree/master/src/Reflection/Adapter

@samsonasik
Copy link
Member

@ondrejmirtes you can skip rule via Option::SKIP:

$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SKIP, [
    \Rector\DowngradePhp74\Rector\MethodCall\DowngradeReflectionGetTypeRector::class,
]);

@ondrejmirtes
Copy link
Contributor Author

Thank you!

@ondrejmirtes
Copy link
Contributor Author

BTW What I think is problematic here is that even when the code is downgraded to pre-7.4, the code might still run on newer versions of PHP where the thing isn't always null. I think that a better replacement for calls like ->getType() isn't always null, but maybe some method_exists($obj, 'getType') ? $obj->getType() : null ternary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants