Go to file
2022-12-15 16:57:52 +01:00
.github/workflows drop Symfony 4 2022-12-12 05:53:31 +03:00
src Cleanup 2022-12-15 16:57:52 +01:00
tests Cleanup 2022-12-15 16:57:52 +01:00
.editorconfig Add template for getRuntime method (#164) 2021-03-29 17:24:13 +03:00
.gitattributes Do not distribute specific CI workflows, tests and analyzer configs (#259) 2022-06-06 15:18:54 +03:00
.gitignore Upgrade php-cs-fixer to v3 (#178) 2021-05-10 11:14:54 +03:00
.php-cs-fixer.dist.php Upgrade php-cs-fixer to v3 (#178) 2021-05-10 11:14:54 +03:00
codeception.yml refactor container check (#8) 2020-03-09 15:24:39 +03:00
composer.json Update composer.json 2022-12-12 06:51:56 +03:00
LICENSE initial commit 2019-11-12 16:36:05 +03:00
phpunit.xml container parameter types (#184) 2021-06-18 10:59:41 +03:00
psalm-baseline.xml container parameter types (#184) 2021-06-18 10:59:41 +03:00
psalm.xml [doctrine] query builder explicit parameter type for performance (#144) 2021-02-14 12:09:40 +03:00
README.md [doc] Update README.md (#273) 2022-08-17 19:13:17 +03:00

Symfony Psalm Plugin

Integrate

Installation

composer require --dev psalm/plugin-symfony
vendor/bin/psalm --init
vendor/bin/psalm-plugin enable psalm/plugin-symfony

Features

  • Detects the ContainerInterface::get() result type. Works better if you configure a compiled container XML file.
  • Supports Service Subscribers. Works only if you configure a compiled container XML file.
  • Detects return types from console arguments (InputInterface::getArgument()) and options (InputInterface::getOption()). Enforces to use "InputArgument" and "InputOption" constants as a best practise.
  • Detects Doctrine repository classes associated to entities when configured via annotations.
  • Fixes PossiblyInvalidArgument for Symfony\Component\HttpFoundation\Request::getContent(). The plugin determines the real return type by checking the given argument and marks it as either "string" or "resource".
  • Detects the return type of Symfony\Component\HttpFoundation\HeaderBag::get() by checking the default value (third argument for < Symfony 4.4).
  • Detects the return types of Symfony\Component\Messenger\Envelope::last and Symfony\Component\Messenger\Envelope::all, based on the provided argument.
  • Taint analysis for Symfony.
  • Detects services and parameters naming conventions violations.
  • Complains when Container is injected in a service, and asks to use dependency-injection instead.
  • Fixes PropertyNotSetInConstructor false positive issues:
    • $container in AbstractController
    • $context in ConstraintValidator classes
    • properties in custom @Annotation classes
  • And much more!

Configuration

If you follow the installation instructions, the psalm-plugin command will add this plugin configuration to the psalm.xml configuration file.

<?xml version="1.0"?>
<psalm errorLevel="1">
    <!--  project configuration -->

    <plugins>
        <pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin" />
    </plugins>
</psalm>

To be able to detect return types of services using ID (generally starts with @ in Symfony YAML config files. Ex: logger service) containerXml must be provided. Example:

<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
    <containerXml>var/cache/dev/App_KernelDevDebugContainer.xml</containerXml>
</pluginClass>

This file path may change based on your Symfony version, file structure and environment settings. Default files are:

  • Symfony 3: var/cache/dev/srcDevDebugProjectContainer.xml
  • Symfony 4: var/cache/dev/srcApp_KernelDevDebugContainer.xml
  • Symfony 5: var/cache/dev/App_KernelDevDebugContainer.xml

Multiple container files can be configured. In this case, the first valid file is taken into account. If none of the given files is valid, a configuration exception is thrown. Example:

<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
    <containerXml>var/cache/dev/App_KernelDevDebugContainer.xml</containerXml>
    <containerXml>var/cache/dev/App_KernelTestDebugContainer.xml</containerXml>
</pluginClass>

If you're using PHP config files for Symfony 5.3+, you also need this for auto-loading of Symfony\Config:

<extraFiles>
    <directory name="var/cache/dev/Symfony/Config" /> <!-- https://github.com/psalm/psalm-plugin-symfony/issues/201 -->
</extraFiles>

If you're getting the following error

MissingFile - config/preload.php - Cannot find file ...var/cache/prod/App_KernelProdContainer.preload.php to include

...you can suppress it like this:

<issueHandlers>
    <MissingFile> <!-- https://github.com/psalm/psalm-plugin-symfony/issues/205 -->
        <errorLevel type="suppress">
            <file name="config/preload.php" />
        </errorLevel>
    </MissingFile>
</issueHandlers>

Twig tainting (experimental)

When it comes to taint analysis for Twig templates, there are currently two approaches:

  • The first one is based on a specific file analyzer (Psalm\SymfonyPsalmPlugin\Twig\TemplateFileAnalyzer) which leverages the Twig parser and visits the AST nodes.
  • The second one is based on the already compiled Twig templates, it only bridges calls from Twig\Environment::render to the actual doRender method of the compiled template.

Twig Analyzer

This approach is more robust since it relies on the official Twig parser and node visitor mechanisms. For the moment, it is only able to detect simple tainted paths.

To leverage the real Twig file analyzer, you have to configure a checker for the .twig extension as follows:

<fileExtensions>
   <extension name=".php" />
   <extension name=".twig" checker="./vendor/psalm/plugin-symfony/src/Twig/TemplateFileAnalyzer.php"/>
</fileExtensions>

See the currently supported cases.

Cache Analyzer

This approach is "dirtier", since it tries to connect the taints from the application code to the compiled PHP code representing a given template. It is theoretically able to detect more taints than the previous approach out-of-the-box, but it still lacks ways to handle inheritance and stuff like that.

To allow the analysis through the cached template files, you have to add the twigCachePath entry to the plugin configuration :

<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
    <twigCachePath>/cache/twig</twigCachePath>
</pluginClass>

See the currently supported cases.

Credits