$30 off During Our Annual Pro Sale. View Details »

Code Analysis with PHP Metrics

Code Analysis with PHP Metrics

Chris Gmyr

July 27, 2017
Tweet

More Decks by Chris Gmyr

Other Decks in Technology

Transcript

  1. Code Analysis
    with
    PHP Metrics

    View Slide

  2. Presented by
    Chris Gmyr
    @cmgmyr

    View Slide

  3. Installation
    composer global require phpmetrics/phpmetrics

    View Slide

  4. Generate Report
    phpmetrics --report-html=myreport /path/of/src

    View Slide

  5. Laravel
    git clone [email protected]:laravel/laravel.git
    phpmetrics --report-html=laravel_metrics laravel

    View Slide

  6. View Slide

  7. View Slide

  8. View Slide

  9. What Are We Going to Learn?
    4 Maintainability
    4 Violations
    4 Complexity
    4 Relationships

    View Slide

  10. But First,
    Some Vocabulary!
    !

    View Slide

  11. Cyclomatic Complexity
    The cyclomatic complexity of a section of source code is the number of
    linearly independent paths within it.
    4 If the source code contained no control flow statements (conditionals
    or decision points), the complexity would be 1.
    4 One IF statement = two paths through the code, complexity would be 2.
    4 Two nested single-condition IFs, or one IF with two conditions, would
    produce a complexity of 3.
    https://en.wikipedia.org/wiki/Cyclomatic_complexity

    View Slide

  12. View Slide

  13. More Conditionals & Statements = More Complex

    View Slide

  14. Lower Score is Better!

    View Slide

  15. Halstead Complexity Measures
    Observation that metrics of the software should
    reflect the implementation or expression of algorithms
    in different languages, but be independent of their
    execution on a specific platform.
    https://en.wikipedia.org/wiki/
    Halstead_complexity_measures
    http://www.verifysoft.com/en_halstead_metrics.html

    View Slide

  16. View Slide

  17. Lower Score is Better!

    View Slide

  18. Maintainability Index
    Provides a score between 0 to 118. Halstead's metrics, LOC and
    Cyclomatic complexity number.
    General scores:
    4 <64: low maintainability. The project has probably technical debt.
    4 65-84: medium maintainability. The project has problems, but
    nothing really serious.
    4 >85: high maintainability. The project is probably good.

    View Slide

  19. Higher Score is Better!

    View Slide

  20. Let's Explore!

    View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. Violations

    View Slide

  36. View Slide

  37. View Slide

  38. Size & Volume

    View Slide

  39. View Slide

  40. Complexity & Defects

    View Slide

  41. View Slide

  42. Object Oriented Metrics

    View Slide

  43. View Slide

  44. Object Relations

    View Slide

  45. View Slide

  46. Coupling

    View Slide

  47. View Slide

  48. View Slide

  49. Takeaways
    1. More Classes
    2. Smaller Classes
    3. Simpler Classes
    4. Loosely Coupled Classes

    View Slide

  50. Thank you!

    View Slide

  51. Say "Hi"!
    twitter.com/cmgmyr
    github.com/cmgmyr
    chrisgmyr.com

    View Slide