百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

phpstan-symfony

> 测试质量
开源

适用于 PHPStan 的 Symfony 扩展

792 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

适用于 PHPStan 的 Symfony 扩展

PHPStan Symfony Framework extensions and rules

  • PHPStan

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 type for ResponseHeaderBag::getCookies() method based on the format argument.
  • Provides correct return types for TreeBuilder and NodeDefinition objects.
  • Provides correct return type for SerializerInterface::deserialize() and DenormalizerInterface::denormalize() methods based on the $type argument.
  • Provides correct return type for HandleTrait::handle() method based on the message handler map.
  • Provides correct return type for FormInterface::getErrors() method based on the $deep and $flatten parameters.
  • Provides correct return type for KernelInterface::locateResource() method.
  • Provides correct return type for Extension::getConfiguration() method.
  • Provides correct return type for CacheInterface::get() method based on the callback return type.
  • Provides correct return type for BrowserKitAssertionsTrait::getClient() method.
  • Provides configurable return type resolution for methods that internally use Messenger HandleTrait.
  • 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.
  • Notifies you when you access undefined console command arguments or options.
  • Validates default values of console command arguments and options.
  • Recognizes @required annotation and #[Required] attribute for autowiring properties and methods.
  • Optionally correct return types for InputInterface::getArgument(), ::getOption(), ::getOptions(), ::hasArgument(), and ::hasOption().
  • Optionally provides correct return type for Command::getHelper() method.

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.

…

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:

php
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().

neon
parameters:
	symfony:
		consoleApplicationLoader: tests/console-application.php

Symfony 4:

php
// 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:

php
// 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);

Single Command Application:

php
// 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:

yaml
parameters:
    container.dumper.inline_class_loader: false

Call the new env in your console-application.php:

php
$kernel = new \App\Kernel('phpstan_env', (bool) $_SERVER['APP_DEBUG']);

Messenger HandleTrait Wrappers

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.

Configuration

neon
parameters:
    symfony:
        messenger:
            handleTraitWrappers:
                - App\Bus\QueryBus::dispatch
                - App\Bus\QueryBus::execute
                - App\Bus\QueryBusInterface::dispatch

Message Handlers

php
use 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);
    }
}

PHP Examples

…

Issues· 78 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPphpphp7phpstanstatic-analysis

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类测试质量
定价开源

> 相关工具

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.