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

Semi-Automated Refactoring and Upgrades with Re...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Anna Filina
November 13, 2024

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

November 13, 2024

Transcript

40 slides
  1. © Perforce Software, Inc. Semi-Automated Refactoring and Upgrades with Rector

    International PHP Conference Anna Filina | November 2024
  2. © Perforce Software, Inc. What is Rector What it does

    and doesn’t do How I upgrade How I leverage automated tools Agenda
  3. © Perforce Software, Inc. Anna Filina • Coding since 1997

    • PHP, Java, C#, etc. • Zend Professional Services • I fix your legacy • I write your tests • I teach you all my tricks
  4. © Perforce Software, Inc. Legacy PHP • 4M lines of

    code • As low as PHP 4 • register_globals • undefined vars • dead libraries • removed extensions
  5. © Perforce Software, Inc. Rector • CLI tool • Has

    out-of-the box rules • Written in PHP, for PHP
  6. © Perforce Software, Inc. Steps $ 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
  7. © Perforce Software, Inc. Result -$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()
  8. © Perforce Software, Inc. How Rector Works Rules hook into

    relevant nodes Static analysis Fix is applied references to PHPExcel_Cell rename to PhpOffice\PhpSpreadsheet \Cell\Coordinate
  9. © Perforce Software, Inc. More Types public function search(int $id)

    { $this->fetch($id); } -private function fetch($id) +private function fetch(int $id)
  10. © Perforce Software, Inc. Frameworks - /** - * @Route("/path",

    name="action") - */ + #[Route(path: '/path', name: 'action')]
  11. © Perforce Software, Inc. Rector Rules • 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.
  12. © Perforce Software, Inc. PHP 4 Style Constructor class MyClass

    { - public function MyClass() + public function __construct() { } }
  13. © Perforce Software, Inc. Incorrect Solution class ChildClass extends ParentClass

    { - public function ChildClass() + public function __construct() { $this->ParentClass(); } } Uncaught Error: Call to undefined method ChildClass::ParentClass()
  14. © Perforce Software, Inc. PHPStorm’s Solution - $this->ParentClass() + ParentClass::__construct()

    https://www.zend.com/php-migration/function-name- restrictions/removed-php4-style-constructor
  15. © Perforce Software, Inc. Extended Support Period 7.4 5.3 8.3

    Community PHP 7.4: November 2022 ZendPHP 7.4: December 2026 Due date: December 2026
  16. © Perforce Software, Inc. Avoid Regressions Make changes Did behavior

    change? Write tests 5,000 changes 3,000 changes 7,000 changes
  17. © Perforce Software, Inc. Tests Execute on legacy & upgrade

    First test passes on upgrade Write tests Make changes
  18. © Perforce Software, Inc. PHPCompatibility FILE: /var/www/upgrade/my_account.php ------------------------------------------------------------------------ FOUND 1

    ERROR AND 1 WARNING AFFECTING 3 LINES ------------------------------------------------------------------------ 202 | ERROR | Function split() is deprecated since PHP 5.3 and removed since PHP 7.0; Use preg_split() instead -------------------------------------------------------------------------
  19. © Perforce Software, Inc. Undefined Variables 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"

More Decks by Anna Filina

Other Decks in Programming