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

How static PHP analyzer changed the way I look at code

IchHabRecht
December 06, 2018

How static PHP analyzer changed the way I look at code

Let me introduce you to the world of static PHP code analyzers. I'd like to show you which tools exists, how to use them and how they help you to improve your code quality.

IchHabRecht

December 06, 2018
Tweet

More Decks by IchHabRecht

Other Decks in Programming

Transcript

  1. How static PHP analyzer
    changed the way I look at code
    Nicole Cordes, biz-design, SymfonyCon Lisbon 2018

    View Slide

  2. TOOLS

    View Slide

  3. TOOLS
    PHPStan
    PHP Static Analysis Tool
     https://github.com/phpstan/phpstan
    Strength:
    Type safety checks

    View Slide

  4. TOOLS
    PHPStan
    PHP Static Analysis Tool
     https://github.com/phpstan/phpstan
    Strength:
    Type safety checks

    View Slide

  5. TOOLS
    PHPMD
    PHP Mess Detector
     https://phpmd.org
    Strength:
    Code structure and measurements

    View Slide

  6. TOOLS
    PHPMD
    PHP Mess Detector
     https://phpmd.org
    Strength:
    Code structure and measurements

    View Slide

  7. TOOLS
    Code Climate
    Automated code review and
    quality analytics
     https://codeclimate.com
    Strength:
    Lines of code and complexity

    View Slide

  8. TOOLS
    Code Climate
    Automated code review and
    quality analytics
     https://codeclimate.com
    Strength:
    Lines of code and complexity

    View Slide

  9. TOOLS
    Code Climate
    Automated code review and
    quality analytics
     https://codeclimate.com
    Strength:
    Lines of code and complexity

    View Slide

  10. TOOLS
    SonarCloud
    SonarQube as a Service
     https://sonarcloud.io
    Strength:
    Code complexity and duplications

    View Slide

  11. TOOLS
    SonarCloud
    SonarQube as a Service
     https://sonarcloud.io
    Strength:
    Code complexity and duplications

    View Slide

  12. TOOLS
    SonarCloud
    SonarQube as a Service
     https://sonarcloud.io
    Strength:
    Code complexity and duplications

    View Slide

  13. TOOLS
    SonarCloud
    SonarQube as a Service
     https://sonarcloud.io
    Strength:
    Code complexity and duplications

    View Slide

  14. MEASUREMENT

    View Slide

  15. MEASUREMENT
    Names
     Variable names
     Not too short
     Not too long
     lowerCamelCase
     Method names
     Not too short
     Usage of __construct method
     isX() or hasY() for bool return type

    View Slide

  16. MEASUREMENT
    Lengths
     Method size too long
     Split into multiple small pieces
     Parameter list too long
     Group parameters in new objects
     (Public) field count to high
     Redesign objects and group fields

    View Slide

  17. CODE

    View Slide

  18. CODE
    Structure I
     Commented out code
     Remove!
     Too many return statements
     Return early but only once
     Prevent superfluous return variables
     Prevent superfluous else branch
     Too many return type possibilities
     Stick to one return type

    View Slide

  19. CODE
    Structure II
     Too many methods
     Split into multiple object
     Throw dedicated exceptions
     Avoid static calls
     Use dependencies

    View Slide

  20. COMPLEXITY

    View Slide

  21. COMPLEXITY
    Cognitive Complexity
     How hard is your code to understand
     Each control structure increases the complexity
     Logical operators increase the count
     Ternary and null coalescing operator increase the
    count
     Merge nested if statements
     Refactor and use helper functions

    View Slide

  22. CONCLUSION

    View Slide

  23. CONCLUSION
    The way I changed
     I pay more attention in naming variables and methods
     I refractor conditions to own functions
     I prevent multiple return points
     I use exactly one return type
     I started to think about my code before I write it
     Benefits
     My classes and concepts are more structured
     My code is more readable
     My code is better testable

    View Slide

  24. WARNING

    View Slide

  25. WARNING
    Stop overengineering
     Maybe not possible to remove all remarks
     Depending on frameworks
     Depending on time
     Depending on money
     False positives may be found
     Use the knowledge in upcoming code!

    View Slide

  26. Thank you for listening

    View Slide

  27. Photograph Credits
     https://pxhere.com/en/photo/868966
     https://pxhere.com/en/photo/599783
     https://pxhere.com/en/photo/46962
     https://commons.wikimedia.org/wiki/File:Jenga_distorted.jpg
     https://pxhere.com/en/photo/1175794
     https://pxhere.com/en/photo/672522

    View Slide