Slide 1

Slide 1 text

PHPStan: finding bugs in your code without even running it by Gabriel Caruso Darkmira Tour PHP 2019

Slide 2

Slide 2 text

Thank You!

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Compiled Languages

Slide 5

Slide 5 text

Do you Monitor your PHP application?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

New Relic Errors Analytics

Slide 8

Slide 8 text

Who am I? ● Backend Engineer at Usabilla ● PHP enthusiastic ● PHPSP member ● OSS contributor ● Stickers/ELEPHANTS lover

Slide 9

Slide 9 text

Compiled X Interpreted Languages

Slide 10

Slide 10 text

Interpreted language PHP, JavaScript, Ruby... “An interpreted language is a type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions.”

Slide 11

Slide 11 text

Compiled Language Java, C, Swift... “A compiled language is a programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters”

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

New Relic Errors Analytics

Slide 15

Slide 15 text

What does a Static Analyzer do?

Slide 16

Slide 16 text

Static Analyzer “Static Analyzer Tool is the analysis of computer software that is performed without actually executing programs.”

Slide 17

Slide 17 text

What is PHPStan?

Slide 18

Slide 18 text

PHPStan “PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line”

Slide 19

Slide 19 text

Why should I use PHPStan if I already write tests for my Code?

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

- Do you have 100% coverage of your code? - Do your tests run under a minute?

Slide 22

Slide 22 text

PHPStan can analyze your entire codebase in almost a minute.

Slide 23

Slide 23 text

The four essential tools for modern PHP development PHP Lint: checks if your code is right PHPUnit: checks if your business logic is right PHP Code Sniffer: checks if your code is readable PHPStan: checks if your code does not have bugs

Slide 24

Slide 24 text

Questions? Something that you didn’t understand? Do you want to add something?

Slide 25

Slide 25 text

Let’s use PHPStan!

Slide 26

Slide 26 text

Installation with Composer composer require --dev phpstan/phpstan "require-dev": { “phpstan/phpstan”: “^0.11” }

Slide 27

Slide 27 text

Usage ./vendor/bin/phpstan analyse src/ tests/ Executable Command Folders

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

What does happen in the first run? - Parse/Fatal Errors - Misconfigured PSR-4 Autoloading - Extra arguments passed to functions - Magic behaviour that needs to be defined (We'll talk more about this point in specific in a second) What happens after I solve all first-run errors?

Slide 30

Slide 30 text

PHPStan is based on levels (Yes, like the Games!) 0 (first run) -> 7 (max level)

Slide 31

Slide 31 text

Defining a level ./vendor/bin/phpstan analyse -l 3 src/ Level

Slide 32

Slide 32 text

Questions about how to use PHPStan?

Slide 33

Slide 33 text

Let’s get the most of PHPStan

Slide 34

Slide 34 text

Using phpstan.neon parameters: level: 3 paths: - src - tests ./vendor/bin/phpstan analyze

Slide 35

Slide 35 text

Ignoring errors (using phpstan.neon) parameters: ignoreErrors: “- '~^Call to an undefined method Foo\\Bar\\::baz\(\)\.\z~'”

Slide 36

Slide 36 text

Using PHPStan extensions - Doctrine - PHPUnit - Symfony - Mockery Some magic functions of ORMs and Frameworks need some extra work

Slide 37

Slide 37 text

For further studies, visit PHPStan’s GitHub repository https://github.com/phpstan/phpstan

Slide 38

Slide 38 text

- Tweet me @carusogabriel - Give a feedback to this talk Questionings? Thank you ♡