• Stars
    star
    655
  • Rank 66,085 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Symfony extension for PHPStan

PHPStan Symfony Framework extensions and rules

Build 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 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 and NodeDefinition 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

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

Single Command Application:

// 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']);

More Repositories

1

phpstan

PHP Static Analysis Tool - discover bugs in your code without running it!
PHP
12,445
star
2

phpdoc-parser

Next-gen phpDoc parser with support for intersection types and generics
PHP
1,212
star
3

phpstan-doctrine

Doctrine extensions for PHPStan
PHP
547
star
4

phpstan-strict-rules

Extra strict and opinionated rules for PHPStan
PHP
545
star
5

phpstan-phpunit

PHPUnit extensions and rules for PHPStan
PHP
427
star
6

extension-installer

Composer plugin for automatic installation of PHPStan extensions.
PHP
361
star
7

phpstan-deprecation-rules

PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.
PHP
341
star
8

phpstan-src

PHPStan's source code. This is where development happens. Check https://github.com/phpstan/phpstan for the distribution repository.
PHP
295
star
9

phpstan-webmozart-assert

PHPStan extension for webmozart/assert
PHP
150
star
10

phpstan-nette

Nette Framework class reflection extension for PHPStan & framework-specific rules
PHP
98
star
11

phpstan-shim

[DEPRECATED] This repository provides easy way to install PHPStan without the risk of conflicting dependencies.
PHP
87
star
12

phpstan-mockery

PHPStan extension for Mockery
PHP
76
star
13

phpstan-beberlei-assert

PHPStan extension for beberlei/assert
PHP
35
star
14

vim-phpstan

A Vim plugin for PHPStan - https://github.com/phpstan/phpstan. It calls `phpstan` to do static analysis of your PHP code and displays the errors in Vim's quickfix list.
PHP
28
star
15

php-8-stubs

PHP
25
star
16

phpstan-php-parser

PHP-Parser extension for PHPStan
Makefile
23
star
17

phpstan-dibi

Dibi class reflection extension for PHPStan
PHP
13
star
18

build-cs

Coding standard for 1st party PHPStan extensions
2
star