Dealing with incorrect PHP Intelephense errors in your Laravel Project

Dealing with incorrect PHP Intelephense errors in your Laravel Project

ยท

3 min read

Play this article

If you are a PHP developer using VS Code, it's almost a no-brainer to use the PHP Intelephense plugin. It provides excellent IntelliSense for your PHP code and makes life easier.

It prompts you to fix any undefined type, function, constant, class constant, method, or property in your code. Which is great, it's the reason why we want to install it. However, if you are someone like me who doesn't want any red wiggly error/warning lines showing up in your code - it can be a bit problematic sometimes.

Let me give you an example - I was using Laravel Livewire for a project recently and I wanted to pass some parameters to my default AppLayout class from the Livewire component.

I know for a fact that this is a perfectly valid code, the layout() method is there in the documentation. The parameters are also correct and the code works without throwing any errors or exceptions.

But it is annoying to see this every time I open the file. To add more to that now all the files, and folders also appear to be red (containing errors) in my VS Code Explorer.

Apart from my being over-sensitive about this, the practical problem is - I don't know whether the warning in the VS Code Explorer is caused by my stupid code or is it because of the layout() method not being recognized.

I searched and found one possible fix to this problem on Stack Overflow.

There are a couple of solutions in the article :

  1. Use Laravel IDE Helper: https://github.com/barryvdh/laravel-ide-helper

    • This was too heavyweight for me, sure those warnings are annoying but I don't want to install a whole package for that - maybe it is something right for you

    • If my solution does not work properly probably I'll switch to this in the future

  2. Use the _ide_helper.php and use that within your code like it is explained in this Laracast Video

    • I like this approach it's not too heavy and still works

I ended up implementing another solution. Create a helper class that targets specifically the methods which are shown in the warning.

Here is how I did it

  1. Create a new Helper class IntelephenseHelper.php in app\Helpers directory

  2. Added the following code

     <?php
    
     namespace Illuminate\Contracts\View;
    
     use Illuminate\Contracts\Support\Renderable;
    
     interface View extends Renderable
     {
       /** @return static */
       public function layout();
       public function slot();
     }
    

And the warnings were gone!

What it does is quite simple and self-explanatory. It extends the interface and adds the methods which were showing the warnings.

No more red wiggly warnings and no more annoying red file indicators. Life is simpler again.


If you are just starting with Laravel, here is an article I wrote that might be useful for you: How to write clean Controllers in Laravel

I hope you find this valuable, if you did - awesome ๐Ÿ‘Œ share it with your folks if it's relevant to them. If you have any suggestions/comments please feel free.

Happy coding!

ย