Symfony extension for PHPStan
This extension provides following features:
ContainerInterface::get() and ::has() methods.Controller::get() and ::has() methods.AbstractController::get() and ::has() methods.ContainerInterface::getParameter() and ::hasParameter() methods.ParameterBagInterface::get() and ::has() methods.Controller::getParameter() method.AbstractController::getParameter() method.Request::getContent() method based on the $asResource parameter.HeaderBag::get() method based on the $first parameter.Envelope::all() method based on the $stampFqcn parameter.InputBag::get() method based on the $default parameter.InputBag::all() method based on the $key parameter.ResponseHeaderBag::getCookies() method based on the format argument.TreeBuilder and NodeDefinition objects.SerializerInterface::deserialize() and DenormalizerInterface::denormalize() methods based on the $type argument.HandleTrait::handle() method based on the message handler map.FormInterface::getErrors() method based on the $deep and $flatten parameters.KernelInterface::locateResource() method.Extension::getConfiguration() method.CacheInterface::get() method based on the callback return type.BrowserKitAssertionsTrait::getClient() method.HandleTrait.@required annotation and #[Required] attribute for autowiring properties and methods.InputInterface::getArgument(), ::getOption(), ::getOptions(), ::hasArgument(), and ::hasOption().Command::getHelper() method.To use this extension, require it in Composer:
composer require --dev phpstan/phpstan-symfonyIf 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.neonTo perform framework-specific checks, include also this file:
includes:
- vendor/phpstan/phpstan-symfony/rules.neonYou have to provide a path to srcDevDebugProjectContainer.xml or similar XML file describing your container.
…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: falseBe aware that it may hide genuine errors in your application.
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.phpSymfony 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: falseCall the new env in your console-application.php:
$kernel = new \App\Kernel('phpstan_env', (bool) $_SERVER['APP_DEBUG']);The extension provides advanced type inference for methods that internally use Symfony Messenger's HandleTrait. This feature is particularly useful for query bus implementations (in CQRS pattern) that use/wrap the HandleTrait::handle() method.
parameters:
symfony:
messenger:
handleTraitWrappers:
- App\Bus\QueryBus::dispatch
- App\Bus\QueryBus::execute
- App\Bus\QueryBusInterface::dispatchuse Symfony\Component\Messenger\Attribute\AsMessageHandler;
// Product handler that returns Product
#[AsMessageHandler]
class GetProductQueryHandler
{
public function __invoke(GetProductQuery $query): Product
{
return $this->productRepository->get($query->productId);
}
}…No open issues yet, or sync has not completed.