If you want to develop PHP with VSCode, use intelephense, the strongest intellisense.

Hello.
I'm Mandai, in charge of Wild on the development team.

I've been working on PHP development this year, and it's been a year of trial and error when it comes to my environment. Thanks to
that, I've been able to set up something quite useful, so I'd like to write an article about it as a way of taking stock of it.

PHP Development with Visual Studio Code

When I first started developing with Visual Studio Code (hereinafter referred to as VSCode), I had some concerns

Actually, before we started development, we were considering introducing PhpStorm, but we gave up on that idea

Because it is an IDE developed for PHP development, I felt that the functionality was perfect, but I just couldn't get used to the GUI.
It's a trivial reason, but since I'll be using it for over a year, I think the appearance is an important point.

That's why I decided to develop using VSCode, but VSCode is just a stylish notepad without extensions, so large-scale development is not possible

There is native PHP support, but it's better than nothing, so I continued to look for extensions that were as powerful as PhpStorm and found intelephense

If you want to know the details, I recommend reading Langserver.org

It's impressive how quickly code completion and code jumping works

Furthermore, during this development, we had carefully written PHPDoc and type hinting, which was beneficial, and it was nice that we were able to get immediate warnings for things like incorrect argument types

Even so, I still had trouble jumping through the code properly, so I tried various things while researching, but there was little information available and I couldn't easily solve the problem, so I would like to share some of the knowledge I have accumulated

 

Make sure the settings are correct

GitHub - bmewburn/vscode-intelephense: As described in PHP intellisense for Visual Studio Code , this disables VSCode's standard PHP support.

It results in duplicate code completion and nothing good

PHP IntelliSense , which is the same PHP intellisense , is also enabled, three hints will appear, which makes it much more annoying.

 

Write as many type hints as you can

The more type hinting you write, the better the intelephense checks will work, so it's a good idea to write as many as you can.
Starting with PHP7, you can write type hinting for return values, so let's write that as well.

/** * Without type hinting */ function square($number) { if (is_array($number)) { $sum = 0; foreach ($number as $n) { $sum+= $n; } return $sum; } return null; }

 

The code above is without type hinting, but you cannot determine what value to set for the argument unless you understand the processing.
If you add type hinting to this, it will look like this:

/** * With type hinting */ function square(array $number): ?float { if (is_array($number)) { $sum = 0; foreach ($number as $n) { $sum+= $n; } return $sum; } return null; }

 

This is a rather extreme example, but with type hinting, you can determine the argument type without reading the contents, and it is immediately obvious what value will be returned

Intellephense displays the method declaration and the PHPDoc written just before the method, which is convenient as you don't have to check each one

 

Properly type class properties

Here is the beginning of a unit test for the AwesomeService class:

class AwesomeServiceTest { protected $awesomeService; public function setUp() { $this->awesomeService = $this->app->make(AwesomeService::class); } // The rest omitted }

 

We will create a new AwesomeService class for each test to prevent it from being affected by other tests. In this case, we will write many methods like $this->awesomeService->awesomeMethod(), so this is where code completion comes in handy. Unfortunately, completion does not work in the above code

In this case, you can enable completion by specifying the type of the class property.
The following code adds a comment to enable completion.

class AwesomeServiceTest { /** @var AwesomeService */ protected $awesomeService; public function setUp() { $this->awesomeService = $this->app->make(AwesomeService::class); } // The following omitted }

 

It seems that completion works in a similar way in PhpStorm and other programs, so this is a nice comment that will make everyone happy regardless of the environment

 

Local variables are also properly typed

Although I was close to the core of the issue, I was unable to get to it, which was specifying the type of local variables. However, I think it would be easier to understand it if you just look at the code, so here is a sample

function superMethod() { $awesomeService = $this->app->make(AwesomeService::class); $awesomeService->awesomeMethod(); }

 

In this case, intelephense cannot complete awesomeMethod() because it does not know the type of $awesomeService .
Therefore, we will use @var to tell the type of local variables so that completion will work.

function superMethod() { /** @var AwesomeService $awesomeService */ $awesomeService = $this->app->make(AwesomeService::class); $awesomeService->awesomeMethod(); }

 

The basic concept is the same as class properties, but the key seems to be to put the variable name after the class name.
If you think about it, it's written in the same way as @param, but it took me a while to get to this point because it's written before the class definition and in the class itself, which is different.
As the development scale grows, method names tend to become longer (this is my personal opinion), so I want to make sure to take this extra step.

 

summary

This time we learned how to use intelephense, a powerful tool for PHP development in VSCode

Although this is an extension that I personally like, when it comes to information about extensions for PHP development, it can be much more difficult to gather information, so I have compiled some useful information on how to use it in the hopes of lowering the barrier to entry even a little

I've written a number of tips to make development easier, but ultimately it comes down to getting into the habit of writing documentation in your code on a daily basis

I'll add more if I find any other useful features

lastly

I have opened the system development service site "SEKARAKU Lab" to which I belong.
Beyond is a one-stop service for everything from server design and construction to operation, so if you have any trouble with server-side development, please feel free to contact us.
SEKARAKU Lab: [https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)

That's it.

If you found this article helpful , please give it a like!
1
Loading...
1 vote, average: 1.00 / 11
74,478
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.