From 2d7f883b3f2bc219cdd5a12555e77ff3f8ccf7c3 Mon Sep 17 00:00:00 2001 From: Olivier Dolbeau Date: Fri, 6 Dec 2019 14:41:32 +0100 Subject: [PATCH] Add php-cs-fixer as github action --- .github/workflows/static-code-analysis.yml | 11 +++++++- .php_cs | 24 ++++++++++------- Makefile | 17 ++++++++++-- phpstan-baseline.neon | 16 ----------- src/FileStorage.php | 31 +++------------------- src/XliffConverter.php | 19 +++---------- 6 files changed, 46 insertions(+), 72 deletions(-) diff --git a/.github/workflows/static-code-analysis.yml b/.github/workflows/static-code-analysis.yml index 7049af3..505db17 100644 --- a/.github/workflows/static-code-analysis.yml +++ b/.github/workflows/static-code-analysis.yml @@ -3,7 +3,7 @@ name: Static code analysis on: [pull_request] jobs: - static-code-analysis: + phpstan: runs-on: ubuntu-latest steps: - uses: actions/checkout@master @@ -11,3 +11,12 @@ jobs: uses: docker://jakzal/phpqa:php7.3-alpine with: args: phpstan analyze + + php-cs-fixer: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + - name: Run PHP-CS-Fixer + uses: docker://jakzal/phpqa:php7.3-alpine + with: + args: php-cs-fixer fix --dry-run --diff-format udiff -vvv diff --git a/.php_cs b/.php_cs index 23ba165..2159fe4 100644 --- a/.php_cs +++ b/.php_cs @@ -1,13 +1,17 @@ setRiskyAllowed(true) + ->setRules([ + '@Symfony' => true, + '@Symfony:risky' => true, + ]) + ->setFinder( + PhpCsFixer\Finder::create() + ->in(__DIR__) + ->exclude(__DIR__.'/vendor') + ->name('*.php') + ) +; -use SLLH\StyleCIBridge\ConfigBridge; - -return ConfigBridge::create(); +return $config; diff --git a/Makefile b/Makefile index 5e342f6..7304054 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,18 @@ +.PHONY: ${TARGETS} + DIR := ${CURDIR} QA_IMAGE := jakzal/phpqa:php7.3-alpine -static: - docker run --rm -v $(DIR):/project -w /project $(QA_IMAGE) phpstan analyze +cs-fix: + @docker run --rm -v $(DIR):/project -w /project $(QA_IMAGE) php-cs-fixer fix --diff-format udiff -vvv + +cs-diff: + @docker run --rm -v $(DIR):/project -w /project $(QA_IMAGE) php-cs-fixer fix --diff-format udiff --dry-run -vvv + +phpstan: + @docker run --rm -v $(DIR):/project -w /project $(QA_IMAGE) phpstan analyze + +static: cs-diff phpstan + +test: static + @vendor/bin/phpunit diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c784c61..87b2407 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,22 +1,6 @@ parameters: ignoreErrors: - - - message: "#^Method Translation\\\\SymfonyStorage\\\\FileStorage\\:\\:writeCatalogue\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/FileStorage.php - - - - message: "#^Method Translation\\\\SymfonyStorage\\\\FileStorage\\:\\:writeCatalogue\\(\\) has parameter \\$locale with no typehint specified\\.$#" - count: 1 - path: src/FileStorage.php - - message: "#^Parameter \\#1 \\$catalogue of method Symfony\\\\Component\\\\Translation\\\\Writer\\\\TranslationWriterInterface\\:\\:write\\(\\) expects Symfony\\\\Component\\\\Translation\\\\MessageCatalogue, Symfony\\\\Component\\\\Translation\\\\MessageCatalogueInterface given\\.$#" count: 2 path: src/FileStorage.php - - - - message: "#^Method Translation\\\\SymfonyStorage\\\\FileStorage\\:\\:loadCatalogue\\(\\) has no return typehint specified\\.$#" - count: 1 - path: src/FileStorage.php - diff --git a/src/FileStorage.php b/src/FileStorage.php index bf7333b..f679239 100644 --- a/src/FileStorage.php +++ b/src/FileStorage.php @@ -52,19 +52,13 @@ final class FileStorage implements Storage, TransferableStorage */ private $catalogues; - /** - * @param TranslationWriterInterface $writer - * @param TranslationReaderInterface $reader - * @param array $dir - * @param array $options - */ public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, array $dir, array $options = []) { if (empty($dir)) { throw new \LogicException('Third parameter of FileStorage cannot be empty'); } - if (!array_key_exists('xliff_version', $options)) { + if (!\array_key_exists('xliff_version', $options)) { // Set default value for xliff version. $options['xliff_version'] = '2.0'; } @@ -141,13 +135,7 @@ public function import(MessageCatalogueInterface $catalogue, array $options = [] } } - /** - * Save catalogue back to file. - * - * @param MessageCatalogueInterface $catalogue - * @param string $domain - */ - private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $domain) + private function writeCatalogue(MessageCatalogueInterface $catalogue, string $locale, string $domain): void { $resources = $catalogue->getResources(); $options = $this->options; @@ -171,12 +159,7 @@ private function writeCatalogue(MessageCatalogueInterface $catalogue, $locale, $ $this->writer->write($catalogue, $format, $options); } - /** - * @param string $locale - * - * @return MessageCatalogue - */ - private function getCatalogue($locale) + private function getCatalogue(string $locale): MessageCatalogue { if (empty($this->catalogues[$locale])) { $this->loadCatalogue($locale, $this->dir); @@ -185,13 +168,7 @@ private function getCatalogue($locale) return $this->catalogues[$locale]; } - /** - * Load catalogue from files. - * - * @param string $locale - * @param array $dirs - */ - private function loadCatalogue($locale, array $dirs) + private function loadCatalogue(string $locale, array $dirs): void { $currentCatalogue = new MessageCatalogue($locale); foreach ($dirs as $path) { diff --git a/src/XliffConverter.php b/src/XliffConverter.php index 8b212e7..20b89c3 100644 --- a/src/XliffConverter.php +++ b/src/XliffConverter.php @@ -24,14 +24,8 @@ final class XliffConverter { /** * Create a catalogue from the contents of a XLIFF file. - * - * @param string $content - * @param string $locale - * @param string $domain - * - * @return MessageCatalogue */ - public static function contentToCatalogue($content, $locale, $domain) + public static function contentToCatalogue(string $content, string $locale, string $domain): MessageCatalogue { $file = sys_get_temp_dir().'/'.uniqid('xliff', true); file_put_contents($file, $content); @@ -39,16 +33,9 @@ public static function contentToCatalogue($content, $locale, $domain) return (new XliffFileLoader())->load($file, $locale, $domain); } - /** - * @param MessageCatalogue $catalogue - * @param string $domain - * @param array $options - * - * @return string - */ - public static function catalogueToContent(MessageCatalogue $catalogue, $domain, array $options = []) + public static function catalogueToContent(MessageCatalogue $catalogue, string $domain, array $options = []): string { - if (!array_key_exists('xliff_version', $options)) { + if (!\array_key_exists('xliff_version', $options)) { // Set default value for xliff version. $options['xliff_version'] = '2.0'; }