rector icon indicating copy to clipboard operation
rector copied to clipboard

Rector prefixed 0.8.56: Class 'Rector\Core\Console\Style\SymfonyStyleFactory' not found

Open simivar opened this issue 6 years ago • 21 comments

Bug Report

Subject Details
Rector version 0.8.56
Installed as prefixed Rector PHAR

While trying to run a Rector with vendor/bin/rector there's an error happening:

❯ vendor/bin/rector
PHP Fatal error:  Uncaught Error: Class 'Rector\Core\Console\Style\SymfonyStyleFactory' not found in /Users/[...]/vendor/rector/rector-prefixed/bin/rector:39
Stack trace:
#0 {main}
  thrown in /Users/[...]/vendor/rector/rector-prefixed/bin/rector on line 39
Fatal error: Uncaught Error: Class 'Rector\Core\Console\Style\SymfonyStyleFactory' not found in /Users/[...]/vendor/rector/rector-prefixed/bin/rector:39
Stack trace:
#0 {main}
  thrown in /Users/[...]vendor/rector/rector-prefixed/bin/rector on line 39

Minimal PHP Code Causing Issue

Expected Behaviour

Rector running properly.

simivar avatar Nov 30 '20 09:11 simivar

I've seen @michaljusiega comment in #4606 but I feel like this is a separate issue not related to PHPStan. I came across that comment by luck and feel that a separate ticket will be easier to find and track for others so they know when it's fixed.

simivar avatar Nov 30 '20 09:11 simivar

No, is not related to PHPStan issue.

michaljusiega avatar Nov 30 '20 09:11 michaljusiega

duplicates https://github.com/rectorphp/rector/issues/4725

staabm avatar Nov 30 '20 17:11 staabm

It doesn't duplicates #4725 as I have it installed as a normal Composer dependency, not a global one.

simivar avatar Nov 30 '20 17:11 simivar

Sorry „duplicates“ was the wrong wording. It seems to be the same error though :)

staabm avatar Nov 30 '20 18:11 staabm

Hello,

Did there is any workaround to use rector to upgrade a symfony 2.8 project ?

bbouabou avatar Dec 07 '20 18:12 bbouabou

@TomasVotruba could you help us with this issue?

simivar avatar Dec 07 '20 18:12 simivar

@simivar Basically, this PR needs to be finished: https://github.com/rectorphp/rector/pull/4559 The fix is included there. I'm busy with other work, so no ETA on it. Any help is welcomed there.

TomasVotruba avatar Dec 07 '20 19:12 TomasVotruba

Hello,

Did there is any workaround to use rector to upgrade a symfony 2.8 project ?

A suggested workaround : use the previous version 0.8.52 wich is based on a phar.

If you current environment contains version 0.8.56, first of all remove it : composer remove rector/rector-prefixed.

Then install the previous version : composer require rector/rector-prefixed 0.8.52

Note : the command init does not work in this version. You will have to copy the rector.php.dist by hand in your main directory.

arnaud-fr avatar Dec 08 '20 07:12 arnaud-fr

Resolved by https://github.com/rectorphp/rector/pull/4559

TomasVotruba avatar Dec 27 '20 10:12 TomasVotruba

Still seeing this problem on 0.9.5

u01jmg3 avatar Jan 06 '21 16:01 u01jmg3

We'll need a reproducible repository to verify

TomasVotruba avatar Jan 06 '21 18:01 TomasVotruba

@TomasVotruba , I have the same error, also if I use Bamarni composer bin plugin.

But my repo is public: https://github.com/Aerendir/component-array-writer

It is only one class, so is perfect for debugging...

I'm trying to solve the issue, but I have no idea where to start... Maybe you can having more experience with the issue that seems already happened...

To reproduce

  1. Clone the repo locally
  2. Switch to dev branch
  3. Run vendor/bin/rector process --dry-run

Aerendir avatar Jan 11 '21 14:01 Aerendir

Ok, the first problem seems to be in vendor/rector/rector/bin/rector.php:104

    /**
     * In case Rector is installed as vendor dependency,
     * this autoloads the project vendor/autoload.php, including Rector
     */
    public function autoloadProjectAutoloaderFile(): void
    {
        $this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../../autoload.php');
+        $this->loadIfExistsAndNotLoadedYet(__DIR__ . '/../../../../vendor/autoload.php');
    }

As I'm using Bamarni Bin Composer plugin, the hard coded path is not correct as Rector is actually installed in vendor-bin/phpstan/vendor/rector/rector.

For this reason we need to go some levels up in the tree.

Adding the change proposed, the error changes to:

PHP Fatal error:  Uncaught TypeError: Symplify\PackageBuilder\Reflection\PrivatesCaller::callPrivateMethod(): Argument #3 ($arguments) must be of type array, Symfony\Component\Console\Input\ArgvInput given, called in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php on line 32 and defined in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php:20
Stack trace:
#0 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php(32): Symplify\PackageBuilder\Reflection\PrivatesCaller->callPrivateMethod(Object(Symfony\Component\Console\Application), 'configureIO', Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#1 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector.php(39): Rector\Core\Console\Style\SymfonyStyleFactory->create()
#2 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector(4): require_once('/Users/Aerendir...')
#3 {main}
  thrown in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php on line 20
Fatal error: Uncaught TypeError: Symplify\PackageBuilder\Reflection\PrivatesCaller::callPrivateMethod(): Argument #3 ($arguments) must be of type array, Symfony\Component\Console\Input\ArgvInput given, called in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php on line 32 and defined in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php:20
Stack trace:
#0 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php(32): Symplify\PackageBuilder\Reflection\PrivatesCaller->callPrivateMethod(Object(Symfony\Component\Console\Application), 'configureIO', Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#1 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector.php(39): Rector\Core\Console\Style\SymfonyStyleFactory->create()
#2 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector(4): require_once('/Users/Aerendir...')
#3 {main}
  thrown in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php on line 20

Aerendir avatar Jan 11 '21 14:01 Aerendir

it seems the mentioned fix is the same as the one I suggested in https://github.com/rectorphp/rector/pull/4726

staabm avatar Jan 11 '21 15:01 staabm

@staabm , similar, not the same: your attempt was to support global install; mine supports installation through Bamarni Composer Bin, that is anyway at project level...

Aerendir avatar Jan 11 '21 17:01 Aerendir

@TomasVotruba , the problem seems to be in Symplify.

I changed this:

    /**
     * @param object|string $object
     * @param mixed[] $arguments
     */
-    public function callPrivateMethod($object, string $methodName, array $arguments)
+    public function callPrivateMethod($object, string $methodName, $arguments)
    {
        $this->ensureIsNotNull($object, __METHOD__);

        if (is_string($object)) {
            $reflectionClass = new ReflectionClass($object);
            $object = $reflectionClass->newInstanceWithoutConstructor();
        }

        $methodReflection = $this->createAccessibleMethodReflection($object, $methodName);

+        if ($arguments instanceof ArgvInput) {
+            $arguments = $arguments->getArguments();
+        }

        return $methodReflection->invokeArgs($object, $arguments);
    }

But now I have other errors:

Aerendir@Archimede component-array-writer % vendor/bin/rector process --dry-run
PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function Symfony\Component\Console\Application::configureIO(), 0 passed and exactly 2 expected in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symfony/console/Application.php:880
Stack trace:
#0 [internal function]: Symfony\Component\Console\Application->configureIO()
#1 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php(36): ReflectionMethod->invokeArgs(Object(Symfony\Component\Console\Application), Array)
#2 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php(32): Symplify\PackageBuilder\Reflection\PrivatesCaller->callPrivateMethod(Object(Symfony\Component\Console\Application), 'configureIO', Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector.php(39): Rector\Core\Console\Style\SymfonyStyleFactory->create()
#4 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector(4): require_once('/Users/Aerendir...')
#5 {main}
  thrown in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symfony/console/Application.php on line 880
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Symfony\Component\Console\Application::configureIO(), 0 passed and exactly 2 expected in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symfony/console/Application.php:880
Stack trace:
#0 [internal function]: Symfony\Component\Console\Application->configureIO()
#1 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symplify/package-builder/src/Reflection/PrivatesCaller.php(36): ReflectionMethod->invokeArgs(Object(Symfony\Component\Console\Application), Array)
#2 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/src/Console/Style/SymfonyStyleFactory.php(32): Symplify\PackageBuilder\Reflection\PrivatesCaller->callPrivateMethod(Object(Symfony\Component\Console\Application), 'configureIO', Array, Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector.php(39): Rector\Core\Console\Style\SymfonyStyleFactory->create()
#4 /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/rector/rector/bin/rector(4): require_once('/Users/Aerendir...')
#5 {main}
  thrown in /Users/Aerendir/Documents/JooServer/Lab/Components/component-array-writer/vendor-bin/phpstan/vendor/symfony/console/Application.php on line 880

The problem seems to be deeper than what it appeared at a first sight.

Aerendir avatar Jan 11 '21 17:01 Aerendir

Neither barmani or global installation is supported. If paths will be resolved, the different vendor directory name or double nesting or package conflicts will appear. We've experienced that in various private projects, before the prefixed Rector was available.

Now prefixed version is way to go here.

TomasVotruba avatar Jan 11 '21 18:01 TomasVotruba

Ok, so Phive to the rescue!

Aerendir avatar Jan 11 '21 19:01 Aerendir

Also if this makes really complex to stay up to date as Dependabot doesn't support Phive I think 🤔

Aerendir avatar Jan 11 '21 19:01 Aerendir