PhpStorm is written in Java. Users can extend the IDE by installing plugins created for PhpStorm or write their own plugins. The software also communicates with external sources like XDebug. All features available in WebStorm are included in PhpStorm, which adds support for PHP and databases.
The page and all the pages under this node are available only when the PHP plugin is enabled. The PHP plugin is not bundled with IntelliJ IDEA, but it can be installed on the Settings/Preferences | Plugins page as described in Installing plugins from JetBrains repository.
Download the latest version of WebStorm for Windows, macOS or Linux. In this particular video we will setup PHP working environment.We will download and install Xampp, PHP Storm and VsCode. Download links are bellow.Subscribe.
Use this page to configure PHP development and unit testing support in the project by choosing one of the available PHP interpreters, see Configuring Local PHP Interpreters.
Item | Tooltip / Shortcut | Description |
---|---|---|
PHP language level | In this list, specify the PHP functionality scope to get coding assistance for. Each functionality scope is associated with the PHP version that supports this functionality. Currently PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, PHP 7, PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, and PHP 8.0 levels are supported. See supported PHP versions for details. No correlation between the PHP version used in the project and the language level is enforced. Although the language version of each interpreter is detected automatically, you can still tell IntelliJ IDEA to provide you with coding assistance that corresponds to a different language level. However, if you attempt to use a code construct that is not supported by the specified language level, IntelliJ IDEA suggests a Switch to PHP <version>quick-fix. When you open an existing project, IntelliJ IDEA analyzes it for the language features used and sets the appropriate language level automatically. You can also set the PHP language level directly from the status bar. The list is inactive when the PHP language version is constrained in composer.json and settings synchronization with composer.json is enabled on the Composer page of the Settings/Preferences dialog Ctrl+Alt+S. | |
CLI Interpreter | In this list, choose the PHP interpreter to use in the current project by default. The list contains all the currently configured local and remote PHP interpreters. See Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters for details. | |
Shift+Enter | Click this button next to the CLI Interpreter list to create a new IntelliJ IDEA-wide PHP installation configuration in the CLI Interpreters dialog that opens. See Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters for details. | |
Path mappings | When you configure a remote interpreter accessible through SFTP, Vagrant, Docker, or WSL this read-only field shows the path mappings retrieved from the corresponding deployment configuration, Vagrantfile, or Dockerfile. These mappings are read-only. To provide the custom mappings, click next to the field and specify them in the Edit Project Path Mappings dialog that opens:
|
Include Path Tab
The area displays the list of configured include paths. Include paths are used for holding third-party code that is used for completion and reference resolution in some functions/methods that use file paths as arguments, for example, require()
or include()
.
Use and to add and remove paths.
Use and to reorder the items in the list.
Click to sort the paths alphabetically in the ascending order.
PHP Runtime Tab
The area lists the available PHP stubs, which are normal, syntactically correct PHP files containing annotated function, method, and class signatures, constant definitions, and so on. PHP stubs are added to IntelliJ IDEA's internal knowledge to enhance coding assistance for all the Standard PHP Library components as well as for common extensions.
Use the checkboxes next to each item to enable/disable the corresponding stub.
To load the set of stubs that matches the set of loaded extensions for the currently configured CLI interpreter, click the Sync Extensions with Interpreter button. For details on configuring interpreters, see Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.
In the Project tool window, the currently loaded stubs are displayed under the External Libraries node.
PHP stubs are open-source, and you can contribute to their development. See the blog post for details.
Advanced Settings Area
If necessary, you can load a set of custom PHP stubs to be used by IntelliJ IDEA. Click next to the Default stubs path field and provide the stubs folder location in the dialog that opens. This way, you can loaIntelliJ IDEAonal stubs as well as override the bundled ones.
PHP stubs are available as a Composer package, and can thus be declared and installed as a dependency for some third-party package. In this case, they are stored under the vendor/jetbrains/phpstorm-stubs folder inside your project.
To prevent conflicts between the Composer package stubs and IntelliJ IDEA bundled stubs, do one of the following:
To use the Composer package stubs in your project, explicitly provide the path to their location in the Default stubs path field.
To use the IntelliJ IDEA's bundled PHP stubs, make sure that the Default stubs path field is cleared and the vendor/jetbrains/phpstorm-stubs folder is excluded from the project.
If the Add packages as libraries option is enabled on the Languages & Frameworks | PHP | Composer page of the Settings/Preferences dialog Ctrl+Alt+S, IntelliJ IDEA automatically marks the vendor/jetbrains/phpstorm-stubs folder as excluded. By default, the option is enabled.
If the Add packages as libraries is disabled, you can exclude the stubs package folder manually. To do this, in the Project view, right-click the vendor/jetbrains/phpstorm-stubs folder and select Mark Directory as | Excluded from the context menu.
For details in working with Composer in IntelliJ IDEA, refer to Composer dependency manager.
Analysis Tab
Use this tab to configure the behavior of certain IntelliJ IDEA inspections.
Exception Analysis
Use this section to configure exception analysis, which alters the behavior of the Unhandled exception, Redundant catch clause, Missing @throws tag(s), and Redundant @throws tag(s) inspections.
Item | Tooltip / Shortcut | Description |
---|---|---|
Call tree analysis depth | Use this list to set the desired exception analysis depth level. By default, 1 is selected, in which case IntelliJ IDEA reports the unhandled exceptions for the parent method throwing an exception, as well as the methods directly calling it. Selecting larger values allows you to drill down deeper into the calls hierarchy. If 0 is selected, the exception analysis is limited to the parent method. Values greater than 1 might negatively affect the IDE's performance. | |
Skip calls with constant params | Select this checkbox to have IntelliJ IDEA skip certain method calls in the instance creation expressions during exception analysis:
| |
Unchecked Exceptions | Use this list to specify the exceptions that are treated as unchecked by IntelliJ IDEA. Such exceptions are skipped during exception analysis. | |
Alt+Insert | Click this button to add a class to the Unchecked Exceptions list. In the Choose Class dialog that opens, locate the desired class by using the Search by Name tab or the Project tab. | |
Delete | Click this button to remove a selected class from the Unchecked Exceptions list. |
Custom Format Functions
Use this section to include your custom string formatting functions into the Format function parameters mismatch inspection analysis scope. This inspection reports the mismatches between format function parameters and specification conversion entries and by default analyzes the usages of the standard PHP printf and sprintf functions.
Item | Tooltip / Shortcut | Description |
---|---|---|
Alt+Insert | Click this button to add a new function record to the custom format functions list. In the Add Custom Format Function dialog that opens, provide the name of a class method or function and specify the index of the argument where your formatting template resides. | |
Delete | Click this button to remove a function record from the custom format functions list. | |
Enter | Click this button to edit the selected function record. |
Include Analysis
Use this section to define a custom folder the $_SERVER['DOCUMENT_ROOT']
expression should resolve to, which alters the behavior of the Unresolved include inspection.
The specified path will be used in code completion for the functions/methods that use file paths as arguments (for example, require()
or include()
). If the path is left blank, code completion will suggest the paths relative to the project root.
Commonly, this setting should be modified in case the document root folder defined by your web server configuration is different from the IntelliJ IDEA project root folder.
Item | Description |
---|---|
$_SERVER['DOCUMENT_ROOT'] | In this field, provide the folder the The default value is blank: the |
PhpStorm 2020.3 will come with several PHP 8 attributes available out-of-the-box:#[ArrayShape]
, #[ExpectedValues]
, #[NoReturn]
, #[Pure]
, #[Deprecated]
, #[Immutable]
. Read on to learn more about the attributes, and please share your feedback about the design.
You’ve probably already heard about the attributes in PHP 8. But just in case you haven’t, they are the new format for structured metadata that replaced PHPDoc and will now be part of the language.
What attributes are in PHP 8?
Apart from the syntax definition and validation when calling ReflectionAttribute::newInstance()
, PHP 8 does not provide any attributes out-of-the-box. For attributes that you define, you have to implement their behavior yourself.
What attributes will be available in PhpStorm 2020.3?
Several attributes will be available in PhpStorm 2020.3 under JetBrainsPhpStorm
namespace. #[ExpectedValues]
and #[NoReturn]
are more advanced descendants of .phpstorm.meta.php
functions. And #[ArrayShape]
is a highly anticipated evolution of PHPDoc’s array description. There also will be #[Deprecated]
, #[Pure]
, and #[Immutable]
.
The design of the attributes below is still a work in progress, and your feedback is very welcome.
#[Deprecated]
This attribute is similar to @deprecated
PHPDoc tag and is used to mark methods, functions, classes, or class constants and it indicates that they will be removed in future versions as they have become obsolete.
The main advantage of this new attribute is that you can specify replacement for functions and methods. That will help users of the deprecated functionality migrate.
If you specify the reason argument for the attribute, then it will be shown to a user in the inspection tooltip.
#[Deprecated(reason: ', replacement: ')]
Let’s take a look at a real-world example.
In Symfony 5.2 the SymfonyComponentDependencyInjectionAlias::setPrivate()
will be deprecated. With #[Deprecated] attribute we can make migration easier.
#[ArrayShape]
One of the most requested features for PhpStorm was support for more specific array PHPDoc annotations. This was partially implemented with Psalm support.
But the other part – specifying the possible keys and what value type they correspond to – was still missing. This functionality could be useful when working with simple data structures or object-like arrays when defining a real class may feel excessive.
Starting from PhpStorm 2020.3, it will be possible to define the structure of such arrays with an #[ArrayShape]
.
The syntax is as follows:
As you can see, the ‘type’ can be specified as a scalar in a string or as a class reference in the form of an FQN string or a ::class
constant.
You can extract an array that defines a shape into a constant and then reuse it inside the attributes where it applies:
What about legacy projects that can’t upgrade to PHP 8?
Fortunately, the syntax of one-line attributes is backward compatible. This means that if you add the #[ArrayShape]
attribute in a separate line in your PHP 7.* project, the PHP interpreter will parse it as just a line comment and you won’t get a parse error. However, multiline attributes are not safe for versions of PHP prior to 8.
Unlike the PHP interpreter, PhpStorm will analyze attributes anyway! So even if your project runs on PHP 7.4 or lower, you still benefit from adding #[ArrayShape]
attributes.
Note, you’ll have code completion when working with earlier PHP versions in PhpStorm, but inspections will run only with language level 8 and above.
#[Immutable]
Immutable objects are the ones that can not be changed after they are initialized or created. The benefits of using them are the following:
Intellij Php
- The program state is more predictable.
- Debugging is easier.
It was possible to somewhat emulate immutable objects using getters and setters or magic methods. Starting from PhpStorm 2020.3, you can simply mark objects or properties with the #[Immutable]
attribute.
PhpStorm will check the usages of objects and properties and highlight change attempts.
You can adjust the write scope restriction to a constructor only, or simulate private and protected scopes. To do that, pass one of the constants CONSTRUCTOR_WRITE_SCOPE
, PRIVATE_WRITE_SCOPE
, PROTECTED_WRITE_SCOPE
to the #[Immutable]
attribute constructor.
The #[Immutable] attribute will work even with PHP 7.4 and lower!
#[Pure]
You can mark functions that do not produce any side effects as pure. Such functions can be safely removed if the result from executing them is not used in the code after.
PhpStorm will detect redundant calls of the pure functions.
If the function is marked as pure, but you try to change something outside it, i.e. it produces a side effect, then PhpStorm will warn you and highlight the unsafe code.
#[ExpectedValues]
With this attribute, you can specify which values a function accepts as parameters and which it can return.
Webstorm Php Tutorial
This is similar to what the expectedArguments()
function could do in .phpstorm.meta.php
, except that the meta version is more like a completion adversary. The attribute, by contrast, assumes that there are no other possible values for the argument or return value.
For example, let’s take the count function:count ( array|Countable $array_or_countable [, int $mode = COUNT_NORMAL ] ) : int
The second argument it takes is an integer, but in reality, it is not an integer. Rather it is one of the constants COUNT_NORMAL
or COUNT_RECURSIVE
, which correspond to the 0
and 1
.
You can add an attribute #[ExpectedValues]
to the second parameter. And this is how the code completion will change in this case.
No meta
With expectedArguments() in .phpstorm.meta.php
With #[ExpectedValues] attribute
Expected values are passed to the attribute constructor and can be any of the following:
- Numbers:
#[ExpectedValues(values: [1,2,3])]
- String literals:
#[ExpectedValues(values: [‘red’, ‘black’, ‘green’])]
- Constant references:
#[ExpectedValues(values: [COUNT_NORMAL, COUNT_RECURSIVE])]
- Class constant references:
#[ExpectedValues(values: [Code::OK, Code::ERROR])]
And there are a few ways to specify expected arguments:
#[ExpectedValues(values: [1,2,3])]
means that only one of the values is expected.#[ExpectedValues(flags: [1, 2, 3])]
means that a bitmask of the specified values is expected, e.g.1 | 3
.#[ExpectedValues(valuesFromClass: MyClass::class)]
means that any of the constants from the class `MyClass
` is expected.#[ExpectedValues(flagsFromClass: ExpectedValues::class)]
means that a bitmask of the constants from the class `MyClass` is expected.
#[ExpectedValues] examples
Let’s take a look at the response()
helper in Laravel. It takes the HTTP status code as the second argument.
This leaves us missing two key features:
- Code completion for possible status codes
- Validation in the editor
Let’s fix this by adding the attribute #[ExpectedValues(valuesFromClass: Response::class)]
#[NoReturn]
Some functions in a codebase may cause the execution of a script to stop. First, this is not always obvious from a function name, for example, trigger_error()
can stop execution depending on the second argument. And second, PhpStorm cannot always detect such functions, because deep analysis can cause performance problems.
This is why it makes sense to mark such functions as exit points to get a more accurate control flow analysis by adding the #[NoReturn]
attribute.
Also, PhpStorm will offer to propagate the attribute down across the hierarchy with a quick-fix to get even more well-defined analysis.
Show me the code!
The definitions of these attributes are available in the github.com/JetBrains/phpstorm-stubs. We are going to annotate some internal functions like parse_url()
with #[ArrayShape]
in the stubs. And also migrate@property-read
to #[Immutable]
.
What other attributes are in the works?
There are ideas for more attributes, such as the Contract
attribute. We are interested to know which ones you would find useful for your work. Feel free to share any comments or suggestions with us.
Final notes
PhpStorm won’t look for attributes deeper than one level. So we expect users to propagate attributes with a quick-fix.
Currently, these attributes are distributed with github.com/JetBrains/phpstorm-stubs. It means they are available in the IDE out-of-the-box. But we may reconsider how the distribution is done in the future.
The full list of changes in this EAP build is available in the release notes.
- Important! PhpStorm EAP builds are not fully tested and may be unstable.
- You can install an EAP build side by side with a stable PhpStorm version to try out the latest features.
- EAP builds are free to use but expire 30 days after the build date.
Please report any problems you find to our issue tracker, or mention them in the comments to this post.
Your JetBrains PhpStorm team
The Drive to Develop