diff --git a/src/Config.php b/src/Config.php index ba91ea8..f4aa888 100644 --- a/src/Config.php +++ b/src/Config.php @@ -38,6 +38,7 @@ public function config(): PhpCsFixerConfig list("paths" => $paths, "rules" => $rules) = $this->options(); $files = []; + foreach ($paths as $path) { $directory = $this->rootPath . "/" . $path; $this->getAllFiles($files, $directory); @@ -76,6 +77,7 @@ protected function getAllFiles(array &$paths, string $path): void if (str_ends_with($path, ".php")) { $paths[] = $path; } + return; } diff --git a/src/Configuration/Defaults/CommonRules.php b/src/Configuration/Defaults/CommonRules.php index e3994df..f151c21 100644 --- a/src/Configuration/Defaults/CommonRules.php +++ b/src/Configuration/Defaults/CommonRules.php @@ -92,6 +92,7 @@ use PhpCsFixer\Fixer\Strict\StrictParamFixer; use PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer; use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer; +use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer; use PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer; use PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer; use PhpCsFixer\Fixer\Whitespace\LineEndingFixer; @@ -304,5 +305,19 @@ class CommonRules extends Rules LineEndingFixer::class => true, StatementIndentationFixer::class => true, BlankLineBetweenImportGroupsFixer::class => true, + BlankLineBeforeStatementFixer::class => [ + "statements" => [ + "break", + "continue", + "declare", + "return", + "throw", + "try", + "if", + "do", + "for", + "foreach", + "while", + ]], ]; } diff --git a/src/Configuration/Defaults/Rules.php b/src/Configuration/Defaults/Rules.php index be34feb..fed62ec 100644 --- a/src/Configuration/Defaults/Rules.php +++ b/src/Configuration/Defaults/Rules.php @@ -15,6 +15,7 @@ abstract class Rules implements RulesContract public function get(): array { $rules = []; + foreach ($this->rules as $fixer => $options) { /** @var AbstractFixer $fixer */ $fixer = new $fixer(); diff --git a/src/Fixers/DoubleQuoteFixer.php b/src/Fixers/DoubleQuoteFixer.php index 6d36696..c423147 100644 --- a/src/Fixers/DoubleQuoteFixer.php +++ b/src/Fixers/DoubleQuoteFixer.php @@ -69,6 +69,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void $content = $token->getContent(); $prefix = ""; + if ( $content[0] === "'" && !str_contains($content, '"') && diff --git a/src/Fixers/NoCommentFixer.php b/src/Fixers/NoCommentFixer.php index f0a6362..ceb6759 100644 --- a/src/Fixers/NoCommentFixer.php +++ b/src/Fixers/NoCommentFixer.php @@ -49,6 +49,7 @@ public function getName(): string public function getPriority(): int { $fixer = new NoExtraBlankLinesFixer(); + return $fixer->getPriority() + 1; } diff --git a/src/Fixers/NoLaravelMigrationsGeneratedCommentFixer.php b/src/Fixers/NoLaravelMigrationsGeneratedCommentFixer.php index 8966062..fd42f9e 100644 --- a/src/Fixers/NoLaravelMigrationsGeneratedCommentFixer.php +++ b/src/Fixers/NoLaravelMigrationsGeneratedCommentFixer.php @@ -67,6 +67,7 @@ public function getName(): string public function getPriority(): int { $fixer = new VoidReturnFixer(); + return $fixer->getPriority() + 1; } diff --git a/tests/codestyle/CommonRulesetTest.php b/tests/codestyle/CommonRulesetTest.php index 57defe5..79ba30b 100644 --- a/tests/codestyle/CommonRulesetTest.php +++ b/tests/codestyle/CommonRulesetTest.php @@ -56,6 +56,7 @@ public static function providePhp80Fixtures(): array ["namespaces"], ["emptyLines"], ["importsOrder"], + ["blankLineBeforeStatement"], ]; } diff --git a/tests/codestyle/fixtures/blankLineBeforeStatement/actual.php b/tests/codestyle/fixtures/blankLineBeforeStatement/actual.php new file mode 100644 index 0000000..dfa5b1f --- /dev/null +++ b/tests/codestyle/fixtures/blankLineBeforeStatement/actual.php @@ -0,0 +1,33 @@ + 0) { + $bar++; + } + for ($i = 0; $i < 5; $i++) { + echo $i; + } + } catch (Exception $exception) { + $value = "error"; + throw new Exception(message: $value); + } + return $bar; + } + + protected function getBar(int $number): void + { + if ($number === 1) { + return; + } + } +} diff --git a/tests/codestyle/fixtures/blankLineBeforeStatement/expected.php b/tests/codestyle/fixtures/blankLineBeforeStatement/expected.php new file mode 100644 index 0000000..244fbe5 --- /dev/null +++ b/tests/codestyle/fixtures/blankLineBeforeStatement/expected.php @@ -0,0 +1,37 @@ + 0) { + $bar++; + } + + for ($i = 0; $i < 5; $i++) { + echo $i; + } + } catch (Exception $exception) { + $value = "error"; + + throw new Exception(message: $value); + } + + return $bar; + } + + protected function getBar(int $number): void + { + if ($number === 1) { + return; + } + } +} diff --git a/tests/codestyle/fixtures/nullableTypeForDefaultNull/expected.php b/tests/codestyle/fixtures/nullableTypeForDefaultNull/expected.php index d87bf9d..bc08836 100644 --- a/tests/codestyle/fixtures/nullableTypeForDefaultNull/expected.php +++ b/tests/codestyle/fixtures/nullableTypeForDefaultNull/expected.php @@ -7,6 +7,7 @@ class NullableTypeForDefaultNull public function getNameLabel(string $name, ?string $title = null): string { $label = $name; + if ($title !== null) { $label .= " " . $title; } diff --git a/tests/codestyle/fixtures/objectOperators/expected.php b/tests/codestyle/fixtures/objectOperators/expected.php index 5d42890..5b4e8c6 100644 --- a/tests/codestyle/fixtures/objectOperators/expected.php +++ b/tests/codestyle/fixtures/objectOperators/expected.php @@ -10,6 +10,7 @@ class ObjectOperatorTest public function get(): void { $array = $this->array; + if ($this->nullable) { unset($array); }