Suppress PropertyNotSetInConstructor error in AbstractController::$container (managed by Symfony DI) (#57)

Co-authored-by: Anton Zagorskii <anton@paytronix.io>
This commit is contained in:
Anton Zagorskii 2020-07-20 04:40:55 +01:00 committed by GitHub
parent 7fb4a082cf
commit 1c68a1529a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 133 additions and 17 deletions

View File

@ -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

View File

@ -54,7 +54,7 @@
"phpunit": "phpunit --testdox",
"codeception": [
"codecept build",
"codecept run -v"
"codecept run -v -g symfony-common"
]
},
"suggest": {

View File

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

View 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;
}

View 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;
}

View 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

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: ConsoleArgument
Background:

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: ConsoleOption
Background:

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: ContainerDependency
In order to follow best practices for Symfony
As a Psalm user

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Container service
Detect ContainerInterface::get() result type

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Container XML config
Detect ContainerInterface::get() result type

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Messenger Envelope
Background:

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Header get
Background:

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Naming conventions
Detect naming convention violations

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: RepositoryStringShortcut
In order to follow best practices for Symfony
As a Psalm user

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Request getContent
Symfony Request has getContent method on which return type changes based on argument

View File

@ -1,3 +1,4 @@
@symfony-common
Feature: Tainting
Background: