PHP Intellisense in Visual Studio Code

Viewed 36938

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.

1 Answers

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();
Related