Skip to content

Commit

Permalink
#94 - return statement missing blank line fixer (#96)
Browse files Browse the repository at this point in the history
* #94 - added blank line fixer

* #94 - fixes

* #94 - add more cases for fixer

* #94 - added one more case for test
  • Loading branch information
kamilpiech97 authored Jun 20, 2023
1 parent f858620 commit 0018785
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -76,6 +77,7 @@ protected function getAllFiles(array &$paths, string $path): void
if (str_ends_with($path, ".php")) {
$paths[] = $path;
}

return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
]],
];
}
1 change: 1 addition & 0 deletions src/Configuration/Defaults/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Fixers/DoubleQuoteFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void

$content = $token->getContent();
$prefix = "";

if (
$content[0] === "'" &&
!str_contains($content, '"') &&
Expand Down
1 change: 1 addition & 0 deletions src/Fixers/NoCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function getName(): string
public function getPriority(): int
{
$fixer = new NoExtraBlankLinesFixer();

return $fixer->getPriority() + 1;
}

Expand Down
1 change: 1 addition & 0 deletions src/Fixers/NoLaravelMigrationsGeneratedCommentFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function getName(): string
public function getPriority(): int
{
$fixer = new VoidReturnFixer();

return $fixer->getPriority() + 1;
}

Expand Down
1 change: 1 addition & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static function providePhp80Fixtures(): array
["namespaces"],
["emptyLines"],
["importsOrder"],
["blankLineBeforeStatement"],
];
}

Expand Down
33 changes: 33 additions & 0 deletions tests/codestyle/fixtures/blankLineBeforeStatement/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);

class BlankLine
{
/**
* @throws Exception
*/
public function getFoo(int $foo): int
{
$bar = $foo;
try {

while ($bar > 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;
}
}
}
37 changes: 37 additions & 0 deletions tests/codestyle/fixtures/blankLineBeforeStatement/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

class BlankLine
{
/**
* @throws Exception
*/
public function getFoo(int $foo): int
{
$bar = $foo;

try {
while ($bar > 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class NullableTypeForDefaultNull
public function getNameLabel(string $name, ?string $title = null): string
{
$label = $name;

if ($title !== null) {
$label .= " " . $title;
}
Expand Down
1 change: 1 addition & 0 deletions tests/codestyle/fixtures/objectOperators/expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ObjectOperatorTest
public function get(): void
{
$array = $this->array;

if ($this->nullable) {
unset($array);
}
Expand Down

0 comments on commit 0018785

Please sign in to comment.