How to Migrate From PHPExcel to PHPSpreadsheet with Rector in 30 minutes
PHPSpreadsheet is direct follower of PHPExcel with the same maintainers, just with dozens of BC breaks. There is the official migration tutorial that describes step by step 24 different changes that you need to do.
Only 1 of those changes - class renames - is automated with
preg_replace(). Regular expressions are not the best thing to use to modify code, rather contrary.
But there are also extra method calls:
Method call renames:
-$worksheet->setSharedStyle($sharedStyle, $range); +$worksheet->duplicateStyle($sharedStyle, $range);
Argument switch and extra method calls:
-$cell = $worksheet->setCellValue('A1', 'value', true); +$cell = $worksheet->getCell('A1')->setValue('value');
Method move to another class:
and so on.
Most people use PHPExcel in the past, and didn't touch the code ever since. If it works, why touch it, right?
That why for last 3 years the download rate decreased just very poorly - from ~25 000 daily downlaod to ~20 000 daily downloads:
We need Migration - Fast
We got into a project that used PHPExcel and wanted to switch to PHP 7.4 and Symfony 5. PHP upgrade and framework migration is half of the work. The other half are these small packages, that vendor locks your project to old PHP or another old dependency.
To get rid of old PHPExcel, we prepare a set
phpexcel-to-phpspreadsheet to help us.
It took 3 days to make and now has 230 lines of configuration and 17 rules.
How to Migrate?
composer require rector/rector --dev vendor/bin/rector process src --set phpexcel-to-phpspreadsheet
That's it! Rector just migrated your code from PHPExcel to PHPSpreadsheet.