PHPStorm - Tips and Tricks for Magento developers

PHPStorm - Tips and Tricks for Magento developers

If you’re still looking for what IDE to use for PHP development, you’ve just found it. It’s PHPStorm period. Take this from a guy that tried a lot of them before finding the right one.

I tried Eclipse, Aptana and NetBeans. They are OK, but they cannot compare with the power PHPStorm has. I even tried the new ones like Sublime, Atom and Brackets. They look nice, but they can’t handle complex projects with a lot of files like Magento. None of them come close to what PHPStorm can do.

In this article I will present tips and tricks that I found using PHPStorm that will help you.

l will not present all the features that PHPStorm has. If you want to find out more about PHPStorm, you can check out their website.

If you like the appearance of a darker IDE, you will love the Darcula theme (I also use this one). You can choose this during the install process or later in the settings (Appearance & Behavior > Appearance > Theme).

A big part that will speed up your development is better navigation into the code base.

Navigation: Search everywhere (Shift+Shift)

This is the quickest way to search for files, classes or see recent open files. Press Shift two times and a dialog will pop up that will let you search for everything. The cool part of this is that you can search classes with only part of the name. If, for example, you are looking for “Mage_Catalog_Model_Product”, you can type in there “MCMP” or “MCatMPro” (if you want to narrow down the filter without typing all of the class name). This CamelCase search functionality is present in a lot of places where you need to filter stuff.

search_everywhere.png

Navigation: Jump to declaration and back

Using Ctrl+Click on classes/methods names to jump to declaration is the way I investigate code all the time. A nice shortcut that I use to come back to the place I left is Ctrl+Alt+Left.

On my Linux Mint, I had problems with the Ctrl+Alt+(Left or Right) shortcut, which is used to change workspaces. To resolve this, you can change the shortcut from PHPStorm Settings - Keymap tab - Main menu > Navigation > Back/Forward section or select “Default for GNOME” or “Default for KDE” as keymap.

On Windows, most video drivers have this shortcut used to flip the image left or right. Press Ctrl+Alt+Up, then go to the video card software and disable this general shortcut from there.

If you had problems trying to jump to a method declaration from Mage::helper('core') or Mage::getModel('catalog/product') using Ctrl+Click and it did not work, this is because PHPStorm does not know what classes this code referrs to. A quick fix for this is to use the @var PHPDoc comment like in the following examples. Another fix is using the shell script Vinai/phpstorm-magento-mapper. You can check the step by step guide on how you can use this script in our article "PHPStorm - Improve autocompletion for Magento".

In the template files, you can write the @var PHPDoc comment for $this variable like this:

/** @var Mage_Catalog_Block_Product_List $this */

It will help you and other frontend developers know what available methods that block has.

add_var_phpdoc_into_template.png

In order to find the class name that should be entered in @var PHPDoc comment, you can use CamelCase filtering (type the first letters from the class name) and PHPStorm will show a list of classes that you can choose from.

CamelCase_in_class_name.png

CamelCase filtering can be used for method autocomplete as well.

method_calls_autocomplate.png

Clipboard history

PHPStorm keeps track of your clipboard content and has a list of 5 latest copy entries. Try pressing Ctrl+Shift+V. The number of entries can be changed in settings.

compare_to_clipboard.png

Compare with clipboard

There are cases when someone extends a class and overwrites the whole method and you want to see what changes have been made in that method. You can copy the parent method code into clipboard, select the new method code and right click > Compare to clipboard.

compare_with_clipboard.png

PHPStorm is also very good at comparing folders and files. If you configure a deploy server, you can compare and synchronize files and folders between the local environment and the server. In order to setup deploy servers, you have to go to Tools > Deployment > Configuration.

Create multiple folders

If you’ve ever found yourself creating folder after folder to replicate the same path from a template file, do that no more. Right Click > New >  File/Directory and, if you put a path in there, PHPStorm will automatically create the folders according to that path.

create_multiple_folders.png

Structure view

The structure shows functions in PHP or JavaScript, but it’s also very useful in other languages. The cool part is that it also works in JSON, XML, HTML and even on DB tables. It can be very useful if you want to navigate within XML files (we have a lot of them in Magento).

structure_view_XML.png

Find by XPath

Because Magento has a lot of configuration stored within XML files, this functionality is very useful. The place where you find it is hidden in the menu Edit > Find > Find by XPath. The shortcut for this is also hard to remember - Ctrl+Alt+X,F.

Examples of XPaths that I found useful:

/config/*/events/*
/config/frontend/events/*
/config/crontab/jobs/*
/config/*/routers/*/args/frontName
/config/*/layout/updates/*/file
/config/global/template/email/*

find_by_xpath.png

Database integration

For debugging or executing quick MySQL queries, I used HeidiSQL in the past. But I gave PHPStorm a try and I was happy I did. You can connect to any accessible server by even using SSH tunnel. To access MySQL or any other databases from PHPStorm, you will have to find a section on the right called “Database”. Drag it on the left, under the “Structure” section (This will help you access it in the future). Click the green + > Datasource > MySQL. There, you can configure your databases. You will have to download a MySQL driver using the link from there.

Once you have configured your database server, you will see a list of your databases and tables. Click inside the Database section and start tapping the table you are looking for (you can use CamelCase search in here as well). Type ‘ccd’ for example, then press down arrow two times. Using down arrow, you can go to the next entry that matches your search.
Furthermore, this type search works in a lot of sections like Project, Structure, Git Branches etc. 

database_integration_1.png

To make custom SQL, you have to click the green + > Console File. One cool feature is that you can use CamelCase filtering when you search for table names. Another cool feature is that a Ctrl+Click on a table or column name from an SQL will go to the Database section and select it. Also, if you select a column or a table and press Ctrl+C, the table/column name will be copied into your clipboard.

database_integration_2.png

If you know you did a script before and want to find it again, you can go to the Project section and click on  Scratches > DB Consoles. All the console files that you did are there. You can even access local history of all changes by Right Click > Local History > Show History.

Git Integration

PHPStorm knows how to work with Git. If you have it configured on a project, it will automatically recognize it. If not, go to File > Settings > Version Control > Git and click Test button. If the test does not show a green success message with the Git version, this means that Git is not installed on your operating system and you have to install it manually or select the correct path to the Git executable file.

Note: Do not commit files from the “.idea” folder. That is the default PHPStorm project configuration folder.

Once you configure it, you will see the current Git branch in the bottom-right corner from where you can manage Git branches (create new branch, checkout, merge, compare, delete). There will also be a section called “Version Control” at the bottom. That section will have “Local Changes” and “Log” tabs.

“Local Changes” has files that you have edited. You can create “Changelists” by clicking the green + button. This way, you can separate work done on separate tasks or local changes of files that you want to keep only locally.

git_local_changes.png

Some changes you can save for later and you can do that with Right+Click on a Changelist > Shelve Changes and give it a name. This is similar to Git stash, but it actually saves a patch file that is accessible in a new tab called “Shelf”. From that, you can Unshelve or Show diff the changes. This is a quick way to save some work and continue to do other stuff on Git without worrying about your work that has not been committed yet.

The “Log” tab shows the Git graph with all the commits.

git_log_section.png

Git: Compare with Branch

One of the best features of Git integration is the ability to compare a file or a folder with the same file/folder from a different branch. To do this, Right Click > Git > Compare with Branch...

 git_comapre_with_branch.png

Note that, when you make changes, the edited lines will be marked with green (new lines), blue (changed lines) or an arrow/grey (deleted lines) in the left section near the line numbers. From there, you can compare or revert each group of changes with only one click.

Git : Resolve Conflicts

The best way to resolve Git conflicts is by using PHPStorm period. Once you have a conflict, a section will pop up asking you to resolve it. If you did the merge from the terminal or other programs, you have to go to menu VCS > Git > Resolve Conflicts… The “Files Merged with Conflicts” popup window will show all the files that have conflicts. You have three options there: “Accept Yours”, “Accept Theirs” or “Merge”. The merge option will show a 3-way compare window that will let you select line by line the changes that you think should end up in the final version of the file.

git_resolve_conflicts.png

Note: The keyboard shortcuts presented in this article are based on the Default keymap.

Let me know what you think after using these tips or, if you know other cool tricks that could be useful to all of us, leave them in the comments below.


NO COMMENTS

Tell us what you think

Fields marked with " * " are mandatory.

We use cookies to offer you the best experience on our website. Learn more

Got it