SlideShare a Scribd company logo
@asgrim
Best practices for crafting high
quality PHP apps
James Titcumb
PHP South Africa 2018
Please clone & composer install:
https://github.com/asgrim/quality-tutorial
$ whoami
James Titcumb
www.jamestitcumb.com
www.roave.com
@asgrim
Please clone & composer install:
https://github.com/asgrim/quality-tutorial
@asgrim
@asgrim
What is “quality”?
@asgrim
photo: Rob Allen https://flic.kr/p/qmGpsq
@asgrim
@asgrim
@asgrim
Quality.
@asgrim
"The best code is no code at all”
-- Jeff Atwood
source: https://blog.codinghorror.com/the-best-code-is-no-code-at-all/
@asgrim
Trade-offs.
@asgrim
Prototyping & short-lived apps/sites
@asgrim
Products
Long-lived projects. Open source software.
@asgrim
What is quality in applications?
@asgrim
What about time/cost…?
@asgrim
“A freelancer at $25 an hour for 100 hours still
costs more than a freelancer at $150 an hour that
takes 10 hours to do the same task”
-- Brandon Savage
source: http://www.brandonsavage.net/earning-more-money-as-a-php-freelancer/
@asgrim
Get an expert in.
@asgrim
Complexity
@asgrim
Processes
@asgrim
This talk...
● Planning
● Development
● Testing
● Continuous integration
● Code reviews
● Deployments
@asgrim
Planning
@asgrim
Planning is communication
@asgrim
Use business terminology
@asgrim
Explore and discover
@asgrim
Build a model of the business
@asgrim
@asgrim
@asgrim
The model must be fluid.
@asgrim
“Estimates”
@asgrim
Development
@asgrim
Care about code
@asgrim
“There are only two hard things in Computer
Science: cache invalidation and naming things.”
-- Phil Karlton
source: https://martinfowler.com/bliki/TwoHardThings.html
@asgrim
SimpleBeanFactoryAwareAspectInstanceFactory
@asgrim
Loader
@asgrim
Describe intent
@asgrim
“Give awkward names to awkward concepts”
-- Eric Evans
source: https://skillsmatter.com/conferences/8231-ddd-exchange-2017
@asgrim
SOLID
@asgrim
KISS
@asgrim
Object Calisthenics
@asgrim
Avoid early abstraction
@asgrim
“Code for your use-case,
not for your re-use-case”
-- Marco Pivetta
source: https://ocramius.github.io/extremely-defensive-php/#/39
@asgrim
Care about your API
@asgrim
A public method is like a child:
once you've written it,
you are going to maintain it
for the rest of its life!
-- Stefan Priebsch
@asgrim
Strict type declarations.
Use declare(strict_types=1); by default
@asgrim
Immutable value objects
declare(strict_types=1);
use AssertAssertion; // beberlei/assert library !
final class PostalCode
{
private const VALIDATION_EXPRESSION = '(GIR 0AA)|((([A-Z-[QVX]][0-9][0-...
/** @var string */
private $value;
public function __construct(string $value) {
$this->assertValidPostalCode($value);
$this->value = $value;
}
private function assertValidPostalCode(string $value) : string {
Assertion::regex($value, self::VALIDATION_EXPRESSION);
}
public function __toString() : string {
return $this->value;
}
}
@asgrim
Value objects are valid!
declare(strict_types=1);
final class Thing {
public function assignPostalCode(PostalCode $postalCode) : void
{
// ... we can trust $postalCode is a valid postal code
}
}
// 12345 is not valid - EXCEPTION!
$myThing->assignPostalCode(new PostalCode('12345'));
// assignPostalCode is happy
$myThing->assignPostalCode(new PostalCode('PO1 1AA'));
// With STRICT types, this will also FAIL
$myThing->assignPostalCode(new PostalCode(12345));
@asgrim
Testing
@asgrim
Testing is NOT a separate line item
@asgrim
Testing should be an assumption
@asgrim
You’re already testing.
@asgrim
Still need convincing?
@asgrim
How?
@asgrim
Reduce complexity
@asgrim
Reduce complexity
public function process(Stuff a, string b, int c) : MoreStuff
{
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
@asgrim
Reduce complexity
public function meaningfulThing(Stuff a, string b, int c) : More
{
// call nicer, meaningful methods below
}
private function throwStuffIntoFire(Stuff a) : Fire
{
// smaller, meaningful chunk of code
}
private function combineStringWithFire(Fire a, string b) : string
{
// simple, lovely code!
}
private function postFlowersToSpain(int c) : void
@asgrim
More complexity = more tests
@asgrim
Test coverage
@asgrim
Line coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
@asgrim
Line coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
@asgrim
Branch coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
@asgrim
Branch coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
@asgrim
Branch coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
foo(false, false); // NEW TEST!!!
@asgrim
Branch coverage
<?php
function foo(bool $a, bool $b) {
if ($a) {
echo "A";
}
if ($b) {
echo "B";
}
}
// generate coverage using calls:
foo(true, false);
foo(false, true);
foo(false, false); // NEW TEST!!!
foo(true, true); // NEW TEST!!!
@asgrim
Prevent coverage leaking
@asgrim
Prevent coverage leaking
<?php
namespace Foo;
/**
* @covers FooBar
*/
final class BarTest extends TestCase
{
// write some tests!
}
@asgrim
Are the tests testing?
@asgrim
Example of a test not testing…
public function testPurchaseTickets()
{
$event = Event::create(uniq('event', true)
$customer = Customer::create(uniq('name', true));
$shop->purchaseTicket(1, $event, [$customer]);
}
@asgrim
@asgrim
Assert you’re asserting
public function testPurchaseTickets()
{
$event = Event::create(uniq('event', true)
$customer = Customer::create(uniq('name', true));
$receipt = $shop->purchaseTicket(1, $event, [$customer]);
self::assertSame($event, $receipt->event());
$customersInReceipt = $receipt->customers();
self::assertCount(1, $customersInReceipt);
self::assertContains($customer, $customersInReceipt);
}
@asgrim
@asgrim
Test the tests are testing!
@asgrim
Mutation testing
function add(int $a, int $b) : int {
return $a + $b;
}
function testAdd() {
$result = add(2, 3);
// self::assertSame(5, $result);
}
@asgrim
Mutation testing
function add(int $a, int $b) : int {
return $a - $b;
}
function testAdd() {
$result = add(2, 3);
// self::assertSame(5, $result);
}
@asgrim
Mutation testing
function add(int $a, int $b) : int {
return $a - $b;
}
function testAdd() {
$result = add(2, 3);
self::assertSame(5, $result);
// /// test will now fail with mutation
}
@asgrim
What about other tests?
@asgrim
Integration tests
@asgrim
Behaviour tests
@asgrim
BAD! Do not do this.
Feature: Ability to print my boarding pass
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked
And I have checked in
When I visit the home page
And I click the ".manage-booking" button
And I enter "CJM23L" in the ".bref-ipt-fld" field
And I click the "Find Booking" button
Then I should see the ".booking-ref.print" button
When I click the ".booking-ref.print" button
Then I should see the print dialogue
@asgrim
Better Behaviour test
Feature: Ability to print my boarding pass
Policies:
- Boarding passes are only available when already checked in
- If customer cannot print boarding pass, they can collect at
The airport
- more business rules etc...
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked for LHR-MIA on 24th May
And I have previously checked in for the flight
When I display my booking reference "CJM23L"
Then I should be able to print my boarding pass
And the boarding pass should display flight details correctly
@asgrim
Why is this important?
@asgrim
Automate these tests
@asgrim
Automated tests
Application (and UI, API, etc.)
Domain / Business Logic
Infrastructure (DBAL, APIs, etc.)
@asgrim
Automated tests
Application (and UI, API, etc.)
Domain / Business Logic
Infrastructure (DBAL, APIs, etc.)
@asgrim
Testing at domain layer
Application (UI, API, etc.)
Domain / Business Logic
Infrastructure (DB, APIs, etc.)
// Testing via the UI
public function iDisplayMyBookingReference(string $reference)
{
$page = $this->getSession()->getPage();
$page->click(".manage-booking");
$page->findField(".bref-ipt-fld")->setValue($reference);
$page->click("Find Booking");
}
// Using the domain layer directly in tests
public function iDisplayMyBookingReference(string $reference)
{
$this->booking = $this->retrieveBooking($reference);
}
@asgrim
Some UI testing is okay!!!
Feature: Ability to print my boarding pass
Policies:
- Boarding passes are only available when already checked in
- If customer cannot print boarding pass, they can collect at
The airport
- more business rules etc...
@ui
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked for LHR-OTP on 24th May
And I have previously checked in for the flight
When I display my booking reference "CJM23L"
Then I should be able to print my boarding pass
And the boarding pass should display flight details correctly
@asgrim
Automate all the things!
@asgrim
Continuous Integration
@asgrim
Tests cost money to run
(manually)
@asgrim
Travis-CI.com
@asgrim
Jenkins
@asgrim
What to automate?
@asgrim
The goal?
@asgrim
Code reviews
@asgrim
What to look for in code review?
@asgrim
What to look for in code review?
Code style.
@asgrim
What to look for in code review?
Small, atomic changes.
@asgrim
What to look for in code review?
Composer versions.
@asgrim
What to look for in code review?
Structure & good practices.
@asgrim
What to look for in code review?
Tests.
@asgrim
What to look for in code review?
Documentation.
@asgrim
What to look for in code review?
Security.
@asgrim
What to look for in code review?
Insight.
@asgrim
It takes practice.
@asgrim
Deployments
@asgrim
Automate deployments!
@asgrim
One-click deployments
@asgrim
“Move fast and break things”
-- a stupid Facebook mantra
@asgrim
“Move fast and break things with stable infra”
-- Facebook mantra since 2014
@asgrim
Continuous Delivery & Deployment
@asgrim
Better quality
@asgrim
Better quality = Higher confidence
@asgrim
Better quality = Higher confidence = Happy customers
Any questions?
James Titcumb
@asgrim
@asgrim
Resources
● Event Storming - Alberto Brandolini
○ http://ziobrando.blogspot.co.uk/2013/11/introducing-event-storming.html
● “Good Design is Imperfect Design” - Eric Evans
○ https://skillsmatter.com/skillscasts/9171-good-design-is-imperfect-design
● “Writing code that lasts” - Rafael Dohms
○ https://www.youtube.com/watch?v=I0y5jU61pS4
● Object Calisthenics - William Durand
○ https://williamdurand.fr/2013/06/03/object-calisthenics/
● “Driving Design through Examples” - Ciaran McNulty
○ https://www.youtube.com/watch?v=83GbyDpJDI4
● Code Review Checklist
○ https://github.com/asgrim/code-review-checklist/blob/master/index.md

More Related Content

PDF
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
PDF
Best practices for crafting high quality PHP apps - PHP UK 2019
PDF
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
PDF
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
PDF
Crafting Quality PHP Applications (PHPkonf 2018)
PDF
Crafting Quality PHP Applications (PHP Benelux 2018)
PDF
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
PDF
Crafting Quality PHP Applications (ConFoo YVR 2017)
Best practices for crafting high quality PHP apps (ScotlandPHP 2018)
Best practices for crafting high quality PHP apps - PHP UK 2019
Best practices for crafting high quality PHP apps (PHP Yorkshire 2018)
Crafting Quality PHP Applications: an overview (PHPSW March 2018)
Crafting Quality PHP Applications (PHPkonf 2018)
Crafting Quality PHP Applications (PHP Benelux 2018)
Crafting Quality PHP Applications (Bucharest Tech Week 2017)
Crafting Quality PHP Applications (ConFoo YVR 2017)

What's hot (20)

PDF
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
PDF
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
PDF
Best practices for crafting high quality PHP apps (php[world] 2019)
PDF
Best practices for crafting high quality PHP apps (Bulgaria 2019)
PDF
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
PDF
Unbreakable Domain Models PHPUK 2014 London
ODP
Non Conventional Android Programming En
PDF
Moving away from legacy code with BDD
PDF
TDD with PhpSpec - Lone Star PHP 2016
PDF
"Managing API Complexity". Matthew Flaming, Temboo
PDF
Beyond MVC: from Model to Domain
ODP
Scti 2011 minicurso jquery
PDF
TDD with PhpSpec
PPT
JavaScript Needn't Hurt!
PPT
PDF
Driving Design with PhpSpec
PPTX
JavaScript, Beyond the Curly Braces
PDF
Oop in java script
PDF
[Practical] Functional Programming in Rails
PDF
Deliver Business Value Faster with AWS Step Functions
Dip Your Toes in the Sea of Security (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Best practices for crafting high quality PHP apps (php[world] 2019)
Best practices for crafting high quality PHP apps (Bulgaria 2019)
Crafting Quality PHP Applications (PHP Joburg Oct 2019)
Unbreakable Domain Models PHPUK 2014 London
Non Conventional Android Programming En
Moving away from legacy code with BDD
TDD with PhpSpec - Lone Star PHP 2016
"Managing API Complexity". Matthew Flaming, Temboo
Beyond MVC: from Model to Domain
Scti 2011 minicurso jquery
TDD with PhpSpec
JavaScript Needn't Hurt!
Driving Design with PhpSpec
JavaScript, Beyond the Curly Braces
Oop in java script
[Practical] Functional Programming in Rails
Deliver Business Value Faster with AWS Step Functions
Ad

Similar to Best practices for crafting high quality PHP apps (PHP South Africa 2018) (20)

PDF
Your code are my tests
PDF
Finding the Right Testing Tool for the Job
PDF
Php tests tips
PDF
Dutch PHP Conference 2013: Distilled
PDF
Save time by applying clean code principles
PDF
Testing untestable code - IPC12
PDF
Testing untestable code - phpday
PDF
Testing untestable code - phpconpl11
PDF
Test and API-driven development of CakePHP Behaviors
PDF
Testing untestable code - ConFoo13
PDF
Narrow Down What to Test
PDF
Living the Best Life on a Legacy Project (phpday 2022).pdf
PDF
Property Based Testing in PHP
ODP
Modernising Legacy Code
PDF
Testing untestable Code - PFCongres 2010
PPT
Unit testing
PDF
Symfony (Unit, Functional) Testing.
PPTX
php unconference Europa: Clean code - Stop wasting my time
PDF
Testing untestable code - DPC10
PDF
Software Quality and Test Strategies for Ruby and Rails Applications
Your code are my tests
Finding the Right Testing Tool for the Job
Php tests tips
Dutch PHP Conference 2013: Distilled
Save time by applying clean code principles
Testing untestable code - IPC12
Testing untestable code - phpday
Testing untestable code - phpconpl11
Test and API-driven development of CakePHP Behaviors
Testing untestable code - ConFoo13
Narrow Down What to Test
Living the Best Life on a Legacy Project (phpday 2022).pdf
Property Based Testing in PHP
Modernising Legacy Code
Testing untestable Code - PFCongres 2010
Unit testing
Symfony (Unit, Functional) Testing.
php unconference Europa: Clean code - Stop wasting my time
Testing untestable code - DPC10
Software Quality and Test Strategies for Ruby and Rails Applications
Ad

More from James Titcumb (17)

PDF
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
PDF
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
PDF
Climbing the Abstract Syntax Tree (php[world] 2019)
PDF
Climbing the Abstract Syntax Tree (PHP Russia 2019)
PDF
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
PDF
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
PDF
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
PDF
Climbing the Abstract Syntax Tree (PHP UK 2018)
PDF
Climbing the Abstract Syntax Tree (Forum PHP 2017)
PDF
Dip Your Toes in the Sea of Security (IPC Fall 2017)
PDF
Climbing the Abstract Syntax Tree (IPC Fall 2017)
PDF
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
PDF
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
PDF
Get Started with RabbitMQ (CoderCruise 2017)
PDF
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Tips for Tackling a Legacy Codebase (ScotlandPHP 2021)
Climbing the Abstract Syntax Tree (Midwest PHP 2020)
Climbing the Abstract Syntax Tree (php[world] 2019)
Climbing the Abstract Syntax Tree (PHP Russia 2019)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP South Africa 2018)
Climbing the Abstract Syntax Tree (PHP Developer Days Dresden 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Kicking off with Zend Expressive and Doctrine ORM (PHP MiNDS March 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (Forum PHP 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Climbing the Abstract Syntax Tree (IPC Fall 2017)
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Climbing the Abstract Syntax Tree (PHP South Africa 2017)
Get Started with RabbitMQ (CoderCruise 2017)
Dip Your Toes in the Sea of Security (CoderCruise 2017)

Recently uploaded (20)

PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
CIFDAQ's Market Insight: SEC Turns Pro Crypto
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
NewMind AI Weekly Chronicles - August'25 Week I
Mobile App Security Testing_ A Comprehensive Guide.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding

Best practices for crafting high quality PHP apps (PHP South Africa 2018)