6

How to achieve autocompletion for Craft 3 for PHP inside Visual Studio Code? I mean, what is required (like installing PHP locally, folder structure etc.) if at all possible?

I assume many people who develop PHP are using PHPStorm for this. Also as I recall there are even instructions on how to get autocompletion for Craft 3 inside PHPStorm here: link. As I'm trying to build some plugins I would prefer setting up an environment that works for me with autocompletion without having to look up everything and playing the guessing game. Out of preference I would like to use Visual Studio Code for this, so currently I'm somewhat stuck as I don't know if it is possible to get a similar kind of autocompletion like in PHPStorm inside Visual Studio Code for Craft 3 and also how to achieve it. If someone could point me in a direction, I would be more than happy!

1 Answer 1

2

Old question I know, but for the sake of having an answer here when people come here from Google...

You need the Intelephense plugin for VS Code. I'd also recommend looking at this question on Github in order to get autocomplete working: https://github.com/craftcms/cms/issues/10109

In a nutshell:

Add a file called vscode_fix.sh to the root of your craft cms repo. Add the execute permission to it chmod +x vscode_fix.sh. Add the following to it:

#!/bin/bash

# Because vscode.
# https://github.com/craftcms/cms/issues/10109

if ! grep -qF '@property \craft\web\Application|\craft\console\Application $app' vendor/craftcms/cms/src/Craft.php; then
  sed -i.bak 's/ \* @mixin CraftTrait/ \* @mixin CraftTrait\n \* @property \\craft\\web\\Application|\\craft\\console\\Application \$app/' vendor/craftcms/cms/src/Craft.php && rm vendor/craftcms/cms/src/Craft.php.bak
fi

Then, add the following script to your composer.json:

"post-autoload-dump": [
    "./vscode_fix.sh"
]

After running composer update the autoload will be generated and code completion should work.

This is, just as the above github link says, quite hacky. However since I'm not a fan of phpstorm I chose this route instead of learning to like a new IDE (that I have tried to like many times...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.