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.

Fef6ec2170ad1ecfcacdf2ead305f040?s=128

Joe Ferguson

May 23, 2019
Tweet

Transcript

  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

    tested
  5. Shameless Plug phparch.com/article/artisinal-producing-packages-part-one/ 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 https://github.com/phan/phan/wiki/Issue-Types-Caught-by-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 https://github.com/phpstan/phpstan

  38. larastan https://github.com/nunomaduro/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 https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended-

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

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

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

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

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

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

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

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

  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
  58. Joe Ferguson Twitter: @JoePFerguson Email: joe@joeferguson.me Contact Info: