phpstan / phpstan-symfony
Symfony Framework extensions and rules for PHPStan
Installs: 43 333 284
Dependents: 980
Suggesters: 2
Security: 0
Stars: 708
Watchers: 11
Forks: 89
Open Issues: 63
Type:phpstan-extension
Requires
- php: ^7.2 || ^8.0
- ext-simplexml: *
- phpstan/phpstan: ^1.12
Requires (Dev)
- nikic/php-parser: ^4.13.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/phpstan-phpunit: ^1.3.11
- phpstan/phpstan-strict-rules: ^1.5.1
- phpunit/phpunit: ^8.5.29 || ^9.5
- psr/container: 1.0 || 1.1.1
- symfony/config: ^5.4 || ^6.1
- symfony/console: ^5.4 || ^6.1
- symfony/dependency-injection: ^5.4 || ^6.1
- symfony/form: ^5.4 || ^6.1
- symfony/framework-bundle: ^5.4 || ^6.1
- symfony/http-foundation: ^5.4 || ^6.1
- symfony/messenger: ^5.4
- symfony/polyfill-php80: ^1.24
- symfony/serializer: ^5.4
- symfony/service-contracts: ^2.2.0
Conflicts
- symfony/framework-bundle: <3.0
- 2.0.x-dev
- 1.4.x-dev
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.x-dev
- 1.3.12
- 1.3.11
- 1.3.10
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.x-dev
- 1.2.25
- 1.2.24
- 1.2.23
- 1.2.22
- 1.2.21
- 1.2.20
- 1.2.19
- 1.2.18
- 1.2.17
- 1.2.16
- 1.2.15
- 1.2.14
- 1.2.13
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.12.44
- 0.12.43
- 0.12.42
- 0.12.41
- 0.12.40
- 0.12.39
- 0.12.38
- 0.12.37
- 0.12.36
- 0.12.35
- 0.12.34
- 0.12.33
- 0.12.32
- 0.12.31
- 0.12.30
- 0.12.29
- 0.12.28
- 0.12.27
- 0.12.26
- 0.12.25
- 0.12.24
- 0.12.23
- 0.12.22
- 0.12.21
- 0.12.20
- 0.12.19
- 0.12.18
- 0.12.17
- 0.12.16
- 0.12.15
- 0.12.14
- 0.12.13
- 0.12.12
- 0.12.11
- 0.12.10
- 0.12.9
- 0.12.8
- 0.12.7
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.6
- 0.11.5
- 0.11.4
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11
- 0.10.2
- 0.10.1
- 0.10
- dev-renovate/major-root-composer
- dev-renovate/major-github-actions
- dev-renovate/root-composer
- dev-hepisec-patch-1
- dev-updates
- dev-commands
This package is auto-updated.
Last update: 2024-10-30 12:10:25 UTC
README
This extension provides following features:
- Provides correct return type for
ContainerInterface::get()
and::has()
methods. - Provides correct return type for
Controller::get()
and::has()
methods. - Provides correct return type for
AbstractController::get()
and::has()
methods. - Provides correct return type for
ContainerInterface::getParameter()
and::hasParameter()
methods. - Provides correct return type for
ParameterBagInterface::get()
and::has()
methods. - Provides correct return type for
Controller::getParameter()
method. - Provides correct return type for
AbstractController::getParameter()
method. - Provides correct return type for
Request::getContent()
method based on the$asResource
parameter. - Provides correct return type for
HeaderBag::get()
method based on the$first
parameter. - Provides correct return type for
Envelope::all()
method based on the$stampFqcn
parameter. - Provides correct return type for
InputBag::get()
method based on the$default
parameter. - Provides correct return type for
InputBag::all()
method based on the$key
parameter. - Provides correct return types for
TreeBuilder
andNodeDefinition
objects. - Notifies you when you try to get an unregistered service from the container.
- Notifies you when you try to get a private service from the container.
- Optionally correct return types for
InputInterface::getArgument()
,::getOption
,::hasArgument
, and::hasOption
.
Installation
To use this extension, require it in Composer:
composer require --dev phpstan/phpstan-symfony
If you also install phpstan/extension-installer then you're all set!
Manual installation
If you don't want to use phpstan/extension-installer
, include extension.neon in your project's PHPStan config:
includes:
- vendor/phpstan/phpstan-symfony/extension.neon
To perform framework-specific checks, include also this file:
includes:
- vendor/phpstan/phpstan-symfony/rules.neon
Configuration
You have to provide a path to srcDevDebugProjectContainer.xml
or similar XML file describing your container.
parameters: symfony: containerXmlPath: var/cache/dev/srcDevDebugProjectContainer.xml # or with Symfony 4.2+ containerXmlPath: var/cache/dev/srcApp_KernelDevDebugContainer.xml # or with Symfony 5+ containerXmlPath: var/cache/dev/App_KernelDevDebugContainer.xml # If you're using PHP config files for Symfony 5.3+, you also need this for auto-loading of `Symfony\Config`: scanDirectories: - var/cache/dev/Symfony/Config # If you're using PHP config files (including the ones under packages/*.php) for Symfony 5.3+, # you need this to load the helper functions (i.e. service(), env()): scanFiles: - vendor/symfony/dependency-injection/Loader/Configurator/ContainerConfigurator.php
Constant hassers
Sometimes, when you are dealing with optional dependencies, the ::has()
methods can cause problems. For example, the following construct would complain that the condition is always either on or off, depending on whether you have the dependency for service
installed:
if ($this->has('service')) { // ... }
In that case, you can disable the ::has()
method return type resolving like this:
parameters:
symfony:
constantHassers: false
Be aware that it may hide genuine errors in your application.
Analysis of Symfony Console Commands
You can opt in for more advanced analysis of Symfony Console Commands
by providing the console application from your own application. This will allow the correct argument and option types to be inferred when accessing $input->getArgument()
or $input->getOption()
.
parameters: symfony: consoleApplicationLoader: tests/console-application.php
Symfony 4:
// tests/console-application.php use App\Kernel; use Symfony\Bundle\FrameworkBundle\Console\Application; require __DIR__ . '/../config/bootstrap.php'; $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); return new Application($kernel);
Symfony 5:
// tests/console-application.php use App\Kernel; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Dotenv\Dotenv; require __DIR__ . '/../vendor/autoload.php'; (new Dotenv())->bootEnv(__DIR__ . '/../.env'); $kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); return new Application($kernel);
// tests/console-application.php use App\Application; // where Application extends Symfony\Component\Console\SingleCommandApplication use Symfony\Component\Console; require __DIR__ . '/../vendor/autoload.php'; $application = new Console\Application(); $application->add(new Application()); return $application;
You may then encounter an error with PhpParser:
Compile Error: Cannot Declare interface PhpParser\NodeVisitor, because the name is already in use
If this is the case, you should create a new environment for your application that will disable inlining. In config/packages/phpstan_env/parameters.yaml
:
parameters: container.dumper.inline_class_loader: false
Call the new env in your console-application.php
:
$kernel = new \App\Kernel('phpstan_env', (bool) $_SERVER['APP_DEBUG']);