Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

TOOLS

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

MEASUREMENT

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

CODE

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

COMPLEXITY

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

CONCLUSION

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

WARNING

Slide 25

Slide 25 text

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!

Slide 26

Slide 26 text

Thank you for listening

Slide 27

Slide 27 text

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