If you develop PHP with VSCode, use the most powerful IntelliSense tool, intelephense

table of contents
Hello.
I'm Mandai, the Wild team member in charge of development.
I've been working on PHP development all year, and I think it's been a year of trial and error regarding the development environment. Thanks to
that effort, I've been able to set things up quite conveniently, so I thought I'd write an article summarizing it, partly as an inventory of my findings.
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
Since it's an IDE developed for PHP development, I felt that its functionality was excellent, 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
For detailed information, Langserver.org I recommend checking out
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: PHP IntelliSense for Visual Studio Code, this disables VS Code's standard PHP support.
It results in duplicate code completion and nothing good
Incidentally, if PHP IntelliSense, the PHP , is also enabled, you'll get three hints, which significantly increases the annoyance.
Write as many type hints as you can
The more type hinting you write, the better the intelephense checks will work, so you should definitely write as much as you can.
Since PHP7 allows you to write type hinting for return values, let's include 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 does not have type hinting, and without understanding the process, it is impossible to determine what values should be set for the arguments.
Adding type hinting to it results in the following:
/** * 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, specifying the type of the class property will enable code completion.
The following is the code with comments added to enable code 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 use @var to tell the type of the local variable as well, so that completion works.
function superMethod() { /** @var AwesomeService $awesomeService */ $awesomeService = $this->app->make(AwesomeService::class); $awesomeService->awesomeMethod(); }
The basic idea is the same as with class properties, but the key point seems to be putting the variable name after the class name.
Upon closer inspection, the syntax is the same as @param, but the location where you write it is different (before the class definition and within the class), so it took me a little while to figure this out.
As the scale of development increases, method names tend to get longer (this is just my personal opinion), so I want to make sure I don't skimp on 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 launched "SEKARAKU Lab," a service site for the system development company I belong to.
Beyond offers a one-stop service for everything from server design and construction to operation, so please feel free to contact us if you have any problems with server-side development.
SEKARAKU Lab:[https://sekarakulab.beyondjapan.com/](https://sekarakulab.beyondjapan.com/)
That's all
1
