CZ · devsense.com

New Features in PHP for Visual Studio Code (January 2023)


This email was sent

Is this your brand on Milled? Claim it.

 
Hello -,

PHP extension is getting exciting new features, improvements, and fixes! Welcome to our newsletter, and thank you for using our extension.

Download PHP extension

New Code Actions

More useful refactorings are coming to the PHP extension and VS Code. Following are refactorings introducing common PHP 8 syntax to your code.

Condition ?: can be replaced with ?->

Simplifying conditional expressions like the one below:

simplify conditional expression

Assignments can be simplified

Simplifying assignments:

simplify assignment expression

Converting switch to match

In certain cases, switch statement can be converted to the new match expression.

Improved Type Checks and IntelliSense

BREAKING: By default, we're now reporting diagnostics inside opened documents only. This can be changed using setting "php.problems.scope". The exception is the syntax errors - those are reported from across the whole workspace.

  • In addition, various falsy warnings have been fixed, mostly in combination with generic types. The type analysis have been improved in general.
  • Also, most of the polyfills are not messing up with codecompletion and symbol navigation; Since built-in manual is annotated with generic type arguments, it is preferred over the polyfills from 3rd party packages (#241).
  • Expression in the assert() language construct is used to determine variables type now. (#744)
  • Certain type annotations are now more precise.
  • Implicit closure variables inside arrow functions are now properly initialized. This fixes falsy warnings about uninitialized variables use inside nested arrow functions.
  • More Core types have been annotated with template type arguments, allowing for better inferring of types where @template and similar are used (#746).
  • Support for generics has been improved to properly handle doctrine/collections and other templated types using extended @template syntax (#746).

More Generic Annotations

Bringing the standard types annotated with generic template types - this is especially helpful in Laravel and Symfony frameworks, where it provides type inferring for all the collections, iterables, Generator, DOMNodeList, etc.

This improves value inferred within foreach for all kinds of iterables.

The PHP 8.1 enum objects implicitly implement UnitEnum and BackedEnum interfaces. The BackedEnum interface is annotated with a template type argument so it can be used in Doc Comments with the backed type for better type analysis. E.g.:

/** @param \BackedEnum $e */
function foo($e) {
    return $e->value // -> string
}

enum MyEnum : string {
    case A;
}

foo( MyEnum::A );

Generic Types & Doc Comments Type Annotations

The type annotations in PHP Doc Comments have been reimplemented, so even complex constructs are handled, can be completed, refactored, highlighted, and previewed. Highlighting occurences works even in nested generic types, as well as rename refactoring and code completion.

/**
 * @template TElement of \BackedEnum
 * @return (Collection|array)[] Complex generic type annotations */

Improved Completion Ordering

The code completion itself has been improved, so local symbols should be listed first. We're still working on this feature, and improving as much as possible.

Formatting

We're happy to introduce new Laravel code style. You just need set "php.format.codestyle" to "laravel".

Now you can also let formatter to auto-align consecutive assignments with "php.format.rules.alignConsecutiveAssignments" option #692.

$a     = 1;
$bbb   = 2;
$ccccc = 3;

More:

  • Removing unwanted extra space after ? token in nullable return type #752
  • Fixes in Wordpress code style
    • Types and function declarations have do not place a open brace { on a new line
    • Unset calls weren't formatted with spaces within parentheses.
    • exit() or die() do not have a space within parentheses.
  • php.format.rules.alignConsecutiveAssignments stopped the formatter in certain cases.
  • Using not equal operator <> instead of != stopped the formatter.
  • Optional comma in constructor header in property promotion stopped the formatter.
  • Certain situations when combining HTML and PHP stopped the formatter.
  • When case inside switch was followed by ; instead of : stopped the formatter.
  • In Wordpress code style we are no longer inserting space between parentheses and brackets when empty e.g foo(), but foo( 1, 2 ).
  • Not aligning array initializers key value pairs when they are not on separate line (when php.format.rules.arrayInitializersAlignKeyValuePairs enabled).
  • Fixes incorrect outdenting of constants in multiline expressions.
  • readonly in namespace caused formatter to stop formatting
  • readonly and enum in function declaration doesn't add space before paren
  • PHP 8.2 Disjunctive Normal Form Types are supported now
  • Constant uses were double indented in multi-line expressions #748
  • We've address some cases when combing html and php code didn't result in neat looking code. Most notably in Wordpress templates.

Doc Comment Syntax, Colorization and Semantic Tokens

PHP Doc Comments are getting very complex, and frameworks like PHPStan add even more syntax and complexity. We're supporting most of the extended syntax, including generic types, templates, type aliases, unions, intersections, callable syntax, and array shapes; and in this release, those types in documentary comments are colorized accordingly.

php callable specification

The whole type expression is now parsed, colorized, and recognized. The tooltip shows the signature help displaying the Closures arguments and return types.

PHPStan Conditional Return Types

This update adds support for the new PHPStan's Conditional return types. Return types specified in this way are properly recognized now, colored accordingly right in the source code, and the IntelliSense uses the specified types correspondingly.

phpstan conditional return types

PHPStan Array Shape, callable, list, and more

Since this release, the editor supports all the syntaxes for array shapes and callables. In addition, the type analysis handles specified PHPStan types accordingly.

PHPStan and Psalm array shapes specified in PHP Doc Comments are allowed, parsed, colorized, and used in code completion and type analysis.

Additionally, list and non-empty-list types are handled as well. Including their generic counterparts with < and > types specified.

All the callable syntaxes in PHP Doc Comments are supported now as well.

Multi-Lined Structured Array Types

We're adding support for multi-lined structured array type annotations. This allows users to specify array type with its keys and corresponding entries types. See the exmaple below:

/**
 * @return array
 *         {
 *           name: string,
 *           age: int,
 *         }
 */

Doc Comment Snippet for Variables

When creating a Doc Block above a variable assignment, or above a global variable, you can now configure the snippet that will be created. Additionally, the snippet setting has a new property "singleline": true|false (for variables it's true by default) allowing you to specify whether to create the Doc Block single-lined (i.e. /** @var Type */).

Sample setting:

{
    "php.docblock.variableSnippet": {
        "singleline": false
    }
}

Fixes

  • Apple Silicon support.
  • We have fixed a memory leak, and the language server crash.
  • Fixed PHPDoc snippet after typing /**, when text got corrupted.
  • Added missing Psalm scalar type annotations.
  • Correct order of resolving @mixin members (#777).
  • html files can be associated with php language support.
  • //region///endregion folding (#788)
  • integer, boolean, list types are handled properly in Doc Block.
  • Unused use analysis fixed, so it handles type names specified in Doc Blocks.
  • Diagnostic and parameter completion for session_set_cookie_params() works for both possible definitions.
  • Missing Imagick constants added.
  • Improves type inferring for generic types and various edge cases.
  • readonly and enum keywords are not incorrectly reported as a syntax error, when used as a part of namespace or a function declaration.
  • The language server is allocating significantly less memory during code completion; this improves the overall performance.
Download PHP extension

Thank you,
DEVSENSE
twitter: @php4vs
web: www.devsense.com
 
Are you sure?

Lists help you organize the brands that you care about. Your lists are private to you.