Skip to content

Commit

Permalink
Add PHPStan integration
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rdex committed Aug 3, 2022
1 parent b8d71ea commit 7ced304
Show file tree
Hide file tree
Showing 88 changed files with 841 additions and 112 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/continuous-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,52 @@ jobs:
- name: 'Run unit tests'
run: |
vendor/bin/phpunit
phpstan:
runs-on: 'ubuntu-latest'
name: 'PHPStan (PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} + ${{ matrix.dependencies }} deps)'
timeout-minutes: 10
env:
SYMFONY_REQUIRE: "${{ matrix.symfony }}"
strategy:
matrix:
php:
- '7.4'
- '8.0'
- '8.1'
symfony:
- '4.4.*'
- '5.4.*'
- '6.0.*'
dependencies:
- 'highest'
include:
- php: '7.4'
dependencies: 'lowest'
symfony: '4.4.*'
exclude:
- php: '7.4'
symfony: '6.0.*'
dependencies: 'highest'
fail-fast: false
steps:
- name: 'Checkout'
uses: 'actions/checkout@v2'

- name: 'Setup PHP'
uses: 'shivammathur/setup-php@v2'
with:
php-version: '${{ matrix.php }}'
coverage: 'none'
tools: 'pecl, composer:v2, flex'
extensions: 'curl, json, mbstring, mongodb, openssl'

- name: "Install Composer dependencies (${{ matrix.dependencies }})"
uses: "ramsey/composer-install@v1"
with:
dependency-versions: "${{ matrix.dependencies }}"
composer-options: "--prefer-dist --prefer-stable"

- name: 'Run PHPStan'
run: |
composer sca
19 changes: 18 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@
"symfony/serializer": "^4.4 || ^5.4 || ^6.0",
"symfony/twig-bundle": "^4.4 || ^5.4 || ^6.0",
"symfony/web-profiler-bundle": "^4.4 || ^5.4 || ^6.0",
"symfony/yaml": "^4.4 || ^5.4 || ^6.0"
"symfony/yaml": "^4.4 || ^5.4 || ^6.0",
"phpstan/phpstan": "^1.4",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-symfony": "^1.2",
"phpstan/phpstan-phpunit": "^1.1"
},
"suggest": {
"enqueue/elastica-bundle": "For populating Elasticsearch indexes asynchronously and using significanly less resources. Uses Enqueue.",
Expand All @@ -78,5 +82,18 @@
"branch-alias": {
"dev-master": "6.1.x-dev"
}
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
},
"scripts": {
"cs": [
"@php vendor/bin/php-cs-fixer fix --allow-risky=yes"
],
"sca": [
"@php vendor/bin/phpstan --memory-limit=256M"
]
}
}
Empty file added phpstan-baseline.neon
Empty file.
28 changes: 28 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
includes:
- phpstan-baseline.neon
parameters:
level: 5
reportUnmatchedIgnoredErrors: false
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: false
paths:
- src
# - tests
ignoreErrors:
- message: /Trying to mock an undefined method/
path: tests/
- message: '/Property .* is never read, only written/'
path: tests/
- message: '/Call to method PHPUnit\\Framework\\Assert::assert\w*\(\).*will always evaluate to (true|false)/'
path: tests/
- message: '/Parameter #\d \$\w+ of class .+ constructor expects array[{<].+[}>], array{.*} given/'
path: tests/
# return type was fixed in psr/log v1.0.2 (https://github.com/php-fig/log/releases/tag/1.0.2)
- message: '/Return type \(void\) of method FOS\\ElasticaBundle\\Logger\\ElasticaLogger::log\(\) should be compatible with return type \(null\) of method Psr\\Log\\LoggerInterface::log\(\)/'
path: src/Logger/ElasticaLogger.php
# it became generic in v3.5.2 (https://github.com/BabDev/Pagerfanta/blob/HEAD/CHANGELOG.md#352-2022-01-24)
- message: '/PHPDoc tag @implements contains generic type Pagerfanta\\Adapter\\AdapterInterface<mixed> but interface Pagerfanta\\Adapter\\AdapterInterface is not generic/'
path: src/Paginator/FantaPaginatorAdapter.php
# there are no Enums in php 7.4
- message: '/(Class BackedEnum not found)|(Access to property \$value on an unknown class BackedEnum)/'
path: src/Transformer/ModelToElasticaAutoTransformer.php
7 changes: 7 additions & 0 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOS\ElasticaBundle\Command;

use FOS\ElasticaBundle\Configuration\ConfigManager;
use FOS\ElasticaBundle\Configuration\IndexConfig;
use FOS\ElasticaBundle\Index\AliasProcessor;
use FOS\ElasticaBundle\Index\IndexManager;
use FOS\ElasticaBundle\Index\MappingBuilder;
Expand Down Expand Up @@ -44,6 +45,9 @@ public function __construct(
$this->aliasProcessor = $aliasProcessor;
}

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

$indexConfig = $this->configManager->getIndexConfiguration($indexName);
if (!$indexConfig instanceof IndexConfig) {
throw new \RuntimeException(\sprintf('Incorrect index configuration object. Expecting IndexConfig, but got: %s ', \get_class($indexConfig)));
}
$index = $this->indexManager->getIndex($indexName);
if ($indexConfig->isUseAlias()) {
$this->aliasProcessor->setRootName($indexConfig, $index);
Expand Down
3 changes: 3 additions & 0 deletions src/Command/DeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function __construct(
$this->indexManager = $indexManager;
}

/**
* @return void
*/
protected function configure()
{
$this
Expand Down
14 changes: 13 additions & 1 deletion src/Command/PopulateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOS\ElasticaBundle\Command;

use Elastica\Exception\Bulk\ResponseException as BulkResponseException;
use FOS\ElasticaBundle\Event\AbstractIndexPopulateEvent;
use FOS\ElasticaBundle\Event\PostIndexPopulateEvent;
use FOS\ElasticaBundle\Event\PreIndexPopulateEvent;
use FOS\ElasticaBundle\Index\IndexManager;
Expand All @@ -30,10 +31,12 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Populate the search index.
*
* @phpstan-import-type TOptions from AbstractIndexPopulateEvent
*/
class PopulateCommand extends Command
{
Expand Down Expand Up @@ -83,6 +86,9 @@ public function __construct(
$this->resetter = $resetter;
}

/**
* @return void
*/
protected function configure()
{
$this
Expand All @@ -103,6 +109,9 @@ protected function configure()
;
}

/**
* @return void
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->pagerPersister = $this->pagerPersisterRegistry->getPagerPersister($input->getOption('pager-persister'));
Expand All @@ -121,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$reset = !$input->getOption('no-reset');
$delete = !$input->getOption('no-delete');

/** @var TOptions $options */
$options = [
'delete' => $delete,
'reset' => $reset,
Expand Down Expand Up @@ -151,6 +161,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

/**
* Recreates an index, populates it, and refreshes it.
*
* @phpstan-param TOptions $options
*/
private function populateIndex(OutputInterface $output, string $index, bool $reset, array $options): void
{
Expand Down
3 changes: 3 additions & 0 deletions src/Command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function __construct(
$this->resetter = $resetter;
}

/**
* @return void
*/
protected function configure()
{
$this
Expand Down
4 changes: 3 additions & 1 deletion src/Command/ResetTemplatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FOS\ElasticaBundle\Index\TemplateResetter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -35,7 +36,7 @@ public function __construct(
}

/**
* {@inheritdoc}
* @return void
*/
protected function configure()
{
Expand Down Expand Up @@ -64,6 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$deleteByPattern = $input->getOption('force-delete');

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

Expand Down
5 changes: 4 additions & 1 deletion src/Command/SearchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function __construct(IndexManager $indexManager)
$this->indexManager = $indexManager;
}

/**
* @return void
*/
protected function configure()
{
$this
Expand All @@ -54,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$indexName = $input->getOption('index');
$index = $this->indexManager->getIndex($indexName ?: null);
$query = Query::create($input->getArgument('query'));
$query->setSize($input->getOption('limit'));
$query->setSize((int) $input->getOption('limit'));
if ($input->getOption('explain')) {
$query->setExplain(true);
}
Expand Down
1 change: 1 addition & 0 deletions src/Configuration/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ConfigManager implements ManagerInterface
{
/**
* @var IndexConfig[]
* @phpstan-var array<string, IndexConfigInterface>
*/
private $indexes = [];

Expand Down
10 changes: 9 additions & 1 deletion src/Configuration/IndexConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace FOS\ElasticaBundle\Configuration;

/**
* @phpstan-import-type TConfig from IndexConfigInterface
*/
class IndexConfig implements IndexConfigInterface
{
use IndexConfigTrait;
Expand All @@ -21,18 +24,23 @@ class IndexConfig implements IndexConfigInterface
*
* @var bool
*/
private $useAlias = false;
private $useAlias;

/**
* Constructor expects an array as generated by the Container Configuration builder.
*
* @phpstan-param TConfig $config
*/
public function __construct(array $config)
{
$this->elasticSearchName = $config['elasticsearch_name'] ?? $config['name'];
$this->name = $config['name'];
// @phpstan-ignore-next-line Ignored because of a bug in PHPStan (https://github.com/phpstan/phpstan/issues/5091)
$this->settings = $config['settings'] ?? [];
$this->useAlias = $config['use_alias'] ?? false;
// @phpstan-ignore-next-line Ignored because of a bug in PHPStan (https://github.com/phpstan/phpstan/issues/5091)
$this->config = $config['config'];
// @phpstan-ignore-next-line Ignored because of a bug in PHPStan (https://github.com/phpstan/phpstan/issues/5091)
$this->mapping = $config['mapping'];
$this->model = $config['model'];
}
Expand Down
32 changes: 32 additions & 0 deletions src/Configuration/IndexConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@
* Interface Index config interface.
*
* @author Dmitry Balabka <[email protected]>
*
* @phpstan-type TMapping = array<string, mixed>
* @phpstan-type TSettings = array<string, mixed>
* @phpstan-type TDynamicDateFormats = list<non-empty-string>
* @phpstan-type TDynamic = true|'runtime'
* @phpstan-type TConfig = array{
* elasticsearch_name?: string,
* name: string,
* settings?: TSettings,
* use_alias?: bool,
* config: TElasticConfig,
* mapping: TMapping,
* model: mixed,
* template?: string,
* }
* @phpstan-type TElasticConfig = array{
* date_detection?: bool,
* dynamic_date_formats?: TDynamicDateFormats,
* analyzer?: string,
* numeric_detection?: bool,
* dynamic?: TDynamic
* }
*/
interface IndexConfigInterface
{
Expand All @@ -24,20 +46,30 @@ public function getModel(): ?string;

public function getName(): string;

/**
* @phpstan-return TSettings
*/
public function getSettings(): array;

public function getDateDetection(): ?bool;

/**
* @phpstan-return ?TDynamicDateFormats
*/
public function getDynamicDateFormats(): ?array;

public function getAnalyzer(): ?string;

/**
* @phpstan-return TMapping
*/
public function getMapping(): array;

public function getNumericDetection(): ?bool;

/**
* @return string|bool|null
* @phpstan-return ?TDynamic
*/
public function getDynamic();
}
Loading

0 comments on commit 7ced304

Please sign in to comment.