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


  1. Static Analysis & Strict Types Joe Ferguson

  2. Who Am I? Joe Ferguson Senior Full Stack Developer @

    Preteckt Twitter: @JoePFerguson OSMI Board Member The Joindin Foundation & Joindin Leadership Team
  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
  4. PHP-Easy-Math Simple example Addition Class Subtraction Class All methods unit

  5. Shameless Plug Artisinal: Producing Packages, Part One

  6. Composer.json composer.json

  7. \EasyMath\Addition src/Addition.php

  8. \EasyMath\Subtraction src/Subtraction.php

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

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

  11. Static Analysis

  12. Phan

  13. Installing Phan

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

  15. Manually Configuring Phan .phan/config.php

  16. Running Phan php vendor/bin/phan

  17. Running Phan php vendor/bin/phan

  18. Understanding Phan

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

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

  21. Real World App

  22. Running Phan

  23. Incrementally Strengthening Analysis

  24. Manually Configuring Phan .phan/config.php

  25. Running Phan

  26. We expected a bit more output…

  27. PHPStan

  28. Installing PHPStan

  29. Configuring PHPStan phpstan.neon

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

  31. PSR-4 Autoloading composer.json

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

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

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

  35. Back to the real world phpstan.neon

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

  37. Extending PHPStan

  38. larastan

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

  40. Running larastan php artisan code:analyse

  41. Improving the Quality of Static Analysis

  42. Documenting Our Code src/Addition.php

  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
  44. Running PHPStan ./vendor/bin/phpstan analyse --level max

  45. Documenting Our Code src/Addition.php

  46. Running Phan ./vendor/bin/phan

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

  48. Real Time Analysis

  49. Php Inspections (EA Extended)

  50. Php Inspections (EA Extended)

  51. Php Inspections (EA Extended)

  52. Php Inspections (EA Extended)

  53. Php Inspections (EA Extended)

  54. Php Inspections (EA Extended)

  55. Php Inspections (EA Extended)

  56. Php Inspections (EA Extended)

  57. Resources The Workshop: The Road to 7.3 devops-depths/ Phan PHPStan
  58. Joe Ferguson Twitter: @JoePFerguson Email: Contact Info: