Configuring coc-phpls and intelephense for Drupal 7

23. December 2020
vim php coc

Since the release of Drupal 7 in 2011, a lot happened in the PHP world. Composer came to live as the now go-to-package-manager, Language Servers became a thing and PHP itself made great progress as a language.

So it is no wonder, that a modern development environment will not work out of the box with such an old architecture. The solution to this will be shown in this post.

Starting point

I'm using neovim with coc.nvim as my LSP-Client. For PHP I'm using coc-phpls, which uses intelephense as a LSP-Server. Normally this works great, but not with Drupal 7. Due to the architecture, intelephense is not able to find all symbols out-of-the-box, so we have to tweak the settings a litte.

Configuration

We will use a local config for coc. Start by issuing :CocLocalConfig. If executing this for the first time, coc will ask you if it should create the local config folder. Hit y to confirm. Now we can enter the following:

{
  "intelephense.environment.documentRoot": "/home/user/git/drupal",
  "intelephense.environment.includePaths": ["/home/user/git/drupal/includes"],
  "intelephense.files.associations": ["*.php", "*.phtml", "*.module", "*.inc"]
}

The first setting tells the documentRoot of your project and the second one tells explicitly to use the includes folders. Using absolute paths is useful if you develop modules in some subfolder of the project. The most important setting is the last one. Drupal makes heavy usage of the file extension .inc for it's php files. Intellephense does not recognize these files as PHP by default, so we have to explicitly say so.

This should be enough for you to get started. Happy coding.