1

I'm looking at some legacy code in PhpStorm and there are some instances within the codebase where a particular function call is wrapped by another function call within an if statement e.g.

if (thisIsTrue($param1, $param2)) {
    // possibly some function calls above
    callThisFunction($paramA, $paramB);
    // possibly some function calls below
}

I've had some success with just basic Linux find/grep commands to highlight files of interest but I would like to be able to create a structural search inspection that can find instances of callThisFunction that aren't wrapped by a specific if statement.

Does anyone know if this is possible with the vanilla IntelliJ search template functionality?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
connrs
  • 283
  • 1
  • 7
  • How BIG is the code base? Mainly, how often that function is called? You can place caret on the function name and invoke `Edit | Find Usages | Find Usages` and then just manually go through found places. https://www.jetbrains.com/help/phpstorm/find-highlight-usages.html – LazyOne Jun 10 '20 at 14:30
  • For SSR though (Structural Search and Replace) ... it's the "if" part that is hard for me to think how it will work. The best I can think of: check what the previous line has and if it does not match some RegEx... Not really using it that match, so cannot give you any solid examples that can be really useful here. Anyway: https://www.jetbrains.com/help/phpstorm/structural-search-and-replace.html – LazyOne Jun 10 '20 at 14:32

1 Answers1

1

The short answer is (still) no.

It is possible to define a "not within an if statement" template constraint, if you copy the existing template "Logging without if" (in the Operators category) or import this XML:

<searchConfiguration name="Logging without if" text="LOG.debug($Argument$);" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default">
  <constraint name="__context__" negateWithin="true" within="statement in if" contains="" />
  <constraint name="Argument" minCount="0" maxCount="2147483647" within="" contains="" />
</searchConfiguration>

However, it does not match when the if body has more than one statement, and my IntelliJ hanged when I modified the text to $Statement1$; log.tracef($Argument$); $Statement2$; with the same count constraints on the statements as on the arguments.

You could instead run 2 searches: one with just callThisFunction($paramA, $paramB); and one with the whole template, and compare the results.

Comparing the results should be easier if you convert your structural searches to inspections and then you run those inspections from the command line (https://www.jetbrains.com/help/idea/command-line-code-inspector.html)

For more information about the "within" constraint see https://ijnspector.wordpress.com/2020/06/11/contained-in-constraints/

Dan Berindei
  • 7,054
  • 3
  • 41
  • 48