PHP for VS Code Updates (June 2023)

Author by Jakub Misek
9 minutes
PHP for VS Code Updates (June 2023)

PHP extension for Visual Studio Code is getting more features and updates!

IntelliSense

The editor is getting more fine-tuned features, minor adjustments, nice-to-have features, new code diagnostics, and fixes. Please see the following list for some of them:

  • Detecting Laravel services improved.
  • Completing declare and declare(strict_types=1); after <?php tag.
  • Now it's possible to Ctrl+Click (or go to with F12) on @inheritdoc to navigate to base Doc Comment directly. #897
  • null specified as Doc Comment @param type is reported as invalid.
  • true pseudo-type correctly handled within union types.
  • Code completion and tooltips in trait adaptation blocks have been implemented. (#582)

IntelliSense and phpstorm.meta.php

Supporting .phpstorm.meta.php with expectedArgments() and argumentsSet() now.

expectedarguments

IntelliSense & @phpstan-type and @psalm-type

Since now, we have basic support for local-type aliasing.

IntelliSense & Traits with Generics

Newly, trait use can be annotated with @use doc comment to specify the trait's generic arguments (#840), i.e.:

class MyClass {
    /** @use MyTrait<int, string> */
    use MyTrait;
}

IntelliSense & static Type

We have re-implemented the internals and improved type analysis for various cases involving the use of static and $this within traits, protected properties, and class inheritance in general. Moreover, generic arguments and trait members are resolved better with more inherited type information (#931).

Inlay Hints Improvements

We've added an option to show return type inlay hint only for named functions, not for anonymous functions and arrow functions (#326).

Refactoring

Sort Uses on Save aka Organize Imports

We have added support for VS Code's "organize imports" command and eventual "editor.codeActionsOnSave" setting allowing you to organize imports (aka remove unused uses) when saving documents automatically.

organize uses

The following setting works for sorting and organizing use statements on save:

{
  "editor.codeActionsOnSave": { "source.organizeImports": true },
}

Add Missing PHPDoc Code Action

The Add Missing PHPDoc code action inserts the doc comment as a snippet (#157(comment)). This replaces template variables and allows you to type into placeholders.

More Code Actions

  • PHPDoc @see and @uses is recognized during rename refactoring, find all references, and highlight occurrences.
  • Unused use check works even if problems setting is turned off.
  • Namespace import for any simple class name. We have code action suggestions to import a namespace or fully qualify the class name if there are more classes with the same name.

PHPUnit and Test Explorer

PHPUnit Tests & Continuous Run

Newly it is possible to run tests continuously. The watch icon in Testing panel allows us to keep all the tests, test suites, or just specific test cases always up-to-date. (#1479)

Test Explorer and ParaTest

This latest update allows running ParaTest in the same way as PHPUnit tests. To do so:

  • Install ParaTest composer package using the command Composer: require dev package, and search package brianium/paratest.
  • Update your workspace settings:
    {
    "phpunit.phpunit": "./vendor/bin/paratest"
    }
  • Navigate to Testing panel and run or debug your tests.

Other Test Explorer Improvements

The Test Explorer looks and watches for any PHPUnit XML configuration file matching glob pattern **/phpunit*.xml or **/phpunit.xml.dist (outside the vendor folder) #313. This allows having multiple configuration files in the same directory with custom names.

multiple phpunit configurations

Additionally, we have fixed:

  • Executing all tests within a configuration file.
  • Executing all tests within a single test suite.
  • Correct Xdebug directives so it won't cause unnecessary warnings when debugging tests.

Composer "composer.workingPath" Setting

Thanks to the feedback #909, we've added setting "composer.workingPath" where users can specify the relative path for the composer.json and vendor folders.

Other Improvements

  • Tons of fixes and performance improvements.
  • Several memory issues were fixed.
  • Code completion of named arguments completes the : at the end. (#1600)
  • Code completion of hinted array keys within single quotes. (#1600)
  • Completion triggered after typing SPACE after instanceof keyword.
  • Methods named new() are handled correctly in mouse hover and code completion.
  • match, fn, and interface are not reported as syntax errors when within a qualified name (PHP 8.0+).
  • PHP 8.2 standalone true, false, and null type names. (#338)
  • Fixed a few type inferring cases.
  • Fixed reporting functions annotated with @ignore tag as unknown.
  • Type inferring in the context of is_a() analyzed correctly.
  • Less falsy warnings for non-static methods called statically in case there is __callStatic() magic method.
  • New quick refactorings through code actions.
  • Updated integrated PHP manual and localizations.
  • and more!