Search for code duplicates in WebStorm/PhpStorm

Posted on by eugene.kudelevsky

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:

Comments below can no longer be edited.

7 Responses to Search for code duplicates in WebStorm/PhpStorm

  1. EugeneOZ says:

    September 28, 2011

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

  2. Bryan Green says:

    September 28, 2011

    Where is ECMAScript used over JavaScript?

  3. ragtek says:

    September 28, 2011

    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 says:

      December 6, 2011

      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.

  4. Sam says:

    September 28, 2011

    Wow. Thanks for it.

  5. ck3g says:

    September 29, 2011

    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 says:

      September 29, 2011

      Restart of the IDE resolve my problem