Structural search and replace examples
Searching for method calls
The simplest template to search for method calls is $Instance$->$MethodCall$($Arguments$)
. The Find tool window shows the detected method calls. If you need to locate a method call with the specific number of arguments, you can configure the Count filter for the $Arguments$
variable.
To navigate to the method call in the source code, double-click it in the Find tool window. PhpStorm opens the corresponding file in the editor and positions the caret at the method call.
Searching for PHP classes
If you have a PHP class MyClass
:
Then the simplest template to search for it is class $a$
.
Searching for implementations of interfaces
If you have a PHP interface MyInterface
and a class Implementation
that implements it:
Then the simplest template to search for the implementation is class $Class$ implements $SomeInterface$ {}
Searching for descendant classes
If you have a PHP class Parent
and a class Descendant
that extends it:
Then the simplest template to search for Descendant
is class $Class$ extends $AnotherClass$ {}
Searching for statements
The simplest template to search for if statements is if($var$){$code$}
As a result, the detected occurrences will be shown in the Find tool window, double-click the one you are interested in to navigate to the source code. PhpStorm opens the corresponding file in the editor and positions the caret at the statement.
Searching in comments and string literals
The simplest template to find comments or literals containing foo
is: $SomethingWeWantToFind$
or "$SomethingWeWantToFind$"
. To find comments/strings containing some particular words (say, foo as a word), this should be specified as a text constraint.
Examples for HTML and XML
The following examples show how you can use structural search in HTML and XML code.
Searching for XML and HTML tags, attributes, and their values
The simplest template to search for a tag is
<$tag$/>
.By placing constraints on the variable
$tag$
, you can specify tags that you want to find. For example, if you specifyli
, you will allli
tags.Consider the following template for searching in XML and HTML:
<$tag$ $attribute$=$value$ />
. For example, if you specify the text filterid
for the$attribute$
variable and the\d+
regular expression as the text filter for the$value$
variable, you can find all tags that have numeric values in theid
attribute.
Delete all lines that have the id attribute greater than 2
In the Search template field, we create a template that searches for all
li
tags with numeric values (\d+
) inid
attributes. We expand our search to the whole string with such values (Search target = Complete match).We filter those lines with the following Groovy script:
d.getText().replaceAll (/"/, '').toInteger() > 2
. The script reads the content of thed
variable and returns it as a string (for example,"1"
). Then the script replaces all quotes and converts the string value to integer and compares it with2
.In the Replace template field, we put nothing to delete the whole string. After the search, we select Replace All to perform the replacing.
Convert uppercase values of the class attribute in p tags to lowercase
In the Search template field, we create a template that searches for all
p
tags with uppercase values ([A-Z].*
and Match case) inclass
attributes. We narrow our search only to these toclass
values (Search target = b).In the Replace template field, we create a new variable
$d$
and assign a Groovy script to it (b.getText().toLowerCase()
). After the search, we select Replace All to perform the replacing.