Search for code duplicates in WebStorm/PhpStorm

We’re glad to announce a new code analysis feature that is available in the most recent PhpStorm EAP— Locate Duplicates. This feature lets you quickly find duplicate code fragments and then fix them, therefore reducing the size of your code and making it easier to understand and maintain.

The feature is invoked via Code | Locate Duplicates menu that brings the Code Duplication Analysis Settings dialog where you can adjust the search options.

PhpStorm can detect duplicates in PHP, JavaScript and other code. Variables, functions and literals can be anonymized.

Do not show duplicates simpler than option can be used to exclude duplicates of insignificant size. Anonymize uncommon subexpressions simpler than option is used to anonymize not only single entries like variables or literals, but also short expressions. Default values for these settings are good to begin with but you can of course experiment and find settings that best match your code.

Results of this search are shown in a diff view with code fragments so that you can inspect the results:

Last, but not least fact is that PhpStorm also detects duplicates in CSS code, so you can easily spot similar declarations, and this surely helps find a way through large CSS files. You can adjusted the criteria for declaring the CSS fragments as duplicates, too:

This entry was posted in Cool Feature, PhpStorm, WebStorm and tagged , , , , . Bookmark the permalink.
  • EugeneOZ

    And I’ve already did one refactoring after this inspection :) Thank you!

  • Bryan Green

    Where is ECMAScript used over JavaScript?

  • ragtek

    What’s this for a value in “Do not show dupl. simpler then x” ?
    Wouldn’t it be easier to say “do not whos dupl. < 3 lines" ?

    • Brendan Wood

      This is just a guess, but it may be using a tokens instead of lines when matching, which may be more beneficial than matching lines.

  • Sam

    Wow. Thanks for it.

  • ck3g

    How are you do that? In my case Code | Locate Duplicates always disabled. I’ve try to open different files (php, js, html). Menu item still disabled

    • ck3g

      Restart of the IDE resolve my problem