From 1aa5c380c66d8948400fbe3b7cb717ed9a6bb8df Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 7 Oct 2021 23:09:17 +0200 Subject: [PATCH] Increase psalm / phpstan level --- composer.json | 1 + phpstan.neon.dist | 2 +- psalm-baseline.xml | 8 ++++++++ psalm.xml | 2 +- src/Command/SitemapGeneratorCommand.php | 16 ++++++++++++++-- .../Compiler/ServiceCompilerPass.php | 1 + src/DependencyInjection/SonataSeoExtension.php | 1 + src/Sitemap/SourceManager.php | 4 +++- src/Twig/Extension/SeoExtension.php | 6 +++--- .../Compiler/ServiceCompilerPassTest.php | 2 ++ tests/DependencyInjection/ConfigurationTest.php | 1 + tests/Seo/SeoPageTest.php | 7 ------- 12 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 psalm-baseline.xml diff --git a/composer.json b/composer.json index c113b37b..00e85d2f 100644 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ "phpstan/extension-installer": "^1.0", "phpstan/phpstan": "^0.12.84", "phpstan/phpstan-phpunit": "^0.12.16", + "phpstan/phpstan-strict-rules": "^0.12.10", "phpstan/phpstan-symfony": "^0.12.44", "psalm/plugin-phpunit": "^0.15", "symfony/console": "^4.4 || ^5.3", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 16fd2cfd..84dc4f81 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,7 +2,7 @@ includes: - phpstan-baseline.neon parameters: - level: 6 + level: 8 bootstrapFiles: - vendor/bin/.phpunit/phpunit/vendor/autoload.php paths: diff --git a/psalm-baseline.xml b/psalm-baseline.xml new file mode 100644 index 00000000..0115e555 --- /dev/null +++ b/psalm-baseline.xml @@ -0,0 +1,8 @@ + + + + + children + + + diff --git a/psalm.xml b/psalm.xml index 06733cc6..33c91fdd 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,5 +1,5 @@ - + diff --git a/src/Command/SitemapGeneratorCommand.php b/src/Command/SitemapGeneratorCommand.php index 8268bfce..2d7ea031 100644 --- a/src/Command/SitemapGeneratorCommand.php +++ b/src/Command/SitemapGeneratorCommand.php @@ -170,12 +170,24 @@ private function moveTemporaryFile(string $tempDir, string $permanentDir): void { $oldFiles = Finder::create()->files()->name('sitemap*.xml')->in($permanentDir); foreach ($oldFiles as $file) { - $this->filesystem->remove($file->getRealPath()); + $pathname = $file->getRealPath(); + + if (false === $pathname) { + throw new \LogicException(sprintf('File %s does not exist', $file)); + } + + $this->filesystem->remove($pathname); } $newFiles = Finder::create()->files()->name('sitemap*.xml')->in($tempDir); foreach ($newFiles as $file) { - $this->filesystem->rename($file->getRealPath(), sprintf('%s/%s', $permanentDir, $file->getFilename())); + $pathname = $file->getRealPath(); + + if (false === $pathname) { + throw new \LogicException(sprintf('File %s does not exist', $file)); + } + + $this->filesystem->rename($pathname, sprintf('%s/%s', $permanentDir, $file->getFilename())); } } } diff --git a/src/DependencyInjection/Compiler/ServiceCompilerPass.php b/src/DependencyInjection/Compiler/ServiceCompilerPass.php index 173d6172..ccc38c30 100644 --- a/src/DependencyInjection/Compiler/ServiceCompilerPass.php +++ b/src/DependencyInjection/Compiler/ServiceCompilerPass.php @@ -24,6 +24,7 @@ final class ServiceCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { + /** @var array $config */ $config = $container->getParameter('sonata.seo.config'); $definition = $container->findDefinition($config['default']); diff --git a/src/DependencyInjection/SonataSeoExtension.php b/src/DependencyInjection/SonataSeoExtension.php index 4d7a517a..f058e962 100644 --- a/src/DependencyInjection/SonataSeoExtension.php +++ b/src/DependencyInjection/SonataSeoExtension.php @@ -34,6 +34,7 @@ public function load(array $configs, ContainerBuilder $container): void $config = $this->processConfiguration($configuration, $configs); $config = $this->fixConfiguration($config); + /** @var array $bundles */ $bundles = $container->getParameter('kernel.bundles'); $loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); diff --git a/src/Sitemap/SourceManager.php b/src/Sitemap/SourceManager.php index bc35d408..3c156cda 100644 --- a/src/Sitemap/SourceManager.php +++ b/src/Sitemap/SourceManager.php @@ -39,10 +39,12 @@ public function __construct() */ public function addSource(string $group, SourceIteratorInterface $source, array $types = []): void { - if (!isset($this->sources[$group])) { + if (!isset($this->sources[$group]) || null === $this->sources[$group]) { $this->sources[$group] = new Source(); } + \assert(null !== $this->sources[$group]); + $this->sources[$group]->addSource($source); if ([] !== $types) { diff --git a/src/Twig/Extension/SeoExtension.php b/src/Twig/Extension/SeoExtension.php index 848bf5cb..29396de4 100644 --- a/src/Twig/Extension/SeoExtension.php +++ b/src/Twig/Extension/SeoExtension.php @@ -67,10 +67,10 @@ public function getMetadatas(): string { $html = ''; foreach ($this->page->getMetas() as $type => $metas) { - foreach ((array) $metas as $name => $meta) { + foreach ($metas as $name => $meta) { [$content, $extras] = $meta; - if (!empty($content)) { + if ('' !== $content) { $html .= sprintf( "\n", $type, @@ -112,7 +112,7 @@ public function getHeadAttributes(): string public function getLinkCanonical(): string { - if ($this->page->getLinkCanonical()) { + if ('' !== $this->page->getLinkCanonical()) { return sprintf("\n", $this->page->getLinkCanonical()); } diff --git a/tests/DependencyInjection/Compiler/ServiceCompilerPassTest.php b/tests/DependencyInjection/Compiler/ServiceCompilerPassTest.php index 002d65de..62b04932 100644 --- a/tests/DependencyInjection/Compiler/ServiceCompilerPassTest.php +++ b/tests/DependencyInjection/Compiler/ServiceCompilerPassTest.php @@ -69,6 +69,8 @@ public function testGlobalTitle(): void $page = $container->get('sonata.seo.custom.page'); + \assert($page instanceof SeoPageInterface); + static::assertSame('Project name', $page->getOriginalTitle()); static::assertSame('Prefix Project name Suffix', $page->getTitle()); } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index 77ce510b..7c04f2c8 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -68,6 +68,7 @@ public function testKeysAreNotNormalized(): void public function testWithYamlConfig(): void { $values = Yaml::parse( + /* @phpstan-ignore-next-line */ file_get_contents(__DIR__.'/data/config.yml'), Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE ); diff --git a/tests/Seo/SeoPageTest.php b/tests/Seo/SeoPageTest.php index 70d9e38c..cafd2d44 100644 --- a/tests/Seo/SeoPageTest.php +++ b/tests/Seo/SeoPageTest.php @@ -175,11 +175,4 @@ public function testHasMeta(): void static::assertTrue($page->hasMeta('property', 'test')); static::assertFalse($page->hasMeta('property', 'fake')); } - - public function testSetSeparator(): void - { - $page = new SeoPage(); - - static::assertInstanceOf(SeoPage::class, $page->setSeparator('-')); - } }