Search Templates
When you construct a template for a structural search you are basically writing a script. To simplify your scripting process, PhpStorm offers you a list of predefined search templates that you can use as prototypes for your search template.
On how to access the list of the existing search and replace templates, see the structural search and replace section.
A valid search or replacement template represents one of the following supported languages constructs:
Expression, for example
"John" . " " . "Doe"
Statement, or sequence of statements, for example
$people[] = new Person('Trillian');
Class designator, for example
class Engine implements IEngine
Line or block comments, for example
/** Created in PhpStorm */
.
In search templates, the following simplifications can be used:
Method body can be omitted.
Short class names (instead of fully qualified names) are used in the templates and constraint fields.
Using
class $Class$
as a template results in finding anonymous classes as well.Templates for comments and documentation comments should contain variables and constructs with correct comment and PHPDoc/JSDoc syntax.
Each search or replace template consists of variables $variable_name$
to which you can add a condition (modifier) to narrow your search results. Modifiers depend on a variable in your search template.
Count modifier
The Count modifier specifies a number of occurrences.
For example, in the class $a$ {public function $b$()}
search template, for $b$
variable specify min and max numbers in the Count modifier field. To set the unlimited maximum count, provide an empty value in the modifier field.
PhpStorm adds [0,∞]
to the variable and searches for the specified range of numbers.
Reference modifier
The Reference modifier lets you reference some other search template in the variable.
The reference will always contain the name of a preconfigured or saved template, and you can use auto-completion to fill out this field.
For example, you can define a search template for locating static methods of a class, and then reference it in another template to detect the erroneous instance calls to such static methods.
Type modifier
The Type modifier adds a type of the value or expression that is expected for the specified variable.
Use the following modifier values to locate variables of the corresponding types:
string
int
orinteger
double
orfloat
boolean
array
null
To locate instances of a particular class, provide its FQN including the leading \
as the Type modifier value.
For example, for the $b$
variable, type \ExampleClass
in the Type modifier field.
PhpStorm searches for ExampleClass
instantiations.
Text filter
The Text modifier checks the variable against regular expressions or plain text.
For example, to detect explicit magic method calls, use the $a$->$b$()
template and set the Text modifier for the $b$
variable to the ^__.+$
regular expression.
Script constraints
The Script modifier adds Groovy script constraints to the search template. Script constraints are used when you search for certain language constructs.
For example, constructors with the specified number of parameters, or members with the specified visibility modifiers.
All variables used in a template can be accessed from script constraints. When you add a script constraint to your variable, PhpStorm matches it against the PSI tree, this variable is in fact a node in the PSI tree.
Let's say, you have a variable that matches a method, a toString()
method. Then this variable is actually a PsiMethod
node. Retrieving variable.parent
will produce a PsiClass
node, and so forth. variable.text
then will give you the entire text of the method. If you just need the name of the method, you can use variable.name
.
In another case the structural search and replace variable may match some expression, for example, a reference to a variable, a PsiReferenceExpression
. An expression has no name of course, but retrieving the entire text of the expression, will give you the name of the variable to which it is referring.
You can check the syntax of script constraints that are used in the following existing templates:
sample method invocation with a constant parameter
classes
classes with parameterless constructors
static fields that are not final
interface that is not implemented or extended
fields/variables read
fields/variables with given name template updated