Slide 1

Slide 1 text

Package development PHPDay, Verona ! @hannesvdvreken @phpday

Slide 2

Slide 2 text

Hi, my name is Hannes.

Slide 3

Slide 3 text

"

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

madewithlove.be

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Package development Intermediate and advanced techniques

Slide 8

Slide 8 text

1. Beginner steps 2. Intermediate tips 3. Package stability

Slide 9

Slide 9 text

1. Beginner steps Everyone started out as a newbie.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

BEGINNER STEPS phppackagechecklist.com

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

BEGINNER STEPS Instructions to install composer require guzzlehttp/guzzle

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 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 26

Slide 26 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 27

Slide 27 text

No content

Slide 28

Slide 28 text

BEGINNER STEPS - CODE STYLE Recommendations: PSR-2

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

BEGINNER STEPS - TESTS/SPECS Not just the happy path

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

BEGINNER STEPS - VERSIONING semver.org read it

Slide 37

Slide 37 text

BEGINNER STEPS - VERSIONING Release often

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

BEGINNER STEPS - CHANGELOG Document releases keepachangelog.com

Slide 40

Slide 40 text

BEGINNER STEPS - CHANGELOG Release often

Slide 41

Slide 41 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 42

Slide 42 text

BEGINNER STEPS - CHANGELOG keepachangelog.com

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

BEGINNER STEPS - CHANGELOG upgrade.md

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

BEGINNER STEPS GitHub PC test service packagist

Slide 49

Slide 49 text

BEGINNER STEPS GitHub PC test service packagist git push

Slide 50

Slide 50 text

BEGINNER STEPS GitHub PC test service packagist webhook webhook

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

BEGINNER STEPS GitHub PC test service packagist status green!

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

BEGINNER STEPS - README.MD Code samples copy-paste

Slide 55

Slide 55 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 56

Slide 56 text

No content

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

BEGINNER STEPS - HINT 1 jonathantorres/construct

Slide 62

Slide 62 text

BEGINNER STEPS - HINT 1 $ construct generate --help Arguments: name The vendor/project name Options: --test (-t) Testing framework (default: "phpunit") --license (-l) License (default: "MIT") --namespace (-s) Namespace for project (default: "Vendor\\Pro --git (-g) Initialize an empty Git repo --phpcs (-p) Generate a PHP Coding Standards Fixer config --keywords (-k) Comma separated list of Composer keywords --vagrant Generate a Vagrantfile

Slide 63

Slide 63 text

BEGINNER STEPS - HINT 1 Or use a skeleton package

Slide 64

Slide 64 text

BEGINNER STEPS - HINT 2 franzl/studio

Slide 65

Slide 65 text

BEGINNER STEPS - HINT 2 $ studio load --help Usage: load Arguments: path The path where the package files are located

Slide 66

Slide 66 text

2. Intermediate tips

Slide 67

Slide 67 text

INTERMEDIATE TIPS - ENVIRONMENTS Packages have environments too

Slide 68

Slide 68 text

INTERMEDIATE TIPS - ENVIRONMENTS dev/local test/ci production

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

INTERMEDIATE TIPS - ENVIRONMENTS - CI CI only: code coverage

Slide 71

Slide 71 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 72

Slide 72 text

INTERMEDIATE TIPS - ENVIRONMENTS - PROD Keep your tests out of other people’s production

Slide 73

Slide 73 text

INTERMEDIATE TIPS - CLEAN DISTS --prefer-dist

Slide 74

Slide 74 text

INTERMEDIATE TIPS - CLEAN DISTS .gitattributes

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

INTERMEDIATE TIPS Composer scripts stop typing vendor/bin/

Slide 78

Slide 78 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS "scripts": { "cs": "php-cs-fixer fix" } $ composer cs

Slide 79

Slide 79 text

INTERMEDIATE TIPS - COMPOSER SCRIPTS "scripts": { "test": ["phpunit", "phpspec run"] }

Slide 80

Slide 80 text

INTERMEDIATE TIPS - COMPOSER BINS #!/usr/bin/env php

Slide 81

Slide 81 text

INTERMEDIATE TIPS - COMPOSER BINS "bin": ["construct"]

Slide 82

Slide 82 text

INTERMEDIATE TIPS - COMPOSER BINS composer require --global not in CI

Slide 83

Slide 83 text

INTERMEDIATE TIPS Code Style

Slide 84

Slide 84 text

INTERMEDIATE TIPS - CODE STYLE Atomic commits easier to review w/o code style fixes

Slide 85

Slide 85 text

INTERMEDIATE TIPS - CODE STYLE Recommendations: PSR-2 < Symfony CS

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

if (5 === count($list)) { Yoda conditions

Slide 88

Slide 88 text

INTERMEDIATE TIPS - CODE STYLE $container = new Container; $container = new Container(); $options = [ 'ssl' => true, 'redirects' => false, ];

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

INTERMEDIATE TIPS - RECAP - separate environments - composer scripts - code style

Slide 92

Slide 92 text

Semver checker ^ vs ~ Marketing Communication (empathy) composer --sort-packages

Slide 93

Slide 93 text

3. Package stability

Slide 94

Slide 94 text

STABILITY As package maintainer: Less major releases

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

Keep number of hard requirements low STABILITY

Slide 97

Slide 97 text

STABILITY - SOFT-REQUIREMENTS Soft requirements eg: use decorators

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

Depend on interface packages STABILITY - DECOUPLING

Slide 101

Slide 101 text

STABILITY - SOFT-REQUIREMENTS Soft requirements eg: use decorators

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

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

Slide 104

Slide 104 text

psr/cache-implementation provides (1.0.0) requires (1.0.0) doctrine/cache requires (^1.3) doctrine/cache psr/cache

Slide 105

Slide 105 text

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

Slide 106

Slide 106 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/doctrine-adapter psr/cache requires (^1.3) doctrine/cache

Slide 107

Slide 107 text

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

Slide 108

Slide 108 text

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

Slide 109

Slide 109 text

Prefer having less hard dependencies STABILITY - RECAP

Slide 110

Slide 110 text

Prefer requiring stabler packages STABILITY - RECAP

Slide 111

Slide 111 text

STABILITY - RECAP Prefer requiring virtual packages

Slide 112

Slide 112 text

STABILITY - BENEFITS As package maintainer: Less major releases

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

RECAP 1. Beginner steps 2. Intermediate tips 3. Decouple for stability

Slide 116

Slide 116 text

Thank you! @hannesvdvreken @phpday https:/ /joind.in/talk/e48d2

Slide 117

Slide 117 text

Time for questions. @hannesvdvreken @phpday

Slide 118

Slide 118 text

• http:/ /mwl.be REFERENCES