Slide 1

Slide 1 text

© Perforce Software, Inc. Semi-Automated Refactoring and Upgrades with Rector International PHP Conference Anna Filina | November 2024

Slide 2

Slide 2 text

© Perforce Software, Inc. Automated Refactoring -array_push($items, $item); +$items[] = $item;

Slide 3

Slide 3 text

© Perforce Software, Inc. What is Rector What it does and doesn’t do How I upgrade How I leverage automated tools Agenda

Slide 4

Slide 4 text

© 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

Slide 5

Slide 5 text

© Perforce Software, Inc. Legacy PHP • 4M lines of code • As low as PHP 4 • register_globals • undefined vars • dead libraries • removed extensions

Slide 6

Slide 6 text

© Perforce Software, Inc.

Slide 7

Slide 7 text

© Perforce Software, Inc. What is Rector

Slide 8

Slide 8 text

© Perforce Software, Inc. Rector • CLI tool • Has out-of-the box rules • Written in PHP, for PHP

Slide 9

Slide 9 text

© Perforce Software, Inc. Example PHPSpreadsheet PHPExcel

Slide 10

Slide 10 text

© 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

Slide 11

Slide 11 text

© 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()

Slide 12

Slide 12 text

© Perforce Software, Inc. How Rector Works Static analysis

Slide 13

Slide 13 text

© 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

Slide 14

Slide 14 text

© Perforce Software, Inc. Types -function save() {} +function save(): void {}

Slide 15

Slide 15 text

© Perforce Software, Inc. More Types public function search(int $id) { $this->fetch($id); } -private function fetch($id) +private function fetch(int $id)

Slide 16

Slide 16 text

© Perforce Software, Inc. Frameworks - /** - * @Route("/path", name="action") - */ + #[Route(path: '/path', name: 'action')]

Slide 17

Slide 17 text

© 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.

Slide 18

Slide 18 text

© Perforce Software, Inc. What it Does and Doesn’t Do

Slide 19

Slide 19 text

© Perforce Software, Inc. Limitations $db = new Db();

Slide 20

Slide 20 text

© Perforce Software, Inc. PHP 4 Style Constructor class MyClass { - public function MyClass() + public function __construct() { } }

Slide 21

Slide 21 text

© 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()

Slide 22

Slide 22 text

© Perforce Software, Inc. PHPStorm’s Solution - $this->ParentClass() + ParentClass::__construct() https://www.zend.com/php-migration/function-name- restrictions/removed-php4-style-constructor

Slide 23

Slide 23 text

© Perforce Software, Inc. Use all available automated tools, not just one.

Slide 24

Slide 24 text

© Perforce Software, Inc. Don’t blindly rely on automation.

Slide 25

Slide 25 text

© Perforce Software, Inc. How I Upgrade

Slide 26

Slide 26 text

© Perforce Software, Inc. Support Period 7.4 5.3 8.3 5.3

Slide 27

Slide 27 text

© 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

Slide 28

Slide 28 text

© Perforce Software, Inc. Avoid Regressions Make changes Did behavior change? Write tests 5,000 changes 3,000 changes 7,000 changes

Slide 29

Slide 29 text

© Perforce Software, Inc. npm install cypress

Slide 30

Slide 30 text

© Perforce Software, Inc. npx cypress open

Slide 31

Slide 31 text

© Perforce Software, Inc. Branches Legacy Upgrade

Slide 32

Slide 32 text

© Perforce Software, Inc. Tests Execute on legacy & upgrade First test passes on upgrade Write tests Make changes

Slide 33

Slide 33 text

© Perforce Software, Inc. How I Leverage Automated Tools

Slide 34

Slide 34 text

© 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 -------------------------------------------------------------------------

Slide 35

Slide 35 text

© Perforce Software, Inc. Migration Guides

Slide 36

Slide 36 text

© Perforce Software, Inc. Mixed Types 1 > 'a' // 5.6: true // 8.3: false

Slide 37

Slide 37 text

© 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"

Slide 38

Slide 38 text

© Perforce Software, Inc. Custom Rector Rules Zend Framework 1 Mezzio

Slide 39

Slide 39 text

© Perforce Software, Inc. Custom Rector Rules Account\IndexHandler AccountController indexAction() viewAction() Zend Framework 1 Mezzio Account\ViewHandler

Slide 40

Slide 40 text

© Perforce Software, Inc. Thank You @afilina@phpc.social @afilina afilina@perforce.com