EVERY LINE OF CODE IS A
LIABILITY
Just like driving
Slide 4
Slide 4 text
PLEASE CONSIDER THIS PRACTICES
ROBUST TESTABLE SAFE MAINTAINABLE
Prevent every possible bug
Slide 5
Slide 5 text
PRATICES Depends of
TEAM & PROJECT
Slide 6
Slide 6 text
LET'S GET STARTED
Slide 7
Slide 7 text
Make State
IMMUTABLE
Slide 8
Slide 8 text
HOW WRITE IMMUTABLE CLASSES?
▸ Constructor is the only Injection Point
▸ No setters
▸ Private Attributes
▸ Make state private
Slide 9
Slide 9 text
Always
ASSERT
Slide 10
Slide 10 text
WHEN TO ASSERT INPUT?
▸ Always
▸ Build your own helpers to avoid code duplication
▸ Consider the package "beberlei/assert"
Slide 11
Slide 11 text
public function create(string $name, array $objects): void
{
Assertion::notEmpty($name);
Assertion::allIsInstanceOf($objects, \App\User::class);
}
Slide 12
Slide 12 text
PUBLIC API
Clear Contract
Slide 13
Slide 13 text
class Money
{
/**
* I know this sucks! But I have deployed
* this in a rush last friday ahahah. Good Luck!
*/
public function __construct($amount);
}
Slide 14
Slide 14 text
/**
* Takes the `amount` of the instance attribute `$money` to create {...} using
* the instance attribute `$dataTime` for the field `created_at`.
*
* @throws \Domain\Exceptions\SomethingNotAvailable If something is not available.
*/
public function handle(): void
Slide 15
Slide 15 text
If you use Semantic Versioning
DECLARE A PUBLIC API
Slide 16
Slide 16 text
HOW WRITE A SOLID CONTRACT?
▸ What is it
▸ What it does
▸ What it returns
▸ What is the behavior
▸ What is the behavior when the contract gets violated
Slide 17
Slide 17 text
Make classes
FINAL BY DEFAULT
Slide 18
Slide 18 text
class Db { /* ... */ }
class Core extends Db { /* ... */ }
class User extends Core { /* ... */ }
class Admin extends User { /* ... */ }
class Bot extends Admin { /* ... */ }
class BotThatDoesSpecialThings extends Bot { /* ... */ }
class PatchedBot extends BotThatDoesSpecialThings { /* ... */ }
class PatchedBotVersion2019 extends PatchedBot { /* ... */ }
Slide 19
Slide 19 text
class Food {}
class Human extends Food {}
class Nuno extends Human {}
$nuno = new Nuno();
$nuno->eat();
Slide 20
Slide 20 text
COMPOSITION OVER INHERITANCE
final class Food {}
$food = new Food();
$nuno = new Nuno($food)
$nuno->eat();
Slide 21
Slide 21 text
WHEN MARK CLASSES AS FINAL?
ALMOST ALWAYS - FORCE DEVELOPERS THIKING ABOUT COMPOSITION
Slide 22
Slide 22 text
HIGH QUALITY TESTS
Slide 23
Slide 23 text
You will implore for
LESS FEATURES
Slide 24
Slide 24 text
STATIC ANALYSIS
PHPSTAN & PHP INSIGHTS
Slide 25
Slide 25 text
HOW HAVE HIGH QUALITY TESTS?
▸ Add 100% of real Test Coverage - PHPUnit
▸ Use and abuse Static Analysis - PHPStan & PHP Insights
Slide 26
Slide 26 text
PLEASE CONSIDER THIS PRACTICES
▸ Open Source Libraries
▸ Important pieces of your software
▸ Long-lived projects
Slide 27
Slide 27 text
EXPERIENCE
Slide 28
Slide 28 text
No content
Slide 29
Slide 29 text
EFFECTIVE-PHP.COM
PREMIUM VIDEO COURSE - GOOD CODE IS SHORT, SIMPLE, AND ROBUST.
NUNOMADURO.COM
Thanks!