Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Intellisense in Visual Studio Code

Tags:

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.

like image 292
Jimmy Avatar asked Aug 25 '18 03:08

Jimmy


People also ask

How do I add IntelliSense code to Visual Studio?

You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript). Tip: The suggestions widget supports CamelCase filtering, meaning you can type the letters which are upper cased in a method name to limit the suggestions.

Does Visual Studio code have IntelliSense?

Visual Studio Code's JavaScript IntelliSense provides intelligent code completion, parameter info, references search, and many other advanced language features. Our JavaScript IntelliSense is powered by the JavaScript language service developed by the TypeScript team.

Can I use Visual Studio code for PHP?

Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.

How do I enable IntelliSense in Visual Studio?

To access this options page, choose Tools > Options, and then choose Text Editor > C# > IntelliSense.


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();
like image 109
Álvaro González Avatar answered Sep 20 '22 17:09

Álvaro González