Skip to content

phpstan/phpstan-symfony

Folders and files

NameName
Last commit message
Last commit date
Apr 16, 2019
Apr 15, 2019
Jul 18, 2017
May 13, 2018
May 22, 2018
Dec 7, 2018
May 5, 2017
Apr 28, 2019
May 13, 2018
Apr 28, 2019
Apr 28, 2019
Oct 28, 2018
Apr 4, 2019
Apr 28, 2019

Repository files navigation

PHPStan Symfony Framework extensions and rules

Build Status Latest Stable Version License

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 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.
  • 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.

Usage

To use this extension, require it in Composer:

composer require --dev phpstan/phpstan-symfony

And include extension.neon in your project's PHPStan config:

includes:
	- vendor/phpstan/phpstan-symfony/extension.neon
parameters:
	symfony:
		container_xml_path: %rootDir%/../../../var/cache/dev/srcDevDebugProjectContainer.xml
		# or with Symfony 4.2+
		container_xml_path: '%rootDir%/../../../var/cache/dev/srcApp_KernelDevDebugContainer.xml' 

You have to provide a path to srcApp_KernelDevDebugContainer.xml or similar xml file describing your container.

To perform framework-specific checks, include also this file:

includes:
	- vendor/phpstan/phpstan-symfony/rules.neon

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:
		constant_hassers: false

Be aware that it may hide genuine errors in your application.

Console command analysis

You can opt in for more advanced analysis 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:
		console_application_loader: tests/console-application.php

For example, in a Symfony project, console-application.php would look something like this:

require dirname(__DIR__).'/../config/bootstrap.php';
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
return new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);