PhpStorm 2017.3 Help

Structural Search and Replace Examples

On this page:

Method call

$Instance$.$MethodCall$($Arguments$)

This template matches method call expressions. If the number of occurrences is zero, it means that a method call can be omitted.

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. When you select one of the detected calls, the Structure tool window shows the structure if the file that contains the selected occurrence.

To navigate to this 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 cursor at the method call.

ps_structural_search_method_call.png

Searching for PHP classes

If you have a PHP class MyClass:

class MyClass { }
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:

class Implementation implements MyInterface }
the simplest template to search for the implementation is class $Class$ implements $SomeInterface$ {}

Searching for descendants

If you have a PHP class Parent and a class Descendant that extends it:

class Descendant extends Parent{ }
the simplest template to search for Descendant is class $Class$ extends $AnotherClass$ {}

Finding statements

The simplest template to search for if statements is if(empty($var$)){$code$}

ps_structural_search_if_statement.png
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 cursor at the statement.

Replacing statements with try/catch/finally constructs

To replace a statement with a try/catch/finally construct, the following pair of search and replace templates can be suggested:

  • The search template is $Statements$ with a certain maximum number of occurrences specified as a constraint.
  • The replacements template is:
    try { $Statements$ } catch(Exception ex) { }

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.

Searching for XML and HTML tags, attributes, and their values

The simplest template to search for a tag is <$a$/>

By placing constraints on the variable $a$, you can specify which tags you want to find. For example, if you specify the text/regexp constraint app.+, you’ll find the tags whose names start with app.

A more versatile template for searching in XML and HTML is <$tag$ $attribute$="$value$"/> By using this template with properly specified search settings and constraints, you can find practically anything that may occur in XML or HTML. For example, if you specify the text/regexp constraint width for the variable $attribute$, you’ll find all the tags that have the width attribute.

Last modified: 6 December 2017

See Also