[JAVA] Write solidly with PhpStorm

Introduction

PHP Advent Calendar 2016 This is the article on the 20th day.

The article Writing robust code in PHP7 has become a hot topic. I've only seen the slides, but there were a lot of things I sympathized with. With that in mind, I tried to sort out what I pay attention to and what I do when developing with php.

Originally I had a long experience with Java and used IntelliJ IDEA, so I am developing with PhpStorm from the same place. It is a story that you can write code with a feeling similar to Java to some extent by using that function.

Documentation comment that specifies what type the variable is

When you take it out of a symfony container, you may be in trouble because you don't know the type and which method. In such a case, if you comment on the local variable as shown below, PhpStorm will recognize the type of the variable and will complete the method etc.

It is a quote from http://symfony.com/doc/current/service_container.html, but if it is left as below, the send method of $ mailer will not be completed.

class HelloController extends Controller
{
    // ...

    public function sendEmailAction()
    {
       
        $mailer = $this->get('app.mailer');
        $mailer->send('[email protected]', ...);← I don't know the type, so if it's PhpStorm, a warn will appear.
    }
}

If you do the following, the warn will disappear and the method of the Mailer class will appear as a completion candidate in $ mailer.

/* @var $mailer AppBundle\Mailer */
$mailer = $this->get('app.mailer');

How to add documentation comments for typed arrays in PhpStorm

For example, when you want to describe an array with a specified type such as "array of character strings" or "array of classes you created" in phpdoc. If you're new to Java, you'll want it.


* @return string[]
* @return Sample[]

You can do it with XXX [].

List where methods are used in Find usages

Alt + F7 (Find usages in the right-click menu) will search for and list where a method is called. This is useful for finding out the extent of the impact of fixing a method and how it is used.

public function isXXX() {...}← Focus on the method name, Alt+F7 or right click

Caution If it is used on a template such as twig, or if PhpStorm is used in a way that the type cannot be determined (such as when getting it with a symfony service container), it will not appear in the search results. Such cases can only be picked up by a character string search in the project. Ctrl + Shift + F will bring up that dialog.

Take advantage of documentation comments such as @ see and @ deprecated

When modifying or adding functions, there are cases where old and new classes coexist due to man-hours and specifications (ex: redirecting to a new URL while leaving the old routing). At that time, one solution is to add @deprecated to make it easier to understand the old and new. In addition, you can use @see to create a route that references a new class. Focus on the class name at @ see and press Ctl + B (shortcut for calling the declaration part) to open the class.

@deprecated old factory class
@see NewFactory new factory class
public class OldFactory {
}

Since @deprecated can be attached to the method, I made it ad hoc and unavoidably, but it seems that it can also be used to appeal that I do not want it to be used elsewhere.

@deprecated Please do not use it in new development as it has too many side effects and it is dangerous to use it any more.
public function dirty_method() {...}

However, in any case, it is a major premise that the team has a good understanding of how to use these and what kind of comments to leave.

View git blame and commit history

Open the file you want to see blame or history, right-click> Git> Annnotate to see git blame, Show History to see a list of commit history so far.

git_annnotate.png

It is useful when you want to get information from the commit comment about which ticket the process was entered in and what the process is for, or to see who wrote it.

Like GitHub, you can also go from blame to the diff of the commit, which is useful if you don't have a git repository browser or just want something a little.

Complement PECL methods with PhpStorm

https://github.com/JetBrains/phpstorm-Download the stubs and go to the standard directory,Preferences | Languages & Frameworks | PHP | Include path Added to.

Some warn comes out because it conflicts with the original one, but if you don't mind, somehow.

Comparison problem

It's a worn-out story, but I've been trapped many times in this regard, so I can't organize it. It is a story that unexpected results are obtained due to loose comparison by ==. By default of PhpStorm, warn appears when comparing with ==, so it is easy to notice to some extent, but it fits when it fits, so if you know the pattern of the problem, it will be the cause when you come across it It's easy to reach.

2a problem

It seems to be so famous that it gets super.

$a = '2a';
$b = 2;
print ($a == $b);
// =>  true 

As mentioned above, it is a phenomenon that the character string 2a and the number 2 are the same. This can be avoided by making a strict comparison including the type with ===.

0e problem

It seems to be famous for comparing hash values of md5.

$a = '0e678342453'
$b = '0e123435465'
print ($a == $b);
// => true

It is a phenomenon that the character strings look completely different, but they are the same. What's happening is that it's interpreted as exponential notation because of the e.

Since 0 ^ 673 = 0 = 0 ^ 123, both are 0, so it is the same as a loose comparison. This can also be avoided by comparing with ===.

switch statement

Since the comparison of switch statements in case is ==, it is easy to get stuck in the above problem. The following articles are well organized, so please refer to them. Six things that PHPer should know about switch statements

Reference site

Recommended Posts

Write solidly with PhpStorm
Write tests with Minitest
Write a Reactive server with Micronaut
symfony / http-kernel with phpstorm & docker phpunit & xdebug
Write tests for JavaFX applications with TestFX