Slide 1

Slide 1 text

Package development #phptour ! @hannesvdvreken @afup

Slide 2

Slide 2 text

Hi, my name is Hannes.

Slide 3

Slide 3 text

"

Slide 4

Slide 4 text

madewithlove.be 20 × "&!'()

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Package development Intermediate and advanced techniques

Slide 7

Slide 7 text

1. Beginner steps 2. Environments 3. Package stability

Slide 8

Slide 8 text

1. Beginner steps Everyone started out as a newbie.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

BEGINNER STEPS No renaming keep the old one, but create a new

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

https:/ /seld.be/notes/common-files-in-php-packages

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

BEGINNER STEPS Instructions to install composer require guzzlehttp/guzzle

Slide 18

Slide 18 text

BEGINNER STEPS Instructions to install NOT: “add guzzlehttp/guzzle to your composer.json”

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

BEGINNER STEPS - VERSIONS Class Sdk { public function __construct(Framework\Config) { … } } /** * Register method. */ public function register() { // Register SDK class. $this->container->share('twilio', function () { return new Sdk($this->container->make('config')) }); }

Slide 22

Slide 22 text

BEGINNER STEPS - VERSIONS Class Sdk { public function __construct(array $options) { … } } /** * Register method. */ public function register() { // Register SDK class. $this->container->share('twilio', function () { $config = $this->app->get('config'); return new Sdk($config->get('services.twilio')); }); }

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

BEGINNER STEPS - CODE STYLE Recommendations: PSR-2

Slide 25

Slide 25 text

BEGINNER STEPS - CODE STYLE Use of tooling phpcs, php-cs-fixer

Slide 26

Slide 26 text

BEGINNER STEPS - CODE STYLE Do it early boy-scouting file-per-file git blame open pull requests

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

BEGINNER STEPS - TESTS/SPECS Not just the happy path

Slide 29

Slide 29 text

BEGINNER STEPS - TESTS/SPECS It’s your insurance in multiple ways

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

BEGINNER STEPS - VERSIONING semver.org read it

Slide 33

Slide 33 text

BEGINNER STEPS - VERSIONING Release often

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

BEGINNER STEPS - CHANGELOG Document releases changelog.md

Slide 36

Slide 36 text

BEGINNER STEPS - CHANGELOG # Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased][unreleased] ### Changed - Improve argument against commit logs. ## [0.0.8] - 2015-02-17 ### Changed - Update year to match in every README example. - Reluctantly stop making fun of Brits only, since most of the world writes dates in a strange way. ### Fixed - Fix typos in recent README changes. - Update outdated unreleased diff link.

Slide 37

Slide 37 text

BEGINNER STEPS - CHANGELOG keepachangelog.com

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

changehub.io

Slide 40

Slide 40 text

BEGINNER STEPS - CHANGELOG upgrade.md

Slide 41

Slide 41 text

BEGINNER STEPS - CHANGELOG Release often

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

BEGINNER STEPS GitHub PC test service packagist

Slide 45

Slide 45 text

BEGINNER STEPS GitHub PC test service packagist git push

Slide 46

Slide 46 text

BEGINNER STEPS GitHub PC test service packagist webhook webhook

Slide 47

Slide 47 text

BEGINNER STEPS GitHub PC test service packagist analyses composer.json /stores version runs tests

Slide 48

Slide 48 text

BEGINNER STEPS GitHub PC test service packagist status green!

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

BEGINNER STEPS - README.MD Code samples copy-paste

Slide 51

Slide 51 text

BEGINNER STEPS - README.MD // open an image file $img = Image::make('public/foo.jpg'); // resize image instance $img->resize(320, 240); // insert a watermark $img->insert('public/watermark.png'); // save image in desired format $img->save('public/bar.jpg');

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

BEGINNER STEPS No license is the worst http:/ /choosealicense.com/

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

BEGINNER STEPS - CONTRIBUTING Instructions how to test how to make pull request philosophy

Slide 56

Slide 56 text

BEGINNER STEPS Congrats, you’re no longer a newbie!

Slide 57

Slide 57 text

2. Environments

Slide 58

Slide 58 text

INTERMEDIATE TIPS - ENVIRONMENTS Packages have environments too

Slide 59

Slide 59 text

INTERMEDIATE TIPS - ENVIRONMENTS dev/local test/ci production

Slide 60

Slide 60 text

INTERMEDIATE TIPS - ENVIRONMENTS - DEV & CI not in production: composer require --dev also installs require-dev dependencies

Slide 61

Slide 61 text

INTERMEDIATE TIPS - ENVIRONMENTS - CI CI only: code coverage reporting

Slide 62

Slide 62 text

INTERMEDIATE TIPS - ENVIRONMENTS - CI phpunit --coverage-html=/tmp/coverage/; vs phpunit --coverage-clover=/tmp/coverage.xml; ocular code-coverage:upload /tmp/coverage.xml;

Slide 63

Slide 63 text

INTERMEDIATE TIPS - ENVIRONMENTS - PROD Keep your tests out of other peoples production

Slide 64

Slide 64 text

INTERMEDIATE TIPS - CLEAN DISTS --prefer-dist

Slide 65

Slide 65 text

INTERMEDIATE TIPS - CLEAN DISTS .gitattributes

Slide 66

Slide 66 text

.gitattributes

Slide 67

Slide 67 text

INTERMEDIATE TIPS - CLEAN DISTS "autoload": { "psr-4": { "GuzzleHttp\\": "src/" } }, "autoload-dev": { "psr-4": { "GuzzleHttp\\Stubs\\": "tests/stubs/" } }

Slide 68

Slide 68 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS Don’t do composer require --global when you need it in CI

Slide 69

Slide 69 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS dev-dependencies + composer scripts

Slide 70

Slide 70 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS "require-dev": { "phpunit/phpunit": "~1.0", }, "scripts": { "unit-tests": "phpunit —coverage-clover…" },

Slide 71

Slide 71 text

INTERMEDIATE TIPS - RECAP - dev dependencies - exclude files from distributions - composer scripts

Slide 72

Slide 72 text

3. Package stability

Slide 73

Slide 73 text

STABILITY As package maintainer: Less major releases

Slide 74

Slide 74 text

STABILITY If a dependency has new major version, your package needs new major version.

Slide 75

Slide 75 text

Trick #1 STABILITY

Slide 76

Slide 76 text

Keep number of hard requirements low STABILITY

Slide 77

Slide 77 text

STABILITY - SOFT-REQUIREMENTS Soft requirements eg: use decorators

Slide 78

Slide 78 text

use Psr\Cache\CacheItemPool; class CacheDecorator implements … { … } STABILITY - SOFT-REQUIREMENTS

Slide 79

Slide 79 text

STABILITY - SOFT-REQUIREMENTS Even better: separate package

Slide 80

Slide 80 text

{ "name": "league/flysystem-eventable-filesystem", "require": { "php": ">=5.4.0", "league/event": "~1.0", "league/flysystem": "~1.0" }, … STABILITY - SOFT-REQUIREMENTS

Slide 81

Slide 81 text

Trick #2 STABILITY - DECOUPLING

Slide 82

Slide 82 text

Depend on interface packages STABILITY - DECOUPLING

Slide 83

Slide 83 text

STABILITY - DECOUPLING Interface packages: - only interfaces - zero dependencies

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

concept virtual package is a high level placeholder for a dependency on a more low level implementation STABILITY - DECOUPLING

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

company/sdk requires (1.0.0) psr/cache-implementation requires (1.0.0) psr/cache

Slide 89

Slide 89 text

psr/cache-implementation provides (1.0.0) requires (1.0.0) league/flysystem requires (^1.0) cache/filesystem-adapter psr/cache

Slide 90

Slide 90 text

company/sdk application requires (x.y.z) requires (1.0.0) psr/cache-implementation

Slide 91

Slide 91 text

company/sdk requires (1.0.0) psr/cache-implementation provides (1.0.0) requires (1.0.0) application requires (x.y.z) requires (0.5.0) requires (1.0.0) cache/filesystem-adapter psr/cache requires (^1.0) league/flysystem

Slide 92

Slide 92 text

STABILITY - DECOUPLING - BENEFITS Users can choose low level packages they want

Slide 93

Slide 93 text

STABILITY - DECOUPLING - BENEFITS Users don’t get locked-in to a major version

Slide 94

Slide 94 text

Prefer having less hard dependencies STABILITY - RECAP

Slide 95

Slide 95 text

Prefer requiring stabler packages STABILITY - RECAP

Slide 96

Slide 96 text

STABILITY - RECAP Prefer requiring virtual packages

Slide 97

Slide 97 text

DECOUPLING - OTHER CASES - Logging (PSR-3) - HTTP (PSR-7) - Caching (PSR-6) - File system (Flysystem)

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

Semver checker ^ vs ~ Marketing Communication (empathy)

Slide 100

Slide 100 text

RECAP 1. Beginner steps 2. Environments 3. Stability

Slide 101

Slide 101 text

Thank you! https:/ /joind.in/talk/28a72 @hannesvdvreken @afup

Slide 102

Slide 102 text

First: Questions, Then: Lunch! @hannesvdvreken @afup

Slide 103

Slide 103 text

• http:/ /mwl.be REFERENCES