Slide 1

Slide 1 text

Refactoring legacy PHP app Nemanja Marić maki10

Slide 2

Slide 2 text

About me ● PHP Developer at Orange Hill Development ● Member of PHP Serbia Community ● Co-organizer Laravel Serbia Meetup’s and PHP Serbia Meetup’s ● Working with PHP since 2014 ● In Laravel world from 2016 ● Open source contributor ● Contributing to the Laravel Framework ● And most !important: Husband and father of two little angels

Slide 3

Slide 3 text

Interesting part Share your knowledge with others to get some knowledge.

Slide 4

Slide 4 text

Over the years

Slide 5

Slide 5 text

Covid years

Slide 6

Slide 6 text

What will be refactored?

Slide 7

Slide 7 text

It’s an old Laravel project started on Laravel 5.4 and PHP 5.6, upgraded to Laravel 6 (migrated 3 times) and PHP 7.3 Our goal is to run it on Laravel 9 (Up 3 major version) and PHP 8.1

Slide 8

Slide 8 text

Which tools we will use?

Slide 9

Slide 9 text

Rector 1. Instant Upgrades Rector now supports upgrades from PHP 5.3 to 8.0 and major open-source projects like Symfony, PHPUnit, Nette, Laravel, CakePHP, Doctrine, PHPOffice and TYPO3 out of the box. Do you want to be constantly on the latest PHP/framework version without effort? Use Rector to handle instant upgrades for you. 2. Automated Refactoring Do you have code quality you need, but struggle to keep it with new developers in your team? Do you want to see smart code-reviews even when every senior developers sleeps? Add Rector to your CI and let it continuously refactor your code and keep the code quality high.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Simple rule

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

Ok, stop.. Do you trust automated tools to change your code?

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Ok, but what about a big ball of mud?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

We don’t have time to refactor whole application, so let’s start one by one

Slide 21

Slide 21 text

Big complexity Poorly designed code is often complex and architecturally all off. Like it or not, that is only and only our mistake as developers. This is something that no tool can help you with, so let's dig in.

Slide 22

Slide 22 text

With the power of a “phpinsights” we can spot issues, let’s warm up

Slide 23

Slide 23 text

[Code] Useless parentheses: Line:149

Slide 24

Slide 24 text

[Code] Nullable type for null default value: Line:82

Slide 25

Slide 25 text

Cyclomatic Complexity

Slide 26

Slide 26 text

Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

Slide 27

Slide 27 text

Lower complexity = less issues [Complexity] Having `classes` with more than 5 cyclomatic complexity is prohibited - Consider refactoring: TransformationController.php: 17 cyclomatic complexity

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Strategy pattern

Slide 33

Slide 33 text

Rector rule [email protected] | Strategy pattern

Slide 34

Slide 34 text

Model Transformer

Slide 35

Slide 35 text

Model Transformer | Can be better?

Slide 36

Slide 36 text

Yes of course | Can be better?

Slide 37

Slide 37 text

How about to concat arrays and search both params to see if exist and to get the first one?!?

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Let’s see whole refactor score

Slide 40

Slide 40 text

[COMPLEXITY] 60.6 pts with average of 1.82 from 17 cyclomatic complexity

Slide 41

Slide 41 text

What is left to do with this?

Slide 42

Slide 42 text

The new colleague will easily join when refactor is good

Slide 43

Slide 43 text

Change from instanceof to class_implements

Slide 44

Slide 44 text

[COMPLEXITY] 60.6 pts with average of 1.45 from 17 cyclomatic complexity

Slide 45

Slide 45 text

Ok, and last, how to stop new shitty code, and set some rules for it?

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Questions? Slides: https://speakerdeck.com/maki10 Twitter: https://twitter.com/NemanjaMaki10 https://github.com/rectorphp/rector https://github.com/phpstan/phpstan https://github.com/nunomaduro/phpinsights