Slide 1

Slide 1 text

Static Analysis & Strict Types Joe Ferguson

Slide 2

Slide 2 text

Who Am I? Joe Ferguson Senior Full Stack Developer @ Preteckt Twitter: @JoePFerguson OSMI Board Member The Joindin Foundation & Joindin Leadership Team

Slide 3

Slide 3 text

Agenda Review a basic PHP package Discuss Static Analysis and demo tools Demo tools on a real world application Add some new(ish) PHP 7.x features to improve analysis results

Slide 4

Slide 4 text

PHP-Easy-Math Simple example Addition Class Subtraction Class All methods unit tested

Slide 5

Slide 5 text

Shameless Plug phparch.com/article/artisinal-producing-packages-part-one/ Artisinal: Producing Packages, Part One

Slide 6

Slide 6 text

Composer.json composer.json

Slide 7

Slide 7 text

\EasyMath\Addition src/Addition.php

Slide 8

Slide 8 text

\EasyMath\Subtraction src/Subtraction.php

Slide 9

Slide 9 text

\EasyMath\Tests\AdditionTest src/tests/AdditionTest.php

Slide 10

Slide 10 text

\EasyMath\Tests\SubtractionTest src/tests/SubtractionTest.php

Slide 11

Slide 11 text

Static Analysis

Slide 12

Slide 12 text

Phan

Slide 13

Slide 13 text

Installing Phan

Slide 14

Slide 14 text

Auto Configuring Phan php vendor/bin/phan --init --init-level=1 --init-overwrite

Slide 15

Slide 15 text

Manually Configuring Phan .phan/config.php

Slide 16

Slide 16 text

Running Phan php vendor/bin/phan

Slide 17

Slide 17 text

Running Phan php vendor/bin/phan

Slide 18

Slide 18 text

Understanding Phan https://github.com/phan/phan/wiki/Issue-Types-Caught-by-Phan

Slide 19

Slide 19 text

\EasyMath\Tests\AdditionTest src/tests/AdditionTest.php

Slide 20

Slide 20 text

Manually Configuring Phan .phan/config.php php ./vendor/bin/phan

Slide 21

Slide 21 text

Real World App

Slide 22

Slide 22 text

Running Phan

Slide 23

Slide 23 text

Incrementally Strengthening Analysis

Slide 24

Slide 24 text

Manually Configuring Phan .phan/config.php

Slide 25

Slide 25 text

Running Phan

Slide 26

Slide 26 text

We expected a bit more output…

Slide 27

Slide 27 text

PHPStan

Slide 28

Slide 28 text

Installing PHPStan

Slide 29

Slide 29 text

Configuring PHPStan phpstan.neon

Slide 30

Slide 30 text

Running PHPStan php ./vendor/bin/phpstan analyse —level 0

Slide 31

Slide 31 text

PSR-4 Autoloading composer.json

Slide 32

Slide 32 text

Running PHPStan php ./vendor/bin/phpstan analyse —level 0

Slide 33

Slide 33 text

Running PHPStan php ./vendor/bin/phpstan analyse —level 5

Slide 34

Slide 34 text

Running PHPStan php ./vendor/bin/phpstan analyse —level max

Slide 35

Slide 35 text

Back to the real world phpstan.neon

Slide 36

Slide 36 text

Back to the real world php ./vendor/bin/phpstan analyse —level 0

Slide 37

Slide 37 text

Extending PHPStan https://github.com/phpstan/phpstan

Slide 38

Slide 38 text

larastan https://github.com/nunomaduro/larastan

Slide 39

Slide 39 text

Installing larastan composer require --dev nunomaduro/larastan

Slide 40

Slide 40 text

Running larastan php artisan code:analyse

Slide 41

Slide 41 text

Improving the Quality of Static Analysis

Slide 42

Slide 42 text

Documenting Our Code src/Addition.php

Slide 43

Slide 43 text

Running Phan ./vendor/bin/phan PhanTypeMismatchDeclaredReturn Doc-block of add contains declared return type float which is incompatible with the return type int declared in the signature

Slide 44

Slide 44 text

Running PHPStan ./vendor/bin/phpstan analyse --level max

Slide 45

Slide 45 text

Documenting Our Code src/Addition.php

Slide 46

Slide 46 text

Running Phan ./vendor/bin/phan

Slide 47

Slide 47 text

Running PHPStan ./vendor/bin/phpstan analyse --level max

Slide 48

Slide 48 text

Real Time Analysis https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 49

Slide 49 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 50

Slide 50 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 51

Slide 51 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 52

Slide 52 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 53

Slide 53 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 54

Slide 54 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 55

Slide 55 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 56

Slide 56 text

Php Inspections (EA Extended) https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

Slide 57

Slide 57 text

Resources The Workshop: The Road to 7.3 https://www.phparch.com/magazine/2019/01/ devops-depths/ Phan https://github.com/phan/phan PHPStan https://github.com/phpstan/phpstan

Slide 58

Slide 58 text

Joe Ferguson Twitter: @JoePFerguson Email: [email protected] Contact Info: