If you want to develop PHP with VSCode, use intelephense, the strongest intellisense.
table of contents
Hello.
I'm Mandai, in charge of Wild on the development team.
I've been developing PHP all this year, and I think it's been a year of trial and error regarding the environment.
It was worth it, and I was able to maintain it quite conveniently, so I would like to summarize it in an article that also includes the meaning of inventory.
PHP development with Visual Studio Code
In the early days, I had some concerns about developing with Visual Studio Code (hereinafter referred to as VSCode).
Actually, I was considering introducing PhpStorm before starting development, but I gave up.
It's an IDE developed for PHP development, so I felt it was perfect in terms of functionality, but I just couldn't help but like the GUI.
It's such a trivial reason, but since we've been together for over a year, I think it's important that people look at me.
That's why I decided to develop with VSCode, but large-scale development is not possible with VSCode, which is a fancy notepad without extensions.
There is native PHP support, but it's better than nothing, so I was looking for a powerful extension that could come close to PhpStorm, and I found intelephense.
more about the details on Langserver.org , but what I liked about it was how effective IntelliSense was.
It's impressive how quickly the code completion and code jumps work.
Furthermore, in this development, I was able to write PHPDoc and type hinting properly, and it was a good thing that I was able to get a warning immediately when there was a type difference in the argument.
Still, I was having trouble jumping the code properly, so I did some research, but there wasn't much information and I couldn't find a solution, so I'd like to share some of the knowledge I've accumulated.
Make sure the settings are correct
GitHub - bmewburn/vscode-intelephense: Disables VSCode's standard PHP support, as described in PHP intellisense for Visual Studio Code.
Code completion is displayed twice and there is nothing good about it.
PHP IntelliSense which is the same PHP intellisense , is also enabled, three hints will appear, making it even more annoying.
Write as much type hinting as you can.
The more I write type hinting, the more intelephense checks will work, so I would like to write more and more.
Starting with PHP7, it is now possible to write type hinting for return values, so let's write this 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 above code is without type hinting, but unless you understand the process, you can't determine what value to set in the argument.
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 pretty extreme example, but if you have type hinting, you can tell the type of the argument without reading the contents, and you can see at a glance what kind of value will be returned.
intelephense also displays the declaration part of the method and the PHPDoc written just before the method, which is convenient because you don't have to check it every time.
Properly type class properties
Below is the beginning of the 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 recreate the AwesomeService class for each test so that it will not be affected by other tests, but in this case we will have to write a lot of methods like $this->awesomeService->awesomeMethod(). , This is where code completion comes into play, but unfortunately in the state of the code above, completion doesn't work.
In this case, completion becomes effective by specifying the type for the class property.
The following is a comment added so that the completion works.
class AwesomeServiceTest { /** @var AwesomeService */ protected $awesomeService; public function setUp() { $this->awesomeService = $this->app->make(AwesomeService::class); } // The following omitted }
PhpStorm etc. seem to have a similar mechanism for completion, so this is a nice comment that will make everyone happy regardless of the environment.
Properly type local variables
One thing I was getting close to getting to the core of the lesson, but couldn't get to was specifying types for local variables, but I think it would be faster to have someone look at the code, so please take a look at the sample.
function superMethod() { $awesomeService = $this->app->make(AwesomeService::class); $awesomeService->awesomeMethod(); }
In this case, intelephense does not know the type of $awesomeService and cannot complete awesomeMethod().
Therefore, by using @var to tell the type of local variables, completion will work.
function superMethod() { /** @var AwesomeService $awesomeService */ $awesomeService = $this->app->make(AwesomeService::class); $awesomeService->awesomeMethod(); }
The basic idea 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 carefully, it's written in the same way as @param, but it's written in different places before the class definition and inside the class, so it took a while to get to this point.
As the scale of development increases, method names tend to become longer (this is my personal opinion), so I would like to make sure that this extra effort is spared.
summary
This time we learned how to use intelephense, a powerful tool for PHP development with VSCode.
Personally, I like this extension, but when it comes to information on extensions for PHP development, it becomes much more difficult to gather information, so I thought it would be great if I could lower the barrier to introduction even just a little, so I would like to share with you some convenient ways to use it. I have summarized the information.
I've written down various tips to make development easier, but in the end, I've tried to get into the habit of writing documentation in my code on a daily basis.
If I find any useful functions, I will add them.
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.