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?
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/