From a36909011b085eb91aa6a0579a14859edbd9d234 Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Mon, 28 Oct 2024 10:40:56 +0100 Subject: [PATCH] feat: Replace `squizlabs/php_codesniffer` package with `friendsofphp/php-cs-fixer` --- .gitignore | 9 ++++--- .gitlab-ci.yml | 2 +- .php-cs-fixer.dist.php | 25 +++++++++++++++++++ Makefile | 2 +- composer.json | 2 +- composer.json.dist | 10 +++++--- composer_dev.json.dist | 10 +++++--- phpcs.xml.dist | 17 ------------- public/index.php | 2 ++ src/Api/Model/CommonContent.php | 10 ++++---- src/Controller/GetCommonContentController.php | 15 +++++------ src/Doctrine/ProjectVersionComparator.php | 2 ++ src/Kernel.php | 12 +++++---- .../Normalizer/MenuLinkPathNormalizer.php | 7 +++--- src/TreeWalker/MenuNodeSourceWalker.php | 8 +----- tests/bootstrap.php | 2 ++ 16 files changed, 75 insertions(+), 60 deletions(-) create mode 100644 .php-cs-fixer.dist.php delete mode 100644 phpcs.xml.dist diff --git a/.gitignore b/.gitignore index 04a3e56..8eb4f37 100644 --- a/.gitignore +++ b/.gitignore @@ -34,10 +34,6 @@ compose.override.yml .phpcs-cache report.txt -###> squizlabs/php_codesniffer ### -/.phpcs-cache -/phpcs.xml -###< squizlabs/php_codesniffer ### openapi.json /*.sql /*.zip @@ -59,3 +55,8 @@ openapi.json !/var/export/.gitkeep supervisord.pid + +###> friendsofphp/php-cs-fixer ### +/.php-cs-fixer.php +/.php-cs-fixer.cache +###< friendsofphp/php-cs-fixer ### diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5620939..98c1f96 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -53,7 +53,7 @@ roadiz_skeleton_test: #- ssh-keyscan -H 'github.com' >> ~/.ssh/known_hosts script: - composer install - - php -d memory_limit=-1 vendor/bin/phpcs -p ./src + - php -d memory_limit=-1 vendor/bin/php-cs-fixer check --ansi -vvv - php -d memory_limit=-1 vendor/bin/phpstan analyse -c phpstan.neon - php -d memory_limit=-1 vendor/bin/phpunit --colors=never diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..b177532 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,25 @@ +in(__DIR__) + ->exclude([ + 'var', + 'docker', + 'config', + // Do not fix generated PHP files + 'src/GeneratedEntity', + 'vendor', + '.data', + '.github', + ]) +; + +return (new PhpCsFixer\Config()) + ->setRules([ + '@Symfony' => true, + 'blank_line_after_opening_tag' => true, + 'declare_strict_types' => true, + ]) + ->setRiskyAllowed(true) + ->setFinder($finder) +; diff --git a/Makefile b/Makefile index 8fab94f..deeb11b 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ cache: docker compose exec -u www-data app php bin/console messenger:stop-workers test: - docker compose exec -u www-data app php -d "memory_limit=-1" vendor/bin/phpcbf --report=full --report-file=./report.txt -p ./src + docker compose exec -u www-data app php -d "memory_limit=-1" vendor/bin/php-cs-fixer fix --ansi -vvv docker compose exec -u www-data app php -d "memory_limit=-1" vendor/bin/phpstan analyse -c phpstan.neon XDEBUG_MODE=coverage php -d "memory_limit=-1" vendor/bin/phpunit diff --git a/composer.json b/composer.json index 6a99ca1..0714415 100644 --- a/composer.json +++ b/composer.json @@ -17,12 +17,12 @@ "symfony/requirements-checker": "^2.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.64", "phpstan/phpstan": "^1.10", "phpstan/phpstan-doctrine": "^1.3.1", "phpstan/phpstan-symfony": "^1.3.7", "phpunit/phpunit": "^9.5", "rezozero/intervention-request-bundle": "~3.0.0", - "squizlabs/php_codesniffer": "^3.6", "symfony/browser-kit": "6.4.*", "symfony/css-selector": "6.4.*", "symfony/debug-bundle": "6.4.*", diff --git a/composer.json.dist b/composer.json.dist index 2034b70..0714415 100644 --- a/composer.json.dist +++ b/composer.json.dist @@ -17,12 +17,12 @@ "symfony/requirements-checker": "^2.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.64", "phpstan/phpstan": "^1.10", "phpstan/phpstan-doctrine": "^1.3.1", "phpstan/phpstan-symfony": "^1.3.7", "phpunit/phpunit": "^9.5", "rezozero/intervention-request-bundle": "~3.0.0", - "squizlabs/php_codesniffer": "^3.6", "symfony/browser-kit": "6.4.*", "symfony/css-selector": "6.4.*", "symfony/debug-bundle": "6.4.*", @@ -70,16 +70,18 @@ "requirements-checker": "script" }, "post-install-cmd": [ - "@auto-scripts" + "@auto-scripts", + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ], "post-create-project-cmd": [ "bin/console secrets:generate-keys", "bin/console secrets:set JWT_PASSPHRASE --random", "bin/console secrets:set APP_SECRET --random", - "bin/console lexik:jwt:generate-keypair" + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ], "post-update-cmd": [ - "@auto-scripts" + "@auto-scripts", + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ] }, "conflict": { diff --git a/composer_dev.json.dist b/composer_dev.json.dist index 33dde6a..e5569a2 100644 --- a/composer_dev.json.dist +++ b/composer_dev.json.dist @@ -18,12 +18,12 @@ "symfony/requirements-checker": "^2.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.64", "phpstan/phpstan": "^1.10", "phpstan/phpstan-doctrine": "^1.3.1", "phpstan/phpstan-symfony": "^1.3.7", "phpunit/phpunit": "^9.5", "rezozero/intervention-request-bundle": "~3.0.0", - "squizlabs/php_codesniffer": "^3.6", "symfony/browser-kit": "6.4.*", "symfony/css-selector": "6.4.*", "symfony/debug-bundle": "6.4.*", @@ -71,16 +71,18 @@ "requirements-checker": "script" }, "post-install-cmd": [ - "@auto-scripts" + "@auto-scripts", + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ], "post-create-project-cmd": [ "bin/console secrets:generate-keys", "bin/console secrets:set JWT_PASSPHRASE --random", "bin/console secrets:set APP_SECRET --random", - "bin/console lexik:jwt:generate-keypair" + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ], "post-update-cmd": [ - "@auto-scripts" + "@auto-scripts", + "bin/console lexik:jwt:generate-keypair --skip-if-exists" ] }, "conflict": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist deleted file mode 100644 index 53ec356..0000000 --- a/phpcs.xml.dist +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - bin/ - config/ - public/ - src/ - tests/ - diff --git a/public/index.php b/public/index.php index 9982c21..97e228d 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,7 @@ createNotFoundException($exception->getMessage(), $exception); @@ -63,11 +64,12 @@ private function getErrorPage(TranslationInterface $translation): ?NodesSources if (!class_exists('\App\GeneratedEntity\NSErrorPage')) { return null; } + // @phpstan-ignore-next-line return $this->managerRegistry->getRepository('\App\GeneratedEntity\NSErrorPage') ->findOneBy([ 'translation' => $translation, - 'node.nodeName' => 'error-page' + 'node.nodeName' => 'error-page', ]); } @@ -98,8 +100,9 @@ private function getTranslationFromRequest(?Request $request): TranslationInterf ->findOneAvailableByLocaleOrOverrideLocale((string) $locale); } if (null === $translation) { - throw new NotFoundHttpException('No translation for locale ' . $locale); + throw new NotFoundHttpException('No translation for locale '.$locale); } + return $translation; } @@ -107,11 +110,9 @@ private function getTranslationRepository(): TranslationRepository { $repository = $this->managerRegistry->getRepository(TranslationInterface::class); if (!$repository instanceof TranslationRepository) { - throw new \RuntimeException( - 'Translation repository must be instance of ' . - TranslationRepository::class - ); + throw new \RuntimeException('Translation repository must be instance of '.TranslationRepository::class); } + return $repository; } } diff --git a/src/Doctrine/ProjectVersionComparator.php b/src/Doctrine/ProjectVersionComparator.php index 9160883..0434164 100644 --- a/src/Doctrine/ProjectVersionComparator.php +++ b/src/Doctrine/ProjectVersionComparator.php @@ -12,6 +12,7 @@ class ProjectVersionComparator implements Comparator private function getClassname(Version $version): string { $tokens = explode('\\', (string) $version); + return $tokens[count($tokens) - 1]; } @@ -19,6 +20,7 @@ public function compare(Version $a, Version $b): int { $classA = $this->getClassname($a); $classB = $this->getClassname($b); + /* * Only compare class-name timestamp */ diff --git a/src/Kernel.php b/src/Kernel.php index cf4518d..ccb1f38 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -1,5 +1,7 @@ import('../config/{packages}/*.yaml'); - $container->import('../config/{packages}/' . $this->environment . '/*.yaml'); + $container->import('../config/{packages}/'.$this->environment.'/*.yaml'); - if (is_file(\dirname(__DIR__) . '/config/services.yaml')) { + if (is_file(\dirname(__DIR__).'/config/services.yaml')) { $container->import('../config/services.yaml'); - $container->import('../config/{services}_' . $this->environment . '.yaml'); + $container->import('../config/{services}_'.$this->environment.'.yaml'); } else { $container->import('../config/{services}.php'); } @@ -26,10 +28,10 @@ protected function configureContainer(ContainerConfigurator $container): void protected function configureRoutes(RoutingConfigurator $routes): void { - $routes->import('../config/{routes}/' . $this->environment . '/*.yaml'); + $routes->import('../config/{routes}/'.$this->environment.'/*.yaml'); $routes->import('../config/{routes}/*.yaml'); - if (is_file(\dirname(__DIR__) . '/config/routes.yaml')) { + if (is_file(\dirname(__DIR__).'/config/routes.yaml')) { $routes->import('../config/routes.yaml'); } else { $routes->import('../config/{routes}.php'); diff --git a/src/Serializer/Normalizer/MenuLinkPathNormalizer.php b/src/Serializer/Normalizer/MenuLinkPathNormalizer.php index 8c40f7e..41c397e 100644 --- a/src/Serializer/Normalizer/MenuLinkPathNormalizer.php +++ b/src/Serializer/Normalizer/MenuLinkPathNormalizer.php @@ -11,10 +11,8 @@ final class MenuLinkPathNormalizer extends AbstractPathNormalizer { /** - * @param mixed $object - * @param string|null $format - * @param array $context * @return array|\ArrayObject|bool|float|int|mixed|string|null + * * @throws \Symfony\Component\Serializer\Exception\ExceptionInterface */ public function normalize(mixed $object, ?string $format = null, array $context = []): mixed @@ -26,12 +24,13 @@ public function normalize(mixed $object, ?string $format = null, array $context $actualUrl = $this->urlGenerator->generate( RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [ - RouteObjectInterface::ROUTE_OBJECT => $object->getLinkInternalReferenceSources()[0] + RouteObjectInterface::ROUTE_OBJECT => $object->getLinkInternalReferenceSources()[0], ] ); } $data['url'] = $actualUrl; } + return $data; } } diff --git a/src/TreeWalker/MenuNodeSourceWalker.php b/src/TreeWalker/MenuNodeSourceWalker.php index 32fe686..aacb564 100644 --- a/src/TreeWalker/MenuNodeSourceWalker.php +++ b/src/TreeWalker/MenuNodeSourceWalker.php @@ -10,9 +10,6 @@ use RZ\TreeWalker\AbstractCycleAwareWalker; use RZ\TreeWalker\Definition\ZeroChildrenDefinition; -/** - * @package App\TreeWalker - */ final class MenuNodeSourceWalker extends AbstractCycleAwareWalker { protected function initializeDefinitions(): void @@ -35,10 +32,7 @@ protected function createDefinitionForNodeType(NodeTypeInterface $nodeType): cal { $context = $this->getContext(); if (!$context instanceof NodeSourceWalkerContext) { - throw new \InvalidArgumentException( - 'TreeWalker context must be instance of ' . - NodeSourceWalkerContext::class - ); + throw new \InvalidArgumentException('TreeWalker context must be instance of '.NodeSourceWalkerContext::class); } $childrenNodeTypes = $context->getNodeTypeResolver()->getChildrenNodeTypeList($nodeType); if (count($childrenNodeTypes) > 0) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 469dcce..cc712a1 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,5 +1,7 @@