From 02fd28edecaf1c2a8f4265110a21d3efa4a01d6b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 20 Jul 2021 13:29:50 +0200 Subject: [PATCH] Installed phpstan-dcotrine and fixed more static analysis errors --- composer.json | 1 + config/cli-config.php | 7 ++----- config/entity-manager.php | 12 ++++++++++++ .../CLI/src/Command/Db/AbstractDatabaseCommand.php | 2 +- .../src/Command/Visit/DownloadGeoLiteDbCommand.php | 4 ++-- module/CLI/src/Command/Visit/LocateVisitsCommand.php | 11 ++++++++--- module/Core/src/Domain/DomainService.php | 1 - module/Core/src/Importer/ImportedLinksProcessor.php | 2 +- phpstan.neon | 4 ++++ 9 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 config/entity-manager.php diff --git a/composer.json b/composer.json index 7193099b..08446f16 100644 --- a/composer.json +++ b/composer.json @@ -67,6 +67,7 @@ "infection/infection": "^0.23.0", "phpspec/prophecy-phpunit": "^2.0", "phpstan/phpstan": "^0.12.92", + "phpstan/phpstan-doctrine": "^0.12.42", "phpstan/phpstan-symfony": "^0.12.41", "phpunit/php-code-coverage": "^9.2", "phpunit/phpunit": "^9.5", diff --git a/config/cli-config.php b/config/cli-config.php index 396fc075..52659e4e 100644 --- a/config/cli-config.php +++ b/config/cli-config.php @@ -4,12 +4,9 @@ declare(strict_types=1); use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Console\ConsoleRunner; -use Psr\Container\ContainerInterface; return (static function () { - /** @var ContainerInterface $container */ - $container = include __DIR__ . '/container.php'; - $em = $container->get(EntityManager::class); - + /** @var EntityManager $em */ + $em = include __DIR__ . '/entity-manager.php'; return ConsoleRunner::createHelperSet($em); })(); diff --git a/config/entity-manager.php b/config/entity-manager.php new file mode 100644 index 00000000..2b4794f7 --- /dev/null +++ b/config/entity-manager.php @@ -0,0 +1,12 @@ +get(EntityManager::class); +})(); diff --git a/module/CLI/src/Command/Db/AbstractDatabaseCommand.php b/module/CLI/src/Command/Db/AbstractDatabaseCommand.php index 1b0a4f9b..9cd6e9ea 100644 --- a/module/CLI/src/Command/Db/AbstractDatabaseCommand.php +++ b/module/CLI/src/Command/Db/AbstractDatabaseCommand.php @@ -32,6 +32,6 @@ abstract class AbstractDatabaseCommand extends AbstractLockedCommand protected function getLockConfig(): LockedCommandConfig { - return LockedCommandConfig::blocking($this->getName()); + return LockedCommandConfig::blocking($this->getName() ?? static::class); } } diff --git a/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php b/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php index aef30427..41fb5f8d 100644 --- a/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php +++ b/module/CLI/src/Command/Visit/DownloadGeoLiteDbCommand.php @@ -45,8 +45,8 @@ class DownloadGeoLiteDbCommand extends Command $io->text(sprintf('%s GeoLite2 db file...', $olderDbExists ? 'Updating' : 'Downloading')); $this->progressBar = new ProgressBar($io); }, function (int $total, int $downloaded): void { - $this->progressBar->setMaxSteps($total); - $this->progressBar->setProgress($downloaded); + $this->progressBar?->setMaxSteps($total); + $this->progressBar?->setProgress($downloaded); }); if ($this->progressBar === null) { diff --git a/module/CLI/src/Command/Visit/LocateVisitsCommand.php b/module/CLI/src/Command/Visit/LocateVisitsCommand.php index 31b1fb05..7352211e 100644 --- a/module/CLI/src/Command/Visit/LocateVisitsCommand.php +++ b/module/CLI/src/Command/Visit/LocateVisitsCommand.php @@ -139,7 +139,7 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat throw IpCannotBeLocatedException::forEmptyAddress(); } - $ipAddr = $visit->getRemoteAddr(); + $ipAddr = $visit->getRemoteAddr() ?? ''; $this->io->write(sprintf('Processing IP %s', $ipAddr)); if ($ipAddr === IpAddress::LOCALHOST) { $this->io->writeln(' [Ignored localhost address]'); @@ -168,7 +168,12 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat private function checkDbUpdate(InputInterface $input): void { - $downloadDbCommand = $this->getApplication()->find(DownloadGeoLiteDbCommand::NAME); + $cliApp = $this->getApplication(); + if ($cliApp === null) { + return; + } + + $downloadDbCommand = $cliApp->find(DownloadGeoLiteDbCommand::NAME); $exitCode = $downloadDbCommand->run($input, $this->io); if ($exitCode === ExitCodes::EXIT_FAILURE) { @@ -178,6 +183,6 @@ class LocateVisitsCommand extends AbstractLockedCommand implements VisitGeolocat protected function getLockConfig(): LockedCommandConfig { - return LockedCommandConfig::nonBlocking($this->getName()); + return LockedCommandConfig::nonBlocking(self::NAME); } } diff --git a/module/Core/src/Domain/DomainService.php b/module/Core/src/Domain/DomainService.php index 95ba05c5..1e1ac279 100644 --- a/module/Core/src/Domain/DomainService.php +++ b/module/Core/src/Domain/DomainService.php @@ -57,7 +57,6 @@ class DomainService implements DomainServiceInterface public function getOrCreate(string $authority): Domain { $repo = $this->em->getRepository(Domain::class); - /** @var Domain|null $domain */ $domain = $repo->findOneBy(['authority' => $authority]) ?? new Domain($authority); $this->em->persist($domain); diff --git a/module/Core/src/Importer/ImportedLinksProcessor.php b/module/Core/src/Importer/ImportedLinksProcessor.php index 6073dd26..b153430b 100644 --- a/module/Core/src/Importer/ImportedLinksProcessor.php +++ b/module/Core/src/Importer/ImportedLinksProcessor.php @@ -28,7 +28,7 @@ class ImportedLinksProcessor implements ImportedLinksProcessorInterface private ShortCodeHelperInterface $shortCodeHelper, private DoctrineBatchHelperInterface $batchHelper ) { - $this->shortUrlRepo = $this->em->getRepository(ShortUrl::class); // @phpstan-ignore-line + $this->shortUrlRepo = $this->em->getRepository(ShortUrl::class); } /** diff --git a/phpstan.neon b/phpstan.neon index 7d90c219..bf3afc8e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,11 @@ includes: + - vendor/phpstan/phpstan-doctrine/extension.neon - vendor/phpstan/phpstan-symfony/extension.neon parameters: checkMissingIterableValueType: false checkGenericClassInNonGenericObjectType: false symfony: console_application_loader: 'config/cli-app.php' + doctrine: + repositoryClass: Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository + objectManagerLoader: 'config/entity-manager.php'