SlideShare a Scribd company logo
Refactoring Legacy PHP
Junade Ali - @IcyApril
Resources
● Code Samples
○ https://github.com/IcyApril/phpasia-examples
○ git clone git@github.com:IcyApril/phpasia-examples.git
● Slides
○ https://www.slideshare.net/JunadeAli
● Free Book: “Object-Oriented PHP”
○ https://ju.je/free-book-phpasia
○ Link valid for attendees only
○ Please be considerate, don’t share/sell/upload
Human Error
Huamn Erorr:
Tehse wrods may look lkie nosnesne,
but yuo can raed tehm, cna't yuo?
Workshop: Refactoring Legacy PHP: The Complete Guide
Design Stamina Hypothesis - Martin Fowler
Clean Code
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
BeckDesignRules - Martin Fowler (www.martinfowler.com)
BeckDesignRules - Martin Fowler (www.martinfowler.com)
Automated Testing
Workshop: Refactoring Legacy PHP: The Complete Guide
RED
GREEN
GREEN
Refactor
Installing PHPUnit
composer require --dev phpunit/phpunit ^7
./vendor/bin/phpunit --version
phpunit.xml
Running PHPUnit
php ./vendor/bin/phpunit --configuration ./phpunit.xml
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Testing Legacy
● It’s hard to apply unit tests before refactoring
● Instead we can start automating things like GUI tests or API tests
● PHPUnit has support for Selenium WebDriver
● Means you can refactor in a reliable way
● With such tests, refactoring becomes easier
API Testing: Installing PHPUnit + Guzzle
composer require phpunit/phpunit
composer require guzzlehttp/guzzle
composer update
API Testing: Installing PHPUnit + Guzzle
composer require phpunit/phpunit
composer require guzzlehttp/guzzle
composer update
API Testing: Base Class
<?php
class UserAgentTest extends PHPUnit_Framework_TestCase
{
private $http;
public function setUp()
{
$this->http = new GuzzleHttpClient(['base_uri' => 'https://httpbin.org/']);
}
public function tearDown() {
$this->http = null;
}
}
API Testing: Testing HTTP GET Methods
public function testGet()
{
$response = $this->http->request('GET', 'user-agent');
$this->assertEquals(200, $response->getStatusCode());
$contentType = $response->getHeaders()["Content-Type"][0];
$this->assertEquals("application/json", $contentType);
$userAgent = json_decode($response->getBody())->{"user-agent"};
$this->assertRegexp('/Guzzle/', $userAgent);
}
API Testing: Testing HTTP PUT Methods
public function testPut()
{
$response = $this->http->request('PUT', 'user-agent', ['http_errors' => false]);
$this->assertEquals(405, $response->getStatusCode());
}
Workshop: Refactoring Legacy PHP: The Complete Guide
PHPMD + PHP Code Sniffer
Installing PHPMD + PHPCS
composer require --dev phpmd/phpmd
composer require --dev friendsofphp/php-cs-fixer
Makefile
.php_cs
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
“Else is never necessary”
“The method validateEmail uses an else expression. Else is never necessary and
you can simplify the code to work without else.”
Design-by-Contract
Installing PhpDeal
composer require php-deal/framework
Recommended PHPStorm plugin: Go! AOP Framework
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Continuous Integration
Extreme Programming Rules
http://www.extremeprogramming.org/rules.html
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Makefile
.travis.yml
Workshop: Refactoring Legacy PHP: The Complete Guide
SOLID Principles
Single Responsibility Principle
Robert C. Martin expresses the principle as, "A class should
have only one reason to change."
Open/Closed Principle
The Open/Closed Principle states “software entities (classes, modules, functions, etc.) should be open for
extension, but closed for modification”.We should be able to extended a given piece of software without
needing to modify it’s source code.
● “Open for Extension” - we can make the class behave in new ways as the requirements for what the
class needs to do evolves
● “Closed for Modification” - you cannot change the source code of the class itself, it is considered
inviolable
Workshop: Refactoring Legacy PHP: The Complete Guide
Liscov Substitution Principle
In it’s simplified form: “Functions that use pointers or references to base classes
must be able to use objects of derived classes without knowing it”.
Any class should be substitutable for it’s base class or interface. Any of the
sub-classes of Staff should be substitutable for the Staff class.
Interface Segregation Principle
The Interface Segregation Principle states that no client should be forced to
depend on methods it does not use.
This principle essentially outlines that we should favour small, specific interfaces
over large bloated ones. All classes should only have to implement the methods
they need - this helps keep our system decoupled.
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Dependency-Inversion Principle
The Dependency-Inversion Principle is stated in two parts:
● High-level modules should not depend on low-level modules. Both should
depend on abstractions.
● Abstractions should not depend on details. Details should depend on
abstractions.
Workshop: Refactoring Legacy PHP: The Complete Guide
Workshop: Refactoring Legacy PHP: The Complete Guide
Delivering Value
- “Refactor Mercilessly” - Extreme Programming
http://www.extremeprogramming.org/rules/refactor.html
Refactor mercilessly to keep the design simple as you go
and to avoid needless clutter and complexity. Keep your
code clean and concise so it is easier to understand,
modify, and extend. Make sure everything is expressed
once and only once. In the end it takes less time to
produce a system that is well groomed.
Kanban System
toyota-global.com
Five Focusing Steps - Theory of Constraints
https://www.leanproduction.com/theory-of-constraints.html
Workshop: Refactoring Legacy PHP: The Complete Guide
Software Engineering Workflow
● Enshrine all your requirements in automated acceptance tests with CI builds
● “Boy Scout Principle” - Leave the codebase cleaner everytime you push
● Use short iterations to prevent task switching
Thank you!
@IcyApril

More Related Content

PDF
The road to php 7.1
PDF
PHPUnit with Magento
PDF
Civilized Git Process
PDF
Integrating react in django while staying sane and happy
PDF
BDD in PHP - Behat
PDF
How QCLean Works? Introduction to Browser Extensions
PPTX
That worked before
PPT
Impact Of A Cms Framework Change - Holland Open Netherlands
The road to php 7.1
PHPUnit with Magento
Civilized Git Process
Integrating react in django while staying sane and happy
BDD in PHP - Behat
How QCLean Works? Introduction to Browser Extensions
That worked before
Impact Of A Cms Framework Change - Holland Open Netherlands

What's hot (20)

ODP
Xdebug for Beginners
PDF
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
PDF
Maven beyond hello_world
PDF
Plugin Development for Beginners
PPTX
Open NTF OpenSource is collaboration at its best and matters
PDF
Improving your code design using Java
ODP
BDD with Behat
PPTX
Behat - human-readable automated testing
PDF
Андрій Юн — Drupal contributor HOWTO
PDF
Full-Stack Development
ODP
Lighning Talk: PHP build process
PPTX
Automation testing with Drupal 8
ODP
OpenNTF Essentials
PDF
Behaviour Testing and Continuous Integration with Drupal
PPTX
Autotools, Design Patterns and more
PDF
Android Modularization
PDF
Foundations of Zend Framework
PDF
Codemotion Amsterdam 2016 - The DevOps Disaster
PDF
Testing Your Code as Part of an Industrial Grade Workflow
PDF
Docker for Artisans
Xdebug for Beginners
Do You Git Your Code? Follow Simplified Gitflow Branching Model to Improve Pr...
Maven beyond hello_world
Plugin Development for Beginners
Open NTF OpenSource is collaboration at its best and matters
Improving your code design using Java
BDD with Behat
Behat - human-readable automated testing
Андрій Юн — Drupal contributor HOWTO
Full-Stack Development
Lighning Talk: PHP build process
Automation testing with Drupal 8
OpenNTF Essentials
Behaviour Testing and Continuous Integration with Drupal
Autotools, Design Patterns and more
Android Modularization
Foundations of Zend Framework
Codemotion Amsterdam 2016 - The DevOps Disaster
Testing Your Code as Part of an Industrial Grade Workflow
Docker for Artisans
Ad

Similar to Workshop: Refactoring Legacy PHP: The Complete Guide (20)

PDF
So You Just Inherited a $Legacy Application… NomadPHP July 2016
PDF
So You Just Inherited a $Legacy Application...
PDF
Test driven development - Zombie proof your code
PPTX
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
PDF
Refactoring Legacy Code
PDF
Intro to PHP Testing
PPTX
Improving The Quality of Existing Software
PDF
Reduce Reuse Refactor
PPTX
Refactoring legacy code driven by tests - ITA
PDF
Clean Code V2
PPTX
From Good to SOLID: How to become a better PHP developer
PDF
Throwing Laravel into your Legacy App™
ODP
PDF
TDD with PhpSpec - Lone Star PHP 2016
PPTX
Refactoring Applications using SOLID Principles
PPTX
Improving the Design of Existing Software
PPTX
Improving the Quality of Existing Software
PPTX
Soild principles
PDF
Dutch PHP Conference 2013: Distilled
PPTX
Continuous feature-development
So You Just Inherited a $Legacy Application… NomadPHP July 2016
So You Just Inherited a $Legacy Application...
Test driven development - Zombie proof your code
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Legacy Code
Intro to PHP Testing
Improving The Quality of Existing Software
Reduce Reuse Refactor
Refactoring legacy code driven by tests - ITA
Clean Code V2
From Good to SOLID: How to become a better PHP developer
Throwing Laravel into your Legacy App™
TDD with PhpSpec - Lone Star PHP 2016
Refactoring Applications using SOLID Principles
Improving the Design of Existing Software
Improving the Quality of Existing Software
Soild principles
Dutch PHP Conference 2013: Distilled
Continuous feature-development
Ad

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Nekopoi APK 2025 free lastest update
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
System and Network Administration Chapter 2
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
history of c programming in notes for students .pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
medical staffing services at VALiNTRY
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
Understanding Forklifts - TECH EHS Solution
Odoo POS Development Services by CandidRoot Solutions
Nekopoi APK 2025 free lastest update
2025 Textile ERP Trends: SAP, Odoo & Oracle
Which alternative to Crystal Reports is best for small or large businesses.pdf
Softaken Excel to vCard Converter Software.pdf
PTS Company Brochure 2025 (1).pdf.......
Wondershare Filmora 15 Crack With Activation Key [2025
ISO 45001 Occupational Health and Safety Management System
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
System and Network Administration Chapter 2
Design an Analysis of Algorithms II-SECS-1021-03
history of c programming in notes for students .pptx
How to Choose the Right IT Partner for Your Business in Malaysia
medical staffing services at VALiNTRY
How to Migrate SBCGlobal Email to Yahoo Easily
System and Network Administraation Chapter 3

Workshop: Refactoring Legacy PHP: The Complete Guide

  • 2. Resources ● Code Samples ○ https://github.com/IcyApril/phpasia-examples ○ git clone git@github.com:IcyApril/phpasia-examples.git ● Slides ○ https://www.slideshare.net/JunadeAli ● Free Book: “Object-Oriented PHP” ○ https://ju.je/free-book-phpasia ○ Link valid for attendees only ○ Please be considerate, don’t share/sell/upload
  • 4. Huamn Erorr: Tehse wrods may look lkie nosnesne, but yuo can raed tehm, cna't yuo?
  • 6. Design Stamina Hypothesis - Martin Fowler
  • 14. BeckDesignRules - Martin Fowler (www.martinfowler.com)
  • 15. BeckDesignRules - Martin Fowler (www.martinfowler.com)
  • 19. Installing PHPUnit composer require --dev phpunit/phpunit ^7 ./vendor/bin/phpunit --version
  • 21. Running PHPUnit php ./vendor/bin/phpunit --configuration ./phpunit.xml
  • 26. Testing Legacy ● It’s hard to apply unit tests before refactoring ● Instead we can start automating things like GUI tests or API tests ● PHPUnit has support for Selenium WebDriver ● Means you can refactor in a reliable way ● With such tests, refactoring becomes easier
  • 27. API Testing: Installing PHPUnit + Guzzle composer require phpunit/phpunit composer require guzzlehttp/guzzle composer update
  • 28. API Testing: Installing PHPUnit + Guzzle composer require phpunit/phpunit composer require guzzlehttp/guzzle composer update
  • 29. API Testing: Base Class <?php class UserAgentTest extends PHPUnit_Framework_TestCase { private $http; public function setUp() { $this->http = new GuzzleHttpClient(['base_uri' => 'https://httpbin.org/']); } public function tearDown() { $this->http = null; } }
  • 30. API Testing: Testing HTTP GET Methods public function testGet() { $response = $this->http->request('GET', 'user-agent'); $this->assertEquals(200, $response->getStatusCode()); $contentType = $response->getHeaders()["Content-Type"][0]; $this->assertEquals("application/json", $contentType); $userAgent = json_decode($response->getBody())->{"user-agent"}; $this->assertRegexp('/Guzzle/', $userAgent); }
  • 31. API Testing: Testing HTTP PUT Methods public function testPut() { $response = $this->http->request('PUT', 'user-agent', ['http_errors' => false]); $this->assertEquals(405, $response->getStatusCode()); }
  • 33. PHPMD + PHP Code Sniffer
  • 34. Installing PHPMD + PHPCS composer require --dev phpmd/phpmd composer require --dev friendsofphp/php-cs-fixer
  • 40. “Else is never necessary” “The method validateEmail uses an else expression. Else is never necessary and you can simplify the code to work without else.”
  • 42. Installing PhpDeal composer require php-deal/framework Recommended PHPStorm plugin: Go! AOP Framework
  • 56. Single Responsibility Principle Robert C. Martin expresses the principle as, "A class should have only one reason to change."
  • 57. Open/Closed Principle The Open/Closed Principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.We should be able to extended a given piece of software without needing to modify it’s source code. ● “Open for Extension” - we can make the class behave in new ways as the requirements for what the class needs to do evolves ● “Closed for Modification” - you cannot change the source code of the class itself, it is considered inviolable
  • 59. Liscov Substitution Principle In it’s simplified form: “Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it”. Any class should be substitutable for it’s base class or interface. Any of the sub-classes of Staff should be substitutable for the Staff class.
  • 60. Interface Segregation Principle The Interface Segregation Principle states that no client should be forced to depend on methods it does not use. This principle essentially outlines that we should favour small, specific interfaces over large bloated ones. All classes should only have to implement the methods they need - this helps keep our system decoupled.
  • 63. Dependency-Inversion Principle The Dependency-Inversion Principle is stated in two parts: ● High-level modules should not depend on low-level modules. Both should depend on abstractions. ● Abstractions should not depend on details. Details should depend on abstractions.
  • 67. - “Refactor Mercilessly” - Extreme Programming http://www.extremeprogramming.org/rules/refactor.html Refactor mercilessly to keep the design simple as you go and to avoid needless clutter and complexity. Keep your code clean and concise so it is easier to understand, modify, and extend. Make sure everything is expressed once and only once. In the end it takes less time to produce a system that is well groomed.
  • 69. Five Focusing Steps - Theory of Constraints https://www.leanproduction.com/theory-of-constraints.html
  • 71. Software Engineering Workflow ● Enshrine all your requirements in automated acceptance tests with CI builds ● “Boy Scout Principle” - Leave the codebase cleaner everytime you push ● Use short iterations to prevent task switching