How to detect complex duplicated methods with PHPStan

Click for: original source

Duplicated code is a code smell that hides the potential of better design. How can we find it? Is it 100 % identical code token by token? Are methods getName() and getName() on 2 entities duplicated? By Tomas Votruba.

The article main points are:

  • Why even care about duplicated code?
  • What is not duplicated method?
  • How can we spot the duplicated method with X-Rays?
  • How to teach it phpStan?
  • 3 steps to get rid of duplications in your code today

PHPStan changes an approach to duplicated code. It runs on php-parser and uses an abstract syntax tree. So why should we care? PHPStan gives human output, so we know what to do.

"Class methods ProductSorter::sortResults() and PictureSorter::sortResults() are identical

PHPStan does this hard work on each commit and tells us precisely the duplicated code. When we know where duplications are, we can do something about it. Our project code will be more consistent, logical, and each piece of code will fit into another. Good read!

[Read More]

Tags php web-development tdd programming