Skip to content

Commit ab53217

Browse files
authored
Merge pull request #1874 from b1rdex/phpstan
PHPStan integration
2 parents a1b44ab + 33936e0 commit ab53217

88 files changed

Lines changed: 842 additions & 111 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/continuous-integration.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,52 @@ jobs:
9090
- name: 'Run unit tests'
9191
run: |
9292
vendor/bin/phpunit
93+
94+
phpstan:
95+
runs-on: 'ubuntu-latest'
96+
name: 'PHPStan (PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} + ${{ matrix.dependencies }} deps)'
97+
timeout-minutes: 10
98+
env:
99+
SYMFONY_REQUIRE: "${{ matrix.symfony }}"
100+
strategy:
101+
matrix:
102+
php:
103+
- '7.4'
104+
- '8.0'
105+
- '8.1'
106+
symfony:
107+
- '4.4.*'
108+
- '5.4.*'
109+
- '6.0.*'
110+
dependencies:
111+
- 'highest'
112+
include:
113+
- php: '7.4'
114+
dependencies: 'lowest'
115+
symfony: '4.4.*'
116+
exclude:
117+
- php: '7.4'
118+
symfony: '6.0.*'
119+
dependencies: 'highest'
120+
fail-fast: false
121+
steps:
122+
- name: 'Checkout'
123+
uses: 'actions/checkout@v2'
124+
125+
- name: 'Setup PHP'
126+
uses: 'shivammathur/setup-php@v2'
127+
with:
128+
php-version: '${{ matrix.php }}'
129+
coverage: 'none'
130+
tools: 'pecl, composer:v2, flex'
131+
extensions: 'curl, json, mbstring, mongodb, openssl'
132+
133+
- name: "Install Composer dependencies (${{ matrix.dependencies }})"
134+
uses: "ramsey/composer-install@v1"
135+
with:
136+
dependency-versions: "${{ matrix.dependencies }}"
137+
composer-options: "--prefer-dist --prefer-stable"
138+
139+
- name: 'Run PHPStan'
140+
run: |
141+
composer sca

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"doctrine/mongodb-odm": "^2.2",
4545
"doctrine/orm": "^2.8",
4646
"doctrine/phpcr-odm": "^1.4",
47+
"ergebnis/composer-normalize": "^2.28",
4748
"friendsofphp/php-cs-fixer": "^3.0",
4849
"jackalope/jackalope-doctrine-dbal": "^1.2",
4950
"jms/serializer": "^3.8",
@@ -52,6 +53,10 @@
5253
"pagerfanta/doctrine-mongodb-odm-adapter": "^2.4 || ^3.0",
5354
"pagerfanta/doctrine-orm-adapter": "^2.4 || ^3.0",
5455
"pagerfanta/doctrine-phpcr-odm-adapter": "^2.4 || ^3.0",
56+
"phpstan/extension-installer": "^1.1",
57+
"phpstan/phpstan": "^1.4",
58+
"phpstan/phpstan-phpunit": "^1.1",
59+
"phpstan/phpstan-symfony": "^1.2",
5560
"phpunit/phpunit": "^9.5",
5661
"symfony/expression-language": "^4.4 || ^5.4 || ^6.0",
5762
"symfony/messenger": "^4.4 || ^5.4 || ^6.0",
@@ -74,9 +79,23 @@
7479
"FOS\\ElasticaBundle\\Tests\\": "tests/"
7580
}
7681
},
82+
"config": {
83+
"allow-plugins": {
84+
"phpstan/extension-installer": true,
85+
"ergebnis/composer-normalize": true
86+
}
87+
},
7788
"extra": {
7889
"branch-alias": {
7990
"dev-master": "6.1.x-dev"
8091
}
92+
},
93+
"scripts": {
94+
"cs": [
95+
"@php vendor/bin/php-cs-fixer fix --allow-risky=yes"
96+
],
97+
"sca": [
98+
"@php vendor/bin/phpstan --memory-limit=256M"
99+
]
81100
}
82101
}

phpstan-baseline.neon

Whitespace-only changes.

phpstan.dist.neon

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
includes:
2+
- phpstan-baseline.neon
3+
parameters:
4+
level: 5
5+
reportUnmatchedIgnoredErrors: false
6+
inferPrivatePropertyTypeFromConstructor: true
7+
treatPhpDocTypesAsCertain: false
8+
paths:
9+
- src
10+
# - tests
11+
ignoreErrors:
12+
- message: /Trying to mock an undefined method/
13+
path: tests/
14+
- message: '/Property .* is never read, only written/'
15+
path: tests/
16+
- message: '/Call to method PHPUnit\\Framework\\Assert::assert\w*\(\).*will always evaluate to (true|false)/'
17+
path: tests/
18+
- message: '/Parameter #\d \$\w+ of class .+ constructor expects array[{<].+[}>], array{.*} given/'
19+
path: tests/
20+
# return type was fixed in psr/log v1.0.2 (https://github.com/php-fig/log/releases/tag/1.0.2)
21+
- message: '/Return type \(void\) of method FOS\\ElasticaBundle\\Logger\\ElasticaLogger::log\(\) should be compatible with return type \(null\) of method Psr\\Log\\LoggerInterface::log\(\)/'
22+
path: src/Logger/ElasticaLogger.php
23+
# it became generic in v3.5.2 (https://github.com/BabDev/Pagerfanta/blob/HEAD/CHANGELOG.md#352-2022-01-24)
24+
- message: '/PHPDoc tag @implements contains generic type Pagerfanta\\Adapter\\AdapterInterface<mixed> but interface Pagerfanta\\Adapter\\AdapterInterface is not generic/'
25+
path: src/Paginator/FantaPaginatorAdapter.php
26+
# there are no Enums in php 7.4
27+
- message: '/(Class BackedEnum not found)|(Access to property \$value on an unknown class BackedEnum)/'
28+
path: src/Transformer/ModelToElasticaAutoTransformer.php

src/Command/CreateCommand.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\ElasticaBundle\Command;
1313

1414
use FOS\ElasticaBundle\Configuration\ConfigManager;
15+
use FOS\ElasticaBundle\Configuration\IndexConfig;
1516
use FOS\ElasticaBundle\Index\AliasProcessor;
1617
use FOS\ElasticaBundle\Index\IndexManager;
1718
use FOS\ElasticaBundle\Index\MappingBuilder;
@@ -44,6 +45,9 @@ public function __construct(
4445
$this->aliasProcessor = $aliasProcessor;
4546
}
4647

48+
/**
49+
* @return void
50+
*/
4751
protected function configure()
4852
{
4953
$this
@@ -62,6 +66,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6266
$output->writeln(\sprintf('<info>Creating</info> <comment>%s</comment>', $indexName));
6367

6468
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
69+
if (!$indexConfig instanceof IndexConfig) {
70+
throw new \RuntimeException(\sprintf('Incorrect index configuration object. Expecting IndexConfig, but got: %s ', \get_class($indexConfig)));
71+
}
6572
$index = $this->indexManager->getIndex($indexName);
6673
if ($indexConfig->isUseAlias()) {
6774
$this->aliasProcessor->setRootName($indexConfig, $index);

src/Command/DeleteCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ public function __construct(
3838
$this->indexManager = $indexManager;
3939
}
4040

41+
/**
42+
* @return void
43+
*/
4144
protected function configure()
4245
{
4346
$this

src/Command/PopulateCommand.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOS\ElasticaBundle\Command;
1313

1414
use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
15+
use FOS\ElasticaBundle\Event\AbstractIndexPopulateEvent;
1516
use FOS\ElasticaBundle\Event\PostIndexPopulateEvent;
1617
use FOS\ElasticaBundle\Event\PreIndexPopulateEvent;
1718
use FOS\ElasticaBundle\Index\IndexManager;
@@ -30,10 +31,12 @@
3031
use Symfony\Component\Console\Input\InputOption;
3132
use Symfony\Component\Console\Output\OutputInterface;
3233
use Symfony\Component\Console\Question\Question;
33-
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
34+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3435

3536
/**
3637
* Populate the search index.
38+
*
39+
* @phpstan-import-type TOptions from AbstractIndexPopulateEvent
3740
*/
3841
class PopulateCommand extends Command
3942
{
@@ -83,6 +86,9 @@ public function __construct(
8386
$this->resetter = $resetter;
8487
}
8588

89+
/**
90+
* @return void
91+
*/
8692
protected function configure()
8793
{
8894
$this
@@ -103,6 +109,9 @@ protected function configure()
103109
;
104110
}
105111

112+
/**
113+
* @return void
114+
*/
106115
protected function initialize(InputInterface $input, OutputInterface $output)
107116
{
108117
$this->pagerPersister = $this->pagerPersisterRegistry->getPagerPersister($input->getOption('pager-persister'));
@@ -121,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121130
$reset = !$input->getOption('no-reset');
122131
$delete = !$input->getOption('no-delete');
123132

133+
/** @var TOptions $options */
124134
$options = [
125135
'delete' => $delete,
126136
'reset' => $reset,
@@ -151,6 +161,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
151161

152162
/**
153163
* Recreates an index, populates it, and refreshes it.
164+
*
165+
* @phpstan-param TOptions $options
154166
*/
155167
private function populateIndex(OutputInterface $output, string $index, bool $reset, array $options): void
156168
{

src/Command/ResetCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function __construct(
3636
$this->resetter = $resetter;
3737
}
3838

39+
/**
40+
* @return void
41+
*/
3942
protected function configure()
4043
{
4144
$this

src/Command/ResetTemplatesCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use FOS\ElasticaBundle\Index\TemplateResetter;
1515
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Helper\QuestionHelper;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
@@ -35,7 +36,7 @@ public function __construct(
3536
}
3637

3738
/**
38-
* {@inheritdoc}
39+
* @return void
3940
*/
4041
protected function configure()
4142
{
@@ -64,6 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6465
$deleteByPattern = $input->getOption('force-delete');
6566

6667
if ($input->isInteractive() && $deleteByPattern) {
68+
/** @var QuestionHelper $helper */
6769
$helper = $this->getHelper('question');
6870
$question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
6971

src/Command/SearchCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public function __construct(IndexManager $indexManager)
3434
$this->indexManager = $indexManager;
3535
}
3636

37+
/**
38+
* @return void
39+
*/
3740
protected function configure()
3841
{
3942
$this
@@ -54,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5457
$indexName = $input->getOption('index');
5558
$index = $this->indexManager->getIndex($indexName ?: null);
5659
$query = Query::create($input->getArgument('query'));
57-
$query->setSize($input->getOption('limit'));
60+
$query->setSize((int) $input->getOption('limit'));
5861
if ($input->getOption('explain')) {
5962
$query->setExplain(true);
6063
}

0 commit comments

Comments
 (0)