mirror of
https://github.com/danog/psalm-plugin-symfony.git
synced 2025-01-22 13:01:49 +01:00
Suppress PropertyNotSetInConstructor error in AbstractController::$container (managed by Symfony DI) (#57)
Co-authored-by: Anton Zagorskii <anton@paytronix.io>
This commit is contained in:
parent
7fb4a082cf
commit
1c68a1529a
52
.github/workflows/integrate.yaml
vendored
52
.github/workflows/integrate.yaml
vendored
@ -70,9 +70,14 @@ jobs:
|
||||
- 7.3
|
||||
- 7.4
|
||||
|
||||
dependencies:
|
||||
- lowest
|
||||
- highest
|
||||
symfony-version:
|
||||
- 3
|
||||
- 4
|
||||
- 5
|
||||
|
||||
exclude:
|
||||
- php-version: 7.1
|
||||
symfony-version: 5
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
@ -92,21 +97,38 @@ jobs:
|
||||
uses: actions/cache@v1
|
||||
with:
|
||||
path: ${{ steps.determine-composer-cache-directory.outputs.directory }}
|
||||
key: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.json') }}
|
||||
restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-
|
||||
key: php-${{ matrix.php-version }}-composer-${{ matrix.symfony-version }}-${{ hashFiles('composer.json') }}
|
||||
restore-keys: php-${{ matrix.php-version }}-composer-${{ matrix.symfony-version }}-
|
||||
|
||||
- name: "Install lowest dependencies from composer.json"
|
||||
if: matrix.dependencies == 'lowest'
|
||||
run: composer update --no-interaction --no-progress --no-suggest --prefer-lowest
|
||||
- name: "Install symfony 3 dependencies from composer.json"
|
||||
if: matrix.symfony-version == '3'
|
||||
run: composer require symfony/framework-bundle ^3.0 --prefer-stable --update-with-all-dependencies --no-interaction --no-progress --no-suggest
|
||||
|
||||
- name: "Install highest dependencies from composer.json"
|
||||
if: matrix.dependencies == 'highest'
|
||||
run: composer update --no-interaction --no-progress --no-suggest
|
||||
- name: "Install Symfony 4 dependencies from composer.json"
|
||||
if: matrix.symfony-version == '4'
|
||||
run: composer require symfony/framework-bundle ^4.0 --prefer-stable --update-with-all-dependencies --no-interaction --no-progress --no-suggest
|
||||
|
||||
- name: "Install Symfony 5 dependencies from composer.json"
|
||||
if: matrix.symfony-version == '5'
|
||||
run: composer require symfony/framework-bundle ^5.0 --prefer-stable --update-with-all-dependencies --no-interaction --no-progress --no-suggest
|
||||
|
||||
- name: "Run unit tests with phpunit"
|
||||
run: vendor/bin/phpunit --configuration=phpunit.xml
|
||||
|
||||
- name: "Run acceptance tests with codeception"
|
||||
run: |
|
||||
vendor/bin/codecept build
|
||||
vendor/bin/codecept run -v
|
||||
- name: "Build acceptance tests with codeception"
|
||||
run: vendor/bin/codecept build
|
||||
|
||||
- name: "Run base acceptance tests with codeception"
|
||||
run: vendor/bin/codecept run -v -g symfony-common
|
||||
|
||||
- name: "Run Symfony 3 acceptance tests with codeception"
|
||||
if: matrix.symfony-version == '3'
|
||||
run: vendor/bin/codecept run -v -g symfony-3
|
||||
|
||||
- name: "Run symfony 4 acceptance tests with codeception"
|
||||
if: matrix.symfony == '4'
|
||||
run: vendor/bin/codecept run -v -g symfony-4
|
||||
|
||||
- name: "Run Symfony 5 acceptance tests with codeception"
|
||||
if: matrix.symfony-version == '5'
|
||||
run: vendor/bin/codecept run -v -g symfony-5
|
||||
|
@ -54,7 +54,7 @@
|
||||
"phpunit": "phpunit --testdox",
|
||||
"codeception": [
|
||||
"codecept build",
|
||||
"codecept run -v"
|
||||
"codecept run -v -g symfony-common"
|
||||
]
|
||||
},
|
||||
"suggest": {
|
||||
|
@ -12,12 +12,29 @@ use Psalm\SymfonyPsalmPlugin\Handler\DoctrineRepositoryHandler;
|
||||
use Psalm\SymfonyPsalmPlugin\Handler\HeaderBagHandler;
|
||||
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
|
||||
use SimpleXMLElement;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
/**
|
||||
* @psalm-suppress UnusedClass
|
||||
*/
|
||||
class Plugin implements PluginEntryPointInterface
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getCommonStubs(): array {
|
||||
return glob(__DIR__ . '/Stubs/common/*.stubphp') ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $majorVersion symfony major version
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getStubsForMajorVersion(int $majorVersion): array {
|
||||
$version = (string) $majorVersion;
|
||||
return glob(__DIR__ . '/Stubs/' . $version . '/*.stubphp') ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -45,7 +62,11 @@ class Plugin implements PluginEntryPointInterface
|
||||
|
||||
$api->registerHooksFromClass(ContainerHandler::class);
|
||||
|
||||
foreach (glob(__DIR__.'/Stubs/*.stubphp') as $stubFilePath) {
|
||||
$stubs = array_merge(
|
||||
$this->getCommonStubs(), $this->getStubsForMajorVersion(Kernel::MAJOR_VERSION)
|
||||
);
|
||||
|
||||
foreach ($stubs as $stubFilePath) {
|
||||
$api->addStubFile($stubFilePath);
|
||||
}
|
||||
}
|
||||
|
15
src/Stubs/4/AbstractController.stubphp
Normal file
15
src/Stubs/4/AbstractController.stubphp
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Controller;
|
||||
|
||||
use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class AbstractController implements ServiceSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
* @psalm-suppress PropertyNotSetInConstructor
|
||||
*/
|
||||
protected $container;
|
||||
}
|
15
src/Stubs/5/AbstractController.stubphp
Normal file
15
src/Stubs/5/AbstractController.stubphp
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Bundle\FrameworkBundle\Controller;
|
||||
|
||||
use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
class AbstractController implements ServiceSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
* @psalm-suppress PropertyNotSetInConstructor
|
||||
*/
|
||||
protected $container;
|
||||
}
|
32
tests/acceptance/acceptance/AbstractController.feature
Normal file
32
tests/acceptance/acceptance/AbstractController.feature
Normal file
@ -0,0 +1,32 @@
|
||||
@symfony-4 @symfony-5
|
||||
Feature: AbstractController
|
||||
|
||||
Background:
|
||||
Given I have the following config
|
||||
"""
|
||||
<?xml version="1.0"?>
|
||||
<psalm errorLevel="1">
|
||||
<projectFiles>
|
||||
<directory name="."/>
|
||||
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
|
||||
</projectFiles>
|
||||
<plugins>
|
||||
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
|
||||
</plugins>
|
||||
</psalm>
|
||||
"""
|
||||
|
||||
Scenario: PropertyNotSetInConstructor error about $container is not raised
|
||||
Given I have the following code
|
||||
"""
|
||||
<?php
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
class MyController extends AbstractController
|
||||
{
|
||||
public function __construct() {}
|
||||
}
|
||||
"""
|
||||
When I run Psalm
|
||||
Then I see no errors
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: ConsoleArgument
|
||||
|
||||
Background:
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: ConsoleOption
|
||||
|
||||
Background:
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: ContainerDependency
|
||||
In order to follow best practices for Symfony
|
||||
As a Psalm user
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Container service
|
||||
Detect ContainerInterface::get() result type
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Container XML config
|
||||
Detect ContainerInterface::get() result type
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Messenger Envelope
|
||||
|
||||
Background:
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Header get
|
||||
|
||||
Background:
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Naming conventions
|
||||
Detect naming convention violations
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: RepositoryStringShortcut
|
||||
In order to follow best practices for Symfony
|
||||
As a Psalm user
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Request getContent
|
||||
Symfony Request has getContent method on which return type changes based on argument
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
@symfony-common
|
||||
Feature: Tainting
|
||||
|
||||
Background:
|
||||
|
Loading…
x
Reference in New Issue
Block a user