preface

PHP Storm is a development tool that many Phpers have heard of and even used as a production tool, but many don’t make the most of it. This article summarizes some tips for developing gracefully.

The development environment

The development tools

Before reading this article, I think you should have installed PHP Storm. If not, you can also try to download, install and use it on the download page of the official website. As for the license, it is beyond the scope of this article.

  • Education license
  • GitHub is an active developer
  • Microsoft MVP

Although there are many special ways to activate it, the use of legitimate software is still advocated. Please try to use the original English interface, do not install Chinese patches.

PHP

At this point, I hope you can install the latest PHP distribution (7.3.x) for development and project deployment.

Enter the theme

With that in mind, I hope you’re ready to address some of the issues mentioned above, so we can begin to address some of the issues in development.

Material-Theme-UI

This is my default development interface, using the built-in Themes, Solarized Light. By default, the built-in Themes in the development tool will only change the theme color of areas of the code, such as the index of the project, which will not change (of course, these can be changed manually). Also, the default ICONS are not very recognisable. A Material Theme UI can be installed here

Notice that on the left, in the index of Project, the Project icon has changed significantly, so that when we have different types of files in the same directory, we can quickly identify them by the icon. In addition, common models, util, log, public, vendor, config, static, Middleware and Controller all have special ICONS to improve their identification, which is much better than the default.

It will be added in September 2020

The new Material Theme UI no longer has the Theme icon. If you need it, please install the Atom Material Icons plugin separately

.ignore

When creating a new project, there are some things that do not need to be submitted to the repository, such as the vendor directory in composer, node_modules in Node.js, etc. These directories usually don’t need to be committed to a version control repository, so front-end development will have a lot more variety and we can use this tool to quickly create them.



As you can see, we can create ignore files efficiently, quickly and accurately by selecting the project template.



Also, when we need to manually ignore a directory, we don’t need to manually edit the.gitignore file, we just need to right-click the directory and select the corresponding action.

Other plug-ins

.env files support

This plugin is used to support the env method prompts

PHP composer. json support

For some hints in composer.json





Depending on the framework

  • Laravel
  • ThinkPHP5 Plugin
  • .

Since I’ve only used these two framework related plug-ins, I won’t list the rest. The Laravel plugin is still cumbersome to install, but it is powerful. As for the installation, you can refer to this article

To the Laravel developers

Another feature of the Laravel IDE Helper that is rarely mentioned is thatModel comments.

Everyone knows that Laravel is supportiveORMHowever, when we retrieve a property of the model directly from an object, the IDE may prompt you for an undefined property, but this does not affect usage, it just looks bad during development, as shown below.



An exception has occurred, but we do have this field in the database, not on the object, but because we inherit from the root Model, which has the __get() method, we show it under the idpointTo get our attention, this code has no impact on the runtime of our application, but the development doesn’t look very friendly.

When we use model annotations, the following annotation information is automatically generated for each of our model objects.

I think it’s amazing, but it also helped us bring column comments to MySQL.



Again, the id property is no longer so harsh, but is there any effect? Duang!

Of course, try to select ID and press itCtrl+Q

How do I get to the defined location quickly? Or how is viewing defined?

If you just go to the definition, press Ctrl+๐Ÿ–ฑ left-click or Ctrl+B to go to the definition.

But sometimes we don’t need to go to the definition, we just need to press Ctrl+Shift+I to get the following window, which helps us to see the definition quickly.

Using these two shortcuts, you can query the document of a method or object property, and when you check a method or object that is built-in to PHP, you can also go directly to the PHP document library and view the corresponding document.

Type hinting

A lot of people may not have noticed that type hints are a thing. Like this:

$Collection = $Collection = $Collection = $Collection = $Collection = $Collection = $Collection = $Collection = $Collection = $Collection = $Collection You’ll see that the development tool will automatically prompt us for these types.

This is probably one of the reasons why many people choose to use an IDE for development, but you might think it’s no magic. However, you will find that in some cases, such as anonymous function pass-by-value, this feature does not work because the context is lost and the development tool does not know what type of data it is, so it cannot provide you with more services, such as this.

The value of the array above is a set, why not prompt? The most common is where and association relationships like ThinkPHP and Laravel require an anonymous method to pass an object, as shown below.

Public function article(){// Note that $query does not indicate where ๐Ÿ‘‡ without a type declaration. return $this->hasMany(Article::class)->where(function($query){ $query->where('open',1); })}

PHP is not like the strongly typed language, can only put the same value of the type of an array, and also unable to external know the array from an array of values is what kind of, these strongly typed language like Java, in a statement, the editor can deduce values within the array type, and PHP cannot be inferred, which in turn could not provide type hinting.

All we need to do is explicitly tell the IDE what type this variable is, and the compiler can provide a good type hint.

Yeah, it’s just a little bit more of a note.

/** @var Collection $collection */

This comment is slightly different from the normal multi-line comment. /** This is a phpDoc block that starts with two ** s.

@var data_type $name

@var means that this is a variable, space followed by the type of the variable, and then space followed by the name of the variable.

This syntax also allows you to add types to class member attributes.

In this way the IDE can be very good prompts, assist development, for us to prompt the method, method parameters to achieve the “static check”.

And there’s more to it than that. There are even more interesting rules in PHPDoc.

  • @param declares constraints and specifications for method parameters
  • @return adds a type constraint and specification to the return
  • @throws This is used to tell the IDE that the method throws an exception and can declare more than one.
  • .

If you are interested, you can also go to PHPDoc Reference — PHPDocumentor for more related tags.

Moreover, PHPDoc has been proposed as a FIG-standards /proposed at Master ยท PHPDocumentor/FIG-standards in the PSR.

But, see here, I have to say, in the PHP 7, has done very well for type constraints, in the upcoming release of PHP 7.4, also support to members of the class attribute type constraints, in the future development, we may not need to go through annotation constraint, tell what type of the parameter of development tools, and, by contrast, The code will also be more formal and less buggy.

shortcuts

In daily development, the use of shortcut keys is indispensable. Shortcuts can improve our development efficiency and are also very cool. In addition to the above shortcuts we used, there are some commonly used ones below.

  • CTRL + Y

    Delete the current line. When you press it for the first time, you will be prompted to set the default for this keyundoordelete line. Choose heredelete line
  • ALT+J

    Select the same word. If you’ve ever used Sublime Text, you know that when you’re in Sublime TextCtlr+DWill help you select the same word to help you synchronize the changes.

  • ๐Ÿ–ฑ left?+ Shift + Alt

    Rectangle selection.

  • Alt+Insert

    Fast insertion method. When this shortcut is pressed in a class, a box pops up for us to select, where we can quickly and accurately override/implement methods of the parent class, and quickly generate getters, setters, and constructors for class members.
  • Alt + Enter

    This is a near universal shortcut that can be used anytime, anywhere, to quickly fix problems in the code, correct spelling, etc., when used on the constructor’s parameters, it can quickly change the parameters into class member variables.

  • Shift + F6

    Refactoring. When we need to change a variable name/method name/class name/file name in a context, this key comes in handy. Press this key, the IDE will automatically search for references to it. Synchronization will help you make the changes, this is a versatile rename shortcut. There are, of course, some problems with changing class names. Sometimes, of course, it can work togetherAlt+JTo do it.
  • Ctrl+Shift+Enter

    Completed quickly

  • Ctrl + W

    This shortcut is used to close tabs on many tools, but it is not used here. It is used to select by word or range context, so we no longer need to move the mouse carefully when we need to delete or select something.

  • Ctrl + Shift + C

    Copies the physical path of the file you are currently editing. This is useful when you need to open it with another editor.
  • Ctrl+Shift+ALT+C

    This shortcut is a bit more than the one above, and will work better with the next one.

    When we’re working on a team and sometimes need to share or locate code, your colleague might tell you that the line of code is on line XXX at XX, and you have to find it from the navigation bar on the left. Unwieldy, this shortcut copies the path of the current file relative to the project root path, as well as the line number, for quick positioning.
  • Double-click on the Shift

    This shortcut is very versatile, and in this search box, we can searchclass,file,methodsYou can even enter full namespaces to quickly locate class methods. And the path copied by the shortcut key above to quickly locate the file.

    The top shortcut is copied like

vendor/composer/ClassLoader.php:47

This is something that we can quickly locate to the file and to the line by double-clicking Shift and pasting it in.

  • Ctrl+Shift+F ใ€Ctrl+Shift+R

    Global Search and Replace.
  • Ctrl+Alt+M

    Extract code into methods. Most of the time, we feel that part of a method is more cumbersome, and we need to put forward a method separately. However, if we manually extract it, the efficiency is not too high. This shortcut key can quickly help us extract it out.

  • Alt + ~

    Bring up the version control shortcut.
  • . More shortcuts are not one introduction, interested can go to the official introduction, in many cases, our common software will take up some shortcuts, such as QQ, will take up a formatting code (Ctrl+Alt+L), music software will take upCtrl+Alt+ write/left/please /.

The suffix syntax

The suffix syntax is an interesting application that many people don’t know about. For example, when I’m writing JS, if I want to print an expression, I usually have to manually type console.log, even if prompted, which is time-consuming, and the suffix syntax just needs to follow the expression.log“And then pressTabJust wait for the magic to happen.



And similarly

  • If generates an if statement
  • Notnul generates an if! = null statement
  • Var stores the expression into a variable
  • .

You can find more here

Does it feel like it’s not enough? Don’t worry, you can also customize, for example here we can customize a try

Where $EXPR$is the original expression, let’s try it.

The end of the

This article almost couldn’t be published, because there was no convenient Markdown automatic uploading tool under Windows. After finishing the article, I found that more than 20 pictures were in my local area. Later, I had no other option, so I wrote a script to automatically analyze the pictures in Markdown and upload them to sm.ms. Then I found that it was originally a GIF but turned into a PNG, which was very frustrating. Finally, I used Sharex to retrieve the GIF before I sent the article out.

Although the article doesn’t fully explain how to code, and some of it is a bit cluttered, I hope some of it has been helpful. When we are skilled in using our development tools, in the development process, we can get twice the result with half the effort.

The resources

  • IntelliJIDEA_ReferenceCard.pdf
  • Install laravel plugin laravel-ide-helper – jinhua – blog garden

A revision history

2020.09.14

  • Adds the installation address of the Atom Material Icons plug-in for the theme icon