From 503547a2e3d4f0914b7dc88f83f38dfe264f7668 Mon Sep 17 00:00:00 2001 From: Krzysztof Rewak Date: Tue, 11 Jun 2024 09:16:34 +0200 Subject: [PATCH] #101 - lowercased keywords (#121) * #87 - no empty lines after docblocks * #101 - lowercased constants * - caching and parallel running * - dependencies hell * - csf --- composer.json | 10 ++-- src/Config.php | 58 ++++++++++++++++++- src/Configuration/Defaults/CommonRules.php | 4 ++ tests/codestyle/CodestyleTestCase.php | 1 - tests/codestyle/CommonRulesetTest.php | 14 +++-- tests/codestyle/IgnoreMarkedFilesTest.php | 1 - tests/fixtures/lowercaseKeywords/actual.php | 2 + tests/fixtures/lowercaseKeywords/expected.php | 2 + 8 files changed, 77 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 606228d..22f041b 100644 --- a/composer.json +++ b/composer.json @@ -5,13 +5,13 @@ "type": "library", "require": { "php": "^8.1", - "friendsofphp/php-cs-fixer": "^3.49", - "kubawerlos/php-cs-fixer-custom-fixers": "^3.20" + "friendsofphp/php-cs-fixer": "^3.58", + "kubawerlos/php-cs-fixer-custom-fixers": "^3.21" }, "require-dev": { - "jetbrains/phpstorm-attributes": "^1.0", - "phpunit/phpunit": "^10.0", - "symfony/console": "^6.0" + "jetbrains/phpstorm-attributes": "^1.1", + "phpunit/phpunit": "^10.0|^11.2", + "symfony/console": "^6.0|^7.0" }, "authors": [ { diff --git a/src/Config.php b/src/Config.php index 639ad6f..4d23d6d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -17,6 +17,8 @@ use JetBrains\PhpStorm\ArrayShape; use PhpCsFixer\Config as PhpCsFixerConfig; use PhpCsFixer\Finder; +use PhpCsFixer\Runner\Parallel\ParallelConfig; +use PhpCsFixer\Runner\Parallel\ParallelConfigFactory; use PhpCsFixerCustomFixers\Fixers as PhpCsFixerCustomFixers; class Config @@ -27,6 +29,9 @@ class Config protected Rules $rules; protected string $rootPath; protected bool $withRiskyFixers = true; + protected bool $withCache = true; + protected bool $withParallelRun = true; + protected ?ParallelConfig $customParallelRunConfig = null; protected bool $ignoreMarkedFiles = false; public function __construct( @@ -57,12 +62,18 @@ public function config(): PhpCsFixerConfig $finder = Finder::create()->directories()->append($filteredFiles); $config = new PhpCsFixerConfig("Blumilk codestyle standard"); - return $config->setFinder($finder) - ->setUsingCache(false) + $config = $config->setFinder($finder) + ->setUsingCache($this->withCache) ->registerCustomFixers(new PhpCsFixerCustomFixers()) ->registerCustomFixers($this->getCustomFixers()) ->setRiskyAllowed($this->withRiskyFixers) ->setRules($rules); + + if ($this->withParallelRun) { + $config = $config->setParallelConfig($this->withCustomParallelRunConfig ?? ParallelConfigFactory::detect()); + } + + return $config; } #[ArrayShape(["paths" => "array", "rules" => "array"])] @@ -81,6 +92,13 @@ public function purgeMode(bool $purgeDocComments = true): static return $this; } + public function withRiskyFixers(): static + { + $this->withRiskyFixers = true; + + return $this; + } + public function withoutRiskyFixers(): static { $this->withRiskyFixers = false; @@ -88,6 +106,42 @@ public function withoutRiskyFixers(): static return $this; } + public function withCache(): static + { + $this->withCache = true; + + return $this; + } + + public function withoutCache(): static + { + $this->withCache = false; + + return $this; + } + + public function withParallelRun(): static + { + $this->withParallelRun = true; + + return $this; + } + + public function withoutParallelRun(): static + { + $this->withParallelRun = false; + + return $this; + } + + public function withCustomParallelRunConfig(ParallelConfig $config): static + { + $this->withParallelRun(); + $this->customParallelRunConfig = $config; + + return $this; + } + public function ignoreMarkedFiles(): static { $this->ignoreMarkedFiles = true; diff --git a/src/Configuration/Defaults/CommonRules.php b/src/Configuration/Defaults/CommonRules.php index cf8b77d..2ab43d0 100644 --- a/src/Configuration/Defaults/CommonRules.php +++ b/src/Configuration/Defaults/CommonRules.php @@ -17,6 +17,7 @@ use PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer; use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer; use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer; +use PhpCsFixer\Fixer\Casing\ConstantCaseFixer; use PhpCsFixer\Fixer\Casing\LowercaseKeywordsFixer; use PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer; use PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer; @@ -88,6 +89,7 @@ use PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer; use PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer; use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer; +use PhpCsFixer\Fixer\PhpUnit\PhpUnitAttributesFixer; use PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer; use PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer; use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer; @@ -342,5 +344,7 @@ class CommonRules extends Rules ClassKeywordFixer::class => true, NamedArgumentFixer::class => true, NoBlankLinesAfterPhpdocFixer::class => true, + ConstantCaseFixer::class => true, + PhpUnitAttributesFixer::class => true, ]; } diff --git a/tests/codestyle/CodestyleTestCase.php b/tests/codestyle/CodestyleTestCase.php index bc0c93b..0256b5d 100644 --- a/tests/codestyle/CodestyleTestCase.php +++ b/tests/codestyle/CodestyleTestCase.php @@ -23,7 +23,6 @@ protected function tearDown(): void } /** - * @dataProvider providePhp80Fixtures * @throws Exception */ protected function testFixture(string $name): void diff --git a/tests/codestyle/CommonRulesetTest.php b/tests/codestyle/CommonRulesetTest.php index 821b098..f0380c4 100644 --- a/tests/codestyle/CommonRulesetTest.php +++ b/tests/codestyle/CommonRulesetTest.php @@ -5,34 +5,36 @@ namespace Blumilk\Codestyle\Tests; use Exception; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\RequiresPhp; class CommonRulesetTest extends CodestyleTestCase { /** - * @dataProvider providePhp80Fixtures - * @requires PHP >= 8.0 * @throws Exception */ + #[DataProvider("providePhp80Fixtures")] + #[RequiresPhp(">= 8.0")] public function testPhp80Fixtures(string $name): void { $this->testFixture($name); } /** - * @dataProvider providePhp81Fixtures - * @requires PHP >= 8.1 * @throws Exception */ + #[DataProvider("providePhp81Fixtures")] + #[RequiresPhp(">= 8.1")] public function testPhp81Fixtures(string $name): void { $this->testFixture($name); } /** - * @dataProvider providePhp82Fixtures - * @requires PHP >= 8.2 * @throws Exception */ + #[DataProvider("providePhp82Fixtures")] + #[RequiresPhp(">= 8.2")] public function testPhp82Fixtures(string $name): void { $this->testFixture($name); diff --git a/tests/codestyle/IgnoreMarkedFilesTest.php b/tests/codestyle/IgnoreMarkedFilesTest.php index f8cc662..5de50dc 100644 --- a/tests/codestyle/IgnoreMarkedFilesTest.php +++ b/tests/codestyle/IgnoreMarkedFilesTest.php @@ -22,7 +22,6 @@ protected function getConfigPath(): string } /** - * @dataProvider providePhp80Fixtures * @throws Exception */ protected function testFixture(string $name): void diff --git a/tests/fixtures/lowercaseKeywords/actual.php b/tests/fixtures/lowercaseKeywords/actual.php index 98b445e..cc1d0f9 100644 --- a/tests/fixtures/lowercaseKeywords/actual.php +++ b/tests/fixtures/lowercaseKeywords/actual.php @@ -4,6 +4,8 @@ Class LowercaseKeywordsTest Extends ObjectOperatorTest { + public bool $true = TRUE; + PUBLIC function test(): int { Foreach (range(1, 5) as $item) { diff --git a/tests/fixtures/lowercaseKeywords/expected.php b/tests/fixtures/lowercaseKeywords/expected.php index e74fa06..804a16f 100644 --- a/tests/fixtures/lowercaseKeywords/expected.php +++ b/tests/fixtures/lowercaseKeywords/expected.php @@ -4,6 +4,8 @@ class LowercaseKeywordsTest extends ObjectOperatorTest { + public bool $true = true; + public function test(): int { foreach (range(1, 5) as $item) {