📜 ⬆️ ⬇️

Productive use of PHPStorm

In the process of using IDE sometimes by chance, sometimes there are specially useful chips, keyboard shortcuts and other nice things laid by developers that never settle into memory without regularly using them and escape in every possible way when reading manuals.

Without claiming a bible or a “programmer’s desktop article,” I want to share useful finds in my favorite IDE, without slipping into a blunt copy-paste of manuals and boring hot-key lists, only what I use all the time and what my colleagues are surprised at: “oh! is that allowed?"

Disclaimer
')
If none of the chips described in the article were new to you, take the trouble to add at least one of your own, then the world will be better, the trees will be greener ... well, you understand.

I work on a Mac, so I’m pointing to hotkeys in “my” layout. Most of them will work by simply replacing [CMD] with [CTRL] or [ALT], the rest may have to be searched on the Keymap tab. I would be grateful if someone checks and duplicates the layout for WIN.

Auto-generate code


There is a bunch of routine tasks that are sometimes performed by the framework itself, and sometimes require stupid manual work. PHPStorm helps simplify some [CTRL] + N , for example:

Getters & Setters (Getters & Setters)
For existing class variables, a set of getters and \ or setters can be generated, and in the window that appears, you can choose which ones.

Phpdoc creation
In essence, the action is the same as if, before the existing method, to start writing / ** and pressing Enter. You can select one or more functions and a Doc Block will be formed based on the attributes and contents.

Reassignment and implementation at inheritance (Override & Implementation)
When creating a descendant class or implementing abstract methods, in order to avoid a mistake, you usually have to climb into the ancestor class and memorize or copy-paste the name and attributes of the method. Or to check if there is an implementation for all abstract methods and remember what is required for ArrayAccess. There is a better option:
[CMD] + I - Implementation
[CMD] + O - Reassignment
After pressing the hotkey, a window appears where you can choose which methods we will implement. After selection, the corresponding method is generated according to privacy and attribute names, like this:
public function createTable($drop = false) { return parent::createTable($drop); } 


Auto Format Code


When you write the code yourself, usually the formatting turns out on its own, but in a situation where you need to change the Code Style or try to open someone else's unusually designed code, problems arise. You can accept, or you can press [CMD] + [ALT] + L and IDE will format the code according to the settings (Preferences -> Project Settings -> Code Style -> PHP)

By default, pressing the hotkey formats the entire file. If you select a region, only the selection is formatted. If you select a folder or entire project in the tree, you can immediately put everything in order at once.

For each language, you can set your own style of design, and for PHP you can also choose one of the predefined styles:




Code Completition Tips


An important function of the IDE is the ability to prompt the names and attributes of methods. This is especially important for the PLO, when chains of 3-4 inherited classes are capable of confusing anyone.

With the normal architecture of the project, the IDE is able to figure it out, but there are situations when it does not cope, for example:


Solution options:



For functions, you need to write PHPDoc with the @ return parameter. For example:
 /** @return SomeClass */ function a() {[…]} 


For variables , you can also specify a comment:
 /** @var $my_var SomeClass */ 

Then hints will be available for $ my_var below.

For methods above class, you can specify phpdoc
 /** @method B foo()   */ class B extends A {[…]} 


If the function returns an array of objects
 /** @return SomeObject[] */ 

Then, for example, prompts of $ value-> ... (by fields and methods of the class SomeClass) are available in foreach. Thanks pro100tak

Tasks


The use of version control systems has long been a sign of good tone. But often there is a problem when work is being done on several tasks in parallel or someone is resorting and urgently asks to fix this bug right here and now. It is very easy to forget to commit the required file or vice versa, extra files get into the commit or changes that should not be there.

To avoid this and bring order to work, you can use Tasks. The bottom line is that each task has its own Change-list, in which the files with changes are indicated and switching between tasks immediately shows which files worked on this task and what you need not to forget.



Another positive thing is that the IDE warns you if you are in one task starting to edit a file already touched in another.

It adds to the convenience that during a commit, all files from the current change list automatically fall into it + the task name is substituted into the comment.

Well, if you already have a bugtracker like Jira or Redmine, if not, on the same GitHub there is a fairly simple bug tracking system, also supported by PHPStorm. This allows you to load the number, name and description of an existing task for which you work directly in the IDE, although nothing prevents you from using the tracker to create arbitrary change sheets.

FTP, deployment and autodeploy


With many projects, work is being done via FTP or SFTP. PHPStorm has convenient functionality for working with them. After saving access to the server and configuring the mapping, it is possible to upload and download (upload & download) files to the server by pressing one hotkey. For this to work, you need to remember to click “use this server as default”



For some reason, the Macs do not have default hotkeys for these actions, but you can set them in Keymap (thanks to Cap). I set the following:
• Upload to Default Server: [CTRL] + U
• Download from Default Server: [CTRL] + D
(brought them for the convenience of searching in Keymap + in my opinion is easily remembered)

But about this all would not be worth writing, if not for the function of auto-heating. Putting in the settings (Preferences -> Project Settings -> Deployment -> Options) point “Upload changed files automatically to default server” in any mode except Never + checkbox External changes, we get automatic synchronization of all changes.

This is due to the convenience of the fact that when you press [CMD] + S, PHPStorm saves all files at once, because as a result, the save procedure and the upload are combined into one.

Now you can not care whether everything is loaded and nothing is forgotten.

Console


Many frameworks use the console to quickly perform frequent actions. For symfony, Zend and other popular frameworks, PHPStorm supports hints and auto-completion of console commands in the Command Line Tools Console [CMD] + [SHIFT] + X

But nothing prevents you from doing the same for your project or frequently used UNIX or GIT commands. To do this, you need to select an executable file and create a special XML file of the form:

 <?xml version="1.0" encoding="UTF-8"?> <framework xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="schemas/frameworkDescriptionVersion1.1.2.xsd" name="My Tools" invoke="/full/path/to/console.php" alias="c" enabled="true" version="2"> <command> <name>command-name</name> <help> .     !</help> <params>class</params> </command> <command> <name>another:command</name> <help>    </help> <params>class[=null]</params> </command> </framework> 


A detailed description of how this is done here: devnet.jetbrains.net/docs/DOC-1230

Varieties of search


[CMD] + N - Find by Class Name

Search by class name. Conveniently in large projects, when everything is buried deep into directories or where it is just unknown. Destruction adds "smart search", because it is searched both in terms of the class name and in the first letters, for example, SomeVeryLongClass will be found by “SVLC”. Considers the case of letters, be careful.



UPD: “Smart Search” developers call Camel's Hump. By the way, in the settings you can make the selection of words, as well as the movement was not entirely (up to a space), but according to the words (up to a capital letter). Thank you, yujin1st

[CMD] + [SHIFT] + N - Find by File Name

Search by file name. Similarly, searching by class name uses “smart search”.

UPD: To find a file by nesting, you can use a slash: some / deep / dir / with / file.txt can be found in “s / d / d / w / f”, thanks develop7

[CMD] + [SHIFT] + [ALT] + N - Find Symbol

The search for any function / method and constants is very convenient when you remember the exact name of the method, but the class does not. Thank you conf

[ALT] + [F7] - Find Usages

When the cursor is on the name of a class or method, pressing a hotkey allows you to find all uses in a project or a specific part (Scope).

There is a variant [CTRL] + [ALT] + [F7] - the same search by usage, but in the form of a context menu. Thank you iPavel

[CTRL] + [SHIFT] + F - Find in Path

Search for arbitrary phrases in the project files. When refactoring, it usually complements Find Usages well.

It would not be worthwhile to write about “just search” if it were not for the possibility to limit the directory in which the necessary things would be searched for, or to configure Scope. Scope allows you to flexibly customize and save the search area for future use. You can throw out, for example, framework files or third-party files from the search.



Remove the frame with captions tabs


In the lower left corner there is a button that allows you to remove the frame for the side and bottom tabs. The tabs themselves can be opened by combinations [CMD] + 0-9, but some tabs do not have numbers. In this case, help double-click (double-click) [CMD] , which for the holding time shows a frame with buttons into which you can poke the mouse.



Productivity Guide



In the menu Help-> Productivity Guide there is an opportunity to see a small statistic about yourself, as well as a list of features with statistics of their use. Thanks nxn



Comparing two files



When two arbitrary files are selected in the file manager, the “Compare Two Files” item appears, which shows them in the built-in diff-viewer and allows you to transfer changes from one file to another. Thanks pro100tak

Tags in the project


With [CTRL] + [SHIFT] + 0 ... 9 you can leave tags. You can return to the label with [CTRL] + 0 ... 9 . Works in the context of the entire project. Thank ualinker

Interesting hotkeys from comments



[CMD] + [ALT] + T (WIN: CTRL + ALT + T)
The environment of the selection with predefined things, such as tags or if-else, depending on the context. Thank you happyproff

[CMD] + [F12] (WIN: CTRL + F12)
The structure of the current class with the search and optional display of inherited members
Analogue of the Structure tab, only in a pop-up window. Thank you happyproff

[ALT] + [Home] - quick navigation on the path to the files. Thank develop7


[CMD] + [SHIFT] + A - search by IDE commands

Alt + ~ - menu with git commands. Thank you denver

Links


Presentation on a similar topic , thanks Aivean

About refactoring in PHP Storm , Thank you fTR

Source: https://habr.com/ru/post/157409/


All Articles