PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

Migration with Rector example outdated?

Open tomkyle opened this issue 3 years ago • 5 comments

This is:

- [X] a bug report
- [ ] a feature request
- [ X] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

It seems the migration example is outdated because the CLI option mentioned has been removed. Unfortunately the Rector docs are not helpful either to someone new to Rector.

What is the expected behavior?

According to the migration example, running the rector binary should do things.

What is the current behavior?

Rector CLI claims The "--set" option does not exist.

What are the steps to reproduce?

composer-require --dev Rector as described, then run

vendor/bin/rector process src --set phpexcel-to-phpspreadsheet

Which versions of PhpSpreadsheet and PHP are affected?

phpoffice/phpspreadsheet 1.15 with PHP 7.4.12 on Ubuntu 18 LTS

tomkyle avatar Nov 02 '20 11:11 tomkyle

Found a workaround – It seems to me Rector requires configuration by an PHP file. So passing s.th. like--set phpexcel-to-phpspreadsheet will not work any longer, and it takes two more steps:

$ composer require rector/rector --dev
$ vendor/bin/rector init

The init command will add a rector.php configuration file. Open that file in an editor and add the PHPEXCEL_TO_PHPSPREADSHEET ruleset to the $parameters. — Note: When you are about to migrate only the PhpOffice packages, you may want to disable the DEAD_CODE ruleset:

<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

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

    // Define what rule sets will be applied
    $parameters->set(Option::SETS, [
        // SetList::DEAD_CODE,
        SetList::PHPEXCEL_TO_PHPSPREADSHEET,
    ]);

    // get services (needed for register a single rule)
    // $services = $containerConfigurator->services();

    // register a single rule
    // $services->set(TypedPropertyRector::class);
};

Now assuming your project code is located in src/ directory, run Rector on it like this:

$ vendor/bin/rector process src/

After successful migration Rector can be removed:

$ composer remove rector/rector && rm rector.php

If this way is considered helpful and this receipt meets the intended usage, I'd happily supply a PR.

tomkyle avatar Nov 02 '20 14:11 tomkyle

I think that this should be a high priority for a pull request.

PhantomWatson avatar Dec 23 '20 22:12 PhantomWatson

This is still an issue.

Also Option::SETS is no longer supported. I tried $containerConfigurator->import(SetList::PHPEXCEL_TO_PHPSPREADSHEET); instead, without any luck

RdeWilde avatar Jul 07 '21 11:07 RdeWilde

This can help https://getrector.org/blog/2020/04/16/how-to-migrate-from-phpexcel-to-phpspreadsheet-with-rector-in-30-minutes

$containerConfigurator->import(SetList::PHPEXCEL_TO_PHPSPREADSHEET);

Abdul-Hafiz avatar Jul 15 '21 10:07 Abdul-Hafiz

Since version 0.12.6 published 28 days ago, this is now:

$containerConfigurator->import(\Rector\PHPOffice\Set\PHPOfficeSetList::PHPEXCEL_TO_PHPSPREADSHEET);

damien-thiesson avatar Jan 07 '22 00:01 damien-thiesson

Rector team revised the documentation via PR #2861 on May 31. Closing this issue.

oleibman avatar Sep 02 '22 18:09 oleibman

The example is still outdated.

Basically, one need to pull the rule sets from composer with composer require --dev rector/rector-phpoffice

From that point, you can effectively add something like $rectorConfig->import(\Rector\PHPOffice\Set\PHPOfficeSetList::PHPEXCEL_TO_PHPSPREADSHEET); in the rector.php file then run rector.

Please note that the aforementioned component requires PHP >= 8.1 so I had to install it beside my current project which doesn't currently support that version of PHP.

fredericgboutin-yapla avatar Dec 21 '22 19:12 fredericgboutin-yapla