24

I'm using Visual Studio Code to develop in PHP, and I've been having some trouble getting Code to provide the proper intellisense results. For example, this newly created Codeception unit test:

<?php

class MyTest extends \Codeception\Test\Unit
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
    }

    protected function _after()
    {
    }

    // tests
    public function testSomeFeature()
    {
        $this->assertFalse(false);
    }
}

When I type $this-> I expect to see assertFalse, assertTrue, and all the other methods provided by \Codeception\Test\Unit. But what I get is basically whatever items exist within the current file and that's it.

incomplete intellisense

What can I do to get all the methods from the Unit class to show up? I already have the PHP IntelliSense extension installed, v2.3.4.

Gama11
  • 31,714
  • 9
  • 78
  • 100
Jimmy
  • 2,805
  • 6
  • 43
  • 57
  • 5
    Visual Studio Code does not include a language server for PHP by default. You need to install a third-party extension. I'd recommend **PHP Intelephense**. – Álvaro González Aug 26 '18 at 12:49
  • @ÁlvaroGonzález thank you for the suggestion. Would you recommend removing the PHP IntelliSense extension before adding that one? – Jimmy Aug 27 '18 at 00:46
  • Oh, I didn't know you already had a PHP extension. You certainly shouldn't have both at the same time because you'll get duplicate stuff everywhere. – Álvaro González Aug 27 '18 at 06:15
  • @ÁlvaroGonzález Just wanted to let you know that PHP Intelephense works exactly as I hoped. Not sure why the PHP IntelliSense plugin didn't work. Maybe your comment would work as an answer to this question? – Jimmy Aug 31 '18 at 03:04

1 Answers1

45

Visual Studio Code core does not include advanced PHP features, just syntax highlighting, simple code completion and code linting provided by the PHP binary as long as you have it installed. In short, the features you can configure with these directives:

// Controls whether the built-in PHP language suggestions are enabled. The support suggests PHP globals and variables.
"php.suggest.basic": true,

// Enable/disable built-in PHP validation.
"php.validate.enable": true,

// Points to the PHP executable.
"php.validate.executablePath": null,

// Whether the linter is run on save or on type.
"php.validate.run": "onSave"

For anything else you need to install a third-party extension.

My personal choice is PHP Intelephense. In particular, it supports docblock annotations, including magic properties:

/**
 * @property string $foo
 */
class Bar
{
}

... and inline types:

/** @var \Database $db */
$db->connect();
Álvaro González
  • 142,137
  • 41
  • 261
  • 360