Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Static Analysis & Strict Types

Static Analysis & Strict Types

Dive deep into your code bases and add strict typing to leverage static analysis tools that can help find bugs in your code before it’s even run! We’ll explore Phan and PhpStan, both fantastic static analysis tools that can give detailed introspection into our applications. Static Analyzers can help find overly complicated logic and other common issues in our code bases as well as ensure our data types are standardized across our application processing.

Joe Ferguson

May 23, 2019
Tweet

More Decks by Joe Ferguson

Other Decks in Technology

Transcript

  1. Static Analysis & Strict Types
    Joe Ferguson

    View Slide

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

    View Slide

  3. 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

    View Slide

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

    View Slide

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

    View Slide

  6. Composer.json
    composer.json

    View Slide

  7. \EasyMath\Addition
    src/Addition.php

    View Slide

  8. \EasyMath\Subtraction
    src/Subtraction.php

    View Slide

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

    View Slide

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

    View Slide

  11. Static Analysis

    View Slide

  12. Phan

    View Slide

  13. Installing Phan

    View Slide

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

    View Slide

  15. Manually Configuring Phan
    .phan/config.php

    View Slide

  16. Running Phan
    php vendor/bin/phan

    View Slide

  17. Running Phan
    php vendor/bin/phan

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  21. Real World App

    View Slide

  22. Running Phan

    View Slide

  23. Incrementally
    Strengthening Analysis

    View Slide

  24. Manually Configuring Phan
    .phan/config.php

    View Slide

  25. Running Phan

    View Slide

  26. We expected a
    bit more output…

    View Slide

  27. PHPStan

    View Slide

  28. Installing PHPStan

    View Slide

  29. Configuring PHPStan
    phpstan.neon

    View Slide

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

    View Slide

  31. PSR-4 Autoloading
    composer.json

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  35. Back to the real world
    phpstan.neon

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  39. Installing larastan
    composer require --dev nunomaduro/larastan

    View Slide

  40. Running larastan
    php artisan code:analyse

    View Slide

  41. Improving the Quality
    of Static Analysis

    View Slide

  42. Documenting Our Code
    src/Addition.php

    View Slide

  43. 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

    View Slide

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

    View Slide

  45. Documenting Our Code
    src/Addition.php

    View Slide

  46. Running Phan
    ./vendor/bin/phan

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  57. 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

    View Slide

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

    View Slide