Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Semi-Automated Refactoring and Upgrades with Re...

Semi-Automated Refactoring and Upgrades with Rector

Rector is a CLI tool that assists with upgrading and refactoring of PHP projects. Do you seek to save months of mindless work and frustration on your next PHP upgrade? After a brief introduction to Rector using practical examples, I will show you what it can and can't do for you. I will also give real-world tips on PHP upgrades and how you can best leverage Rector and other automated tools.

Anna Filina

February 23, 2024
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. I Stand With Ukraine What is Rector What is does,

    what it doesn’t do How I upgrade How I leverage automated tools
  2. I Stand With Ukraine Anna Filina • Coding since 1997.

    • PHP, Java, C#, etc. • Legacy archaeology. • Test automation. • Talks and workshops. • YouTube videos. • Filina Consulting.
  3. I Stand With Ukraine • 4M lines of code •

    PHP 5 or lower • register_globals • undefined vars • dead libraries • removed extensions
  4. I Stand With Ukraine • CLI tool • Has out-of-the

    box rules • Written in PHP, for PHP
  5. I Stand With Ukraine Source: https://getrector.com/blog/how-to-migrate-from- phpexcel-to-phpspreadsheet-with-rector-in-30-minutes $ composer require

    rector/rector-phpoffice --dev $ vendor/bin/rector init return function (RectorConfig $rectorConfig): void { $rectorConfig->import( PHPOfficeSetList::PHPEXCEL_TO_PHPSPREADSHEET ); }; $ vendor/bin/rector process src
  6. I Stand With Ukraine -$worksheet->getDefaultStyle(); +$worksheet->getParent()->getDefaultStyle(); -$worksheet->setSharedStyle($sharedStyle, $range); +$worksheet->duplicateStyle($sharedStyle, $range);

    -$cell = $worksheet->setCellValue('A1', 'value', true); +$cell = $worksheet->getCell('A1')->setValue('value’); -PHPExcel_Cell::absoluteCoordinate() +PhpOffice\PhpSpreadsheet\Cell\Coordinate::absoluteCoordinate()
  7. I Stand With Ukraine Static Analysis Rules hook into relevant

    nodes references to PHPExcel_Cell class Fix is applied rename references to PhpOffice\PhpSpreadsheet \Cell\Coordinate
  8. I Stand With Ukraine public function search(int $id) { $this->fetch($id);

    } -private function fetch($id) +private function fetch(int $id) { }
  9. I Stand With Ukraine - /** - * @Route("/path", name="action")

    - */ + #[Route(path: '/path', name: 'action')]
  10. I Stand With Ukraine • Framework rules: Symfony • Library

    rules: Doctrine, PHPUnit, PHPSpreadsheet • Upgrade rules: PHP 5.3, 5.4, etc. • Configurable rules: add arg to method signature and its references • Can write your own custom rules.
  11. I Stand With Ukraine class SomeClass { - public function

    SomeClass() + public function __construct() { } }
  12. I Stand With Ukraine class ChildClass extends ParentClass { -

    public function SomeClass() + public function __construct() { $this->ParentClass(); } } Fatal error: Uncaught Error: Call to undefined method ChildClass::ParentClass()
  13. I Stand With Ukraine 5.3 ~ 3 months 7.4 7.4

    ~ 7 months 8.3 Running a supported version
  14. I Stand With Ukraine Make changes Did behavior change? Write

    tests 5,000 changes 3,000 changes 7,000 changes
  15. I Stand With Ukraine Make changes Write tests Execute on

    legacy & upgrade First test passes on upgrade
  16. I Stand With Ukraine PHPCompatibility FILE: /var/www/upgrade/my_account.php ------------------------------------------------------------------------------------------------------------- FOUND 2

    ERRORS AND 1 WARNING AFFECTING 3 LINES ------------------------------------------------------------------------------------------------------------- 33 | WARNING | INI directive 'track_errors' is deprecated since PHP 7.2 202 | ERROR | Function split() is deprecated since PHP 5.3 and removed since PHP 7.0; Use preg_split() instead 208 | WARNING | The variable '$php_errormsg' is deprecated since PHP 7.2; Use error_get_last() instead -------------------------------------------------------------------------------------------------------------
  17. I Stand With Ukraine ini_set('date.timezone', 'UTC'); date('Y-m-d H:i', $timestamp); //

    5.6: string(16) "2024-01-01 12:00" // 8.3: string(16) "1970-01-01 00:00"