Newsletter Releases

PhpStorm 2021.1: Preview for PHP and HTML Files, 20+ New Inspections, Improvements in All Subsystems, and Pair Programming via Code With Me

Read this post in other languages:
Русский

PhpStorm 2021.1 is now available!

This major release introduces a built-in preview for PHP and HTML files, 20+ new inspections to help prevent bugs, improvements in all subsystems, pair programming via Code With Me, and much more.

Download PhpStorm 2021.1

Here are the main highlights of the release:

PHP

IDE

Docker

Version Control

Code With Me

Read this blog post for details about all the significant updates, along with a ton of GIFs!

 
 

PHP

Set the PHP language level in the status bar

In the previous release we added a PHP version indicator to the status bar. In 2021.1 you can unsync the version from the one specified in composer.json and set it to any version you want. This allows you to check the compatibility of your code with newer versions of PHP.

Locate PHP settings more easily

We moved the main PHP-related settings to the top level of the Settings / Preferences | PHP dialog and it’s now faster to get to them.

Preview PHP and HTML files in the editor

When you hover the cursor over the editor, a popup with each of the different browser icons appears in the top right corner. When you click on one of the browser icons, the current file is opened in that browser.

Now there will be an additional PhpStorm icon there . Click on it to open a preview of the file directly in the editor without switching to another window.

This feature works with HTML and PHP files, as well as for all linked CSS and JavaScript files.


Open a file, start typing, and the preview tab will instantly show all the changes.

PhpStorm uses a local PHP interpreter that is specified in the project settings under Settings/Preferences | PHP. Docker and other remote interpreters are not yet supported.

For the PhpStorm icon to appear, you need to select at least one other browser under Preferences | Tools | Web Browsers and make sure that the For HTML files checkbox is checked.

You can also use the Alt+F2 shortcut to open a preview tab.

Use table prefixes in SQL queries

Many content management systems and frameworks allow you to specify a table prefix, which is useful when using the same database for multiple applications.

In previous versions, PhpStorm would lose database integration in such cases because SQL queries contained markers for prefixes.

It is now possible to specify prefixes in the .phpstorm.meta.php file. Learn more about metafiles and how to specify SQL prefixes in this help article.

Let’s say we have the following query in a Drupal app:

You’ll notice that PhpStorm is unable to resolve either the table name or the columns. That’s because we have specified a table prefix in the Drupal config.

To fix this, add a .phpstorm.meta.php file with the following content anywhere in the project:

namespace PHPSTORM_META {
 override(
  // Virtual function to indicate that all SQL
  // injections will have the following replacement rules.
   sql_injection_subst(),
   map([
     '{' => "PS2021_", // all `{` in injected SQL strings will be replaced with a prefix
     '}' => '',       // all `}` will be replaced with an empty string
   ]));
}

With this file, PhpStorm will be able to link SQL queries to data sources and provide you with all its smart features, such as completion, resolve, and so on.

This works with concatenation, as well, as you can see in the following example:

<?php
const DB_PREFIX = "mydatabase_";
$sql = "SELECT * FROM " . DB_PREFIX . 'table_name';

Inspections and quick-fixes

In this release we have added more than 20 different inspections and quick-fixes to help prevent bugs in the early stages of development. You can check the list of all the inspections and adjust them under Settings / Preferences | Editor | Inspections.

To fix any problems, press Alt+Enter and select a suggested quick-fix.

Here are some of the most notable new inspections.

Simplify `if` blocks with common bodies

Some if and else blocks can become redundant, for example after changes that make their bodies the same.

PhpStorm will detect such redundancies and offer a quick-fix to eliminate them:

If only part of the body is shared, then PhpStorm will suggest extracting just that part:

Invert `if` statement

Press Alt+Enter on any `if` and choose “Invert `if` statement” from the menu. This will change the condition to its opposite and make the necessary code adjustments to preserve the logic.

If you are a fan of the early return practice, this action will help you refactor your code.

Take a look at this example:

It will also work in loop constructs and help you simplify them:

PHP 8: Replace assignment in the function call with named argument

It used to be common practice to add a variable assignment in a function call to mimic a parameter name.

With PHP 8, use Alt+Enter to replace the assignment with a real named argument.

Replace `isset` with coalesce

This code snippet isset($a) ? $a : $b; is exactly the same as $a ?? $b, so PhpStorm will suggest an Alt+Enter quick-fix to replace it.

You can run an inspection on the whole project to find all the places where this quick-fix can be applied and fix them all at once.

In the menu, choose Code | Run Inspection by Name… and select the desired inspection, for example `isset` can be replaced with coalesce. All the instances identified can be corrected immediately in the search results.

Replace `isset` with `!== null`

Using the isset() function is only effective for arrays and variables. In all other cases, it makes sense to verify whether the operand is not null. Use Alt+Enter to replace isset() checks with null checks.

Unnecessary leading ‘\’ in ‘use’ statement

In some cases, the leading backslash is redundant for namespaces. PhpStorm helps determine where it can be removed.

`foreach` variable overwrites already defined variables

Variables for key and value in a foreach loop can clash with the names of other variables or parameters.

The problem is that PHP does not have a separate scope for them, meaning that the value will be overridden, which likely is not what you might expect.

PhpStorm highlights all of the places where errors could occur.

Unnecessary curly braces syntax for variables

PhpStorm highlights when the curly braces in the context of string interpolation are redundant and can be safely removed to make the code cleaner.

Boolean expression can be simplified

If a boolean expression contains true or false literals then you can make them shorter by removing excessive chunks.

PhpStorm highlights such expressions, and you can use the Alt+Enter quick-fix to simplify them.

Strict comparison of operands with incompatible types

The === operator for strict comparison will always result in false if one operand type is not the same as the other.

PhpStorm will highlight such cases, as they can potentially be a source of bugs. PhpStorm takes into account all type-inferred information, and will help you find issues in less obvious cases.

Change parameter type based on a default value

If the declared type does not match the type of a value, you can quickly update the type using Alt+Enter. Or with PHP 8, PhpStorm will offer you the choice to use a union type.

PhpStorm highlights http:// protocol usages in strings and offers an Alt-Enter quick-fix to change them to https://. You can also add URLs to the ignore list with a quick-fix.

Suspicious name combination

PhpStorm highlights some typical misusages of parameter names or return values, for example $needle and $haystack or $x and $y. Because such misusage is either a bug, or at least very confusing for those who read the code.

Array access can be replaced with ‘foreach’ value

There’s no need to access an array item by key in a foreach loop when you have a value. PhpStorm can replace the usage with the Alt+Enter quick-fix.

Array is always empty

Iterating over an empty array effectively does nothing. So PhpStorm highlights such cases to call your attention to them.

‘define’ can be replaced with ‘const’

PhpStorm detects cases where define is effectively the same as const and suggests an Alt+Enter quick-fix to improve readability by replacing it.

Configure pre-commit inspections

You can now choose a code inspection profile before committing changes to the VCS. Click the gear icon to show commit options, then tick the Analyze code checkbox, click Configure, and choose the desired profile. Profiles can be created in Preferences/Settings | Editor | Inspections.

Other

Synthetic scope for better refactoring

In PHP, loop constructs like foreach, for, while, and catch blocks do not have isolated scopes. This can be inconvenient if you want to rename a variable only inside a block.
In PhpStorm 2021.1, we have introduced a synthetic scope for such blocks, so the rename refactoring (Shift+F6) will be more intuitive.

Better automatic language injection

In previous versions, PhpStorm detected the language for arguments. For example, if you passed a pattern string to preg_* functions, it was highlighted as a regular expression.

But if you wanted to pass a variable, the value of the variable would not be highlighted.

PhpStorm 2021.1 analyzes how variables are used and injects language references automatically.

Extensions in the `suggest` section of composer.json

Your codebase may have classes from PHP extensions. Previously, PhpStorm checked whether such extensions were added in the `require` section of composer.json.

However, extensions may sometimes be optional, and you may need to check for the existence of classes from the `suggest` section before they can be used.

PhpStorm 2021.1 checks for extensions in both the `require` and `suggest` sections.

PhpStorm warns you when an extension is registered in `composer.json` as suggested but is used without checks, like extension_loaded() or function_exists(). You can use a quick-fix to move extensions to `require/require-dev`.

Notable Fixes

In this release, our main focus was stability and quality. In total, we fixed more than 2400 issues submitted to our issue tracker by users and JetBrains team members. Here are just a few of the most interesting ones:

  • It is now possible to debug WSL 2 projects in Docker (WI-53396).
  • Quality tools now properly resolve paths with docker-compose in exec mode (WI-55840).
  • Large files (more than 5k lines) are handled correctly (WI-31569).
  • We’ve significantly improved performance in projects with many aliases (WI-58306).

Code With Me

Code With Me is a JetBrains tool for collaborative development and pair programming. We initially introduced it in PhpStorm 2020.3, and this release delivers a number of significant improvements.

You can now set the required level of access to your project and share a link with your guests. Peers do not even need to have their own IDEs installed to collaborate, and the platform features embedded audio and video calls, along with chat messaging.

For businesses that require extra security, Code With Me on-premises can be installed and securely run on a company’s own private network.

To learn more about the availability of Code With Me with your current JetBrains license, check out the pricing page.

IDE

Maximize tabs in split view

Split the editor by dragging a tab to the corners of the IDE window. Then double-click the tab to maximize the editor area for it. Double-click it again to restore it to its original size.

JSONPath support

Use the JSONPath query language to search through JSON documents. New actions are available under Edit | Find | Evaluate JSONPath.

JSON Lines format support

PhpStorm now supports the newline-delimited JSON Lines format, used for working with structured data and logs. The IDE will recognize .jsonl, .jslines, .ldjson, and .ndjson file types.

Typography settings

It is now possible to adjust font variations and a fallback font. You’ll find the new options under Typography Settings in Settings / Preferences | Editor | Fonts.

Docker

Image completion in Dockerfiles

Adding image names has become easier because code completion now works for them. Have a look:

Folding in multi-stage Dockerfiles

If you use multiple FROM statements in your Dockerfile, each FROM instruction starts a new stage. In v2021.1, you can fold stages and see a separator line between them.

Cancel Docker run

You can easily stop a running Dockerfile from the Services tool window. Select the running item, call up the context menu, and click Stop Deploy.

GitHub pull request improvements

To create a new pull request, click on the + icon or go to Git | GitHub | Create Pull Request.

The new dialog includes everything you might need:

  • Select base and head branches from a list that includes all the branches available.
  • Review changed files in the Files tab.
  • Edit the PR’s title and description, appoint reviewers and assignees, and add labels.
  • Create draft pull requests.

PhpStorm 2021.1 also supports PR templates. Add the pull_requst_template.md file with the PR description to your project, and every time you create a PR this description will appear in it.

Support for a Git commit template

When creating a commit message, PhpStorm now will take into account your Git commit template if one is specified in the commit.template option of your Git configuration.

HTTP Client

Support for SSL

You can now specify SSL settings in the HTTP client by clicking Add environment file and selecting Private. The IDE will automatically create a file where you can add your SSL configurations: `clientCertificate`, `hasCertificatePassphrase`, `clientCertificateKey`, `verifyHostCertificate`.

UI improvements

There is a new eye icon that contains options for the way the body is displayed. You can switch modes between Text / JSON / HTML / XML, you can enable line numbers, and there also is a new button to quickly Copy Response Body to Clipboard.

Swagger Improvements

PhpStorm has support for OpenAPI specifications and the built-in Swagger UI.
In this release, the Swagger UI now supports specifications with external file references ($ref).

Database Tools

PhpStorm includes almost all of DataGrip’s features as standard. Check out What’s New in DataGrip 2021.1 for an overview of the new features we’ve added for working with databases.

Web

As usual, all the updates for WebStorm 2021.1 have also been incorporated into PhpStorm. The most notable is Tailwind CSS support.


The full list of changes in PhpStorm 2021.1 is available in the really long release notes.

That’s all for today. Thanks for keeping up with the changes! We hope they improve your PhpStorm experience.

Your JetBrains PhpStorm team
The Drive to Develop