Skip to content

Commit

Permalink
#98 - single line control structures indentation (#99)
Browse files Browse the repository at this point in the history
* #98 - single line control structures indentation

* #98 - csf
  • Loading branch information
krzysztofrewak authored Aug 21, 2023
1 parent 41639ad commit d69772a
Show file tree
Hide file tree
Showing 62 changed files with 87 additions and 53 deletions.
3 changes: 1 addition & 2 deletions codestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"bin",
"src",
"tests/unit",
"tests/codestyle/CodestyleTest.php",
"tests/codestyle/config.php",
"tests/codestyle",
),
);

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"type": "library",
"require": {
"php": "^8.1",
"friendsofphp/php-cs-fixer": "^3.16",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.14"
"friendsofphp/php-cs-fixer": "^3.23",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.16"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
Expand Down
27 changes: 18 additions & 9 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer;
use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer;
use PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer;
use PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer;
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;
use PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer;
use PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer;
use PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer;
Expand All @@ -29,13 +32,14 @@
use PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer;
use PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer;
use PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer;
use PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer;
use PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer;
use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer;
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
use PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer;
use PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer;
use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer;
use PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer;
Expand All @@ -52,9 +56,9 @@
use PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer;
use PhpCsFixer\Fixer\LanguageConstruct\IsNullFixer;
use PhpCsFixer\Fixer\NamespaceNotation\BlankLineAfterNamespaceFixer;
use PhpCsFixer\Fixer\NamespaceNotation\BlankLinesBeforeNamespaceFixer;
use PhpCsFixer\Fixer\NamespaceNotation\CleanNamespaceFixer;
use PhpCsFixer\Fixer\NamespaceNotation\NoLeadingNamespaceWhitespaceFixer;
use PhpCsFixer\Fixer\NamespaceNotation\SingleBlankLineBeforeNamespaceFixer;
use PhpCsFixer\Fixer\Naming\NoHomoglyphNamesFixer;
use PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer;
use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer;
Expand Down Expand Up @@ -99,10 +103,10 @@
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer;
use PhpCsFixer\Fixer\Whitespace\NoSpacesInsideParenthesisFixer;
use PhpCsFixer\Fixer\Whitespace\NoWhitespaceInBlankLineFixer;
use PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer;
use PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\TypeDeclarationSpacesFixer;
use PhpCsFixerCustomFixers\Fixer\CommentedOutFunctionFixer;
use PhpCsFixerCustomFixers\Fixer\ConstructorEmptyBracesFixer;
use PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer;
Expand All @@ -113,7 +117,6 @@
use PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer;
use PhpCsFixerCustomFixers\Fixer\PhpdocNoIncorrectVarAnnotationFixer;
use PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer;
use PhpCsFixerCustomFixers\Fixer\PhpdocParamOrderFixer;
use PhpCsFixerCustomFixers\Fixer\PromotedConstructorPropertyFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
Expand Down Expand Up @@ -157,7 +160,6 @@ class CommonRules extends Rules
"remove_inheritdoc" => true,
"allow_mixed" => true,
],
SingleBlankLineBeforeNamespaceFixer::class => true,
PhpUnitTestAnnotationFixer::class => true,
PhpUnitSetUpTearDownVisibilityFixer::class => true,
BlankLineAfterOpeningTagFixer::class => true,
Expand All @@ -173,7 +175,6 @@ class CommonRules extends Rules
],
],
SingleTraitInsertPerStatementFixer::class => true,
FunctionTypehintSpaceFixer::class => true,
NoBlankLinesAfterClassOpeningFixer::class => true,
NoSinglelineWhitespaceBeforeSemicolonsFixer::class => true,
PhpdocSingleLineVarSpacingFixer::class => true,
Expand Down Expand Up @@ -275,8 +276,6 @@ class CommonRules extends Rules
NoPhpStormGeneratedCommentFixer::class => true,
PhpdocNoIncorrectVarAnnotationFixer::class => true,
PhpdocNoSuperfluousParamFixer::class => true,
PhpdocParamOrderFixer::class => true,
NoSpacesInsideParenthesisFixer::class => true,
YodaStyleFixer::class => [
"equal" => false,
"identical" => false,
Expand Down Expand Up @@ -318,6 +317,16 @@ class CommonRules extends Rules
"for",
"foreach",
"while",
]],
],
],
NoMultipleStatementsPerLineFixer::class => true,
BlankLinesBeforeNamespaceFixer::class => true,
PsrAutoloadingFixer::class => true,
TypeDeclarationSpacesFixer::class => true,
ControlStructureBracesFixer::class => true,
ControlStructureContinuationPositionFixer::class => true,
CurlyBracesPositionFixer::class => [
"anonymous_functions_opening_brace" => "same_line",
],
];
}
4 changes: 2 additions & 2 deletions src/Configuration/Defaults/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function get(): array
public function add(string ...$paths): static
{
foreach ($paths as $path) {
if (!in_array($path, $this->paths, true)) {
if (!in_array($path, $this->paths, strict: true)) {
$this->paths[] = $path;
}
}
Expand All @@ -41,7 +41,7 @@ public function clear(): static
public function filter(string ...$paths): static
{
foreach ($this->paths as $index => $path) {
if (in_array($path, $paths, true)) {
if (in_array($path, $paths, strict: true)) {
unset($this->paths[$index]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Configuration/Defaults/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function get(): array
public function add(Rule ...$rules): static
{
foreach ($rules as $rule) {
if (!in_array($rule->getFixerClassName(), array_keys($this->rules), true)) {
if (!in_array($rule->getFixerClassName(), array_keys($this->rules), strict: true)) {
$this->rules[$rule->getFixerClassName()] = $rule->getOptions();
}
}
Expand All @@ -46,7 +46,7 @@ public function clear(): static
public function filter(string ...$rules): static
{
foreach (array_keys($this->rules) as $rule) {
if (in_array($rule, $rules, true)) {
if (in_array($rule, $rules, strict: true)) {
unset($this->rules[$rule]);
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/codestyle/CodestyleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function tearDown(): void
*/
protected function testFixture(string $name): void
{
copy(__DIR__ . "/fixtures/{$name}/actual.php", __DIR__ . "/tmp/{$name}.php");
copy(__DIR__ . "/../fixtures/{$name}/actual.php", __DIR__ . "/tmp/{$name}.php");

$this->assertFalse(
$this->runFixer(),
Expand All @@ -41,7 +41,7 @@ protected function testFixture(string $name): void
);

$this->assertFileEquals(
__DIR__ . "/fixtures/{$name}/expected.php",
__DIR__ . "/../fixtures/{$name}/expected.php",
__DIR__ . "/tmp/{$name}.php",
"Result of proceeded fixture fixtures/{$name} is not equal to expected.",
);
Expand All @@ -67,6 +67,7 @@ protected function runFixer(bool $fix = false): bool
protected function clearTempDirectory(): void
{
$files = glob(__DIR__ . "/tmp/*.php");

foreach ($files as $file) {
unlink($file);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Tests;

use Exception;

class CommonRulesetTest extends CodestyleTestCase
{
/**
Expand Down Expand Up @@ -57,6 +60,8 @@ public static function providePhp80Fixtures(): array
["emptyLines"],
["importsOrder"],
["blankLineBeforeStatement"],
["braces"],
["stringVariables"],
];
}

Expand Down
3 changes: 3 additions & 0 deletions tests/codestyle/PurgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
declare(strict_types=1);

namespace Blumilk\Codestyle\Tests;

use Exception;

class PurgeTest extends CodestyleTestCase
{
/**
Expand Down
8 changes: 7 additions & 1 deletion tests/codestyle/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\Paths;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;

$paths = new Paths("tests/codestyle/tmp");
$rules = new CommonRules();

$config = new Config(
paths: new Paths("tests/codestyle/tmp"),
paths: $paths,
rules: $rules->filter(PsrAutoloadingFixer::class),
);

return $config->config();
8 changes: 7 additions & 1 deletion tests/codestyle/config/config.purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\Paths;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;

$paths = new Paths("tests/codestyle/tmp");
$rules = new CommonRules();

$config = new Config(
paths: new Paths("tests/codestyle/tmp"),
paths: $paths,
rules: $rules->filter(PsrAutoloadingFixer::class),
);

return $config->purgeMode()->config();
16 changes: 0 additions & 16 deletions tests/codestyle/fixtures/bracesFixer/actual.php

This file was deleted.

16 changes: 0 additions & 16 deletions tests/codestyle/fixtures/bracesFixer/expected.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/fixtures/braces/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

class Braces
{
public function transform(array $data): void
{
if (trim($data[1]) === "Türkiye") $countryName = "Turkey";
else $countryName = $this->translate(trim($data[1]), "en");

if (trim($data[0]) === "Uşak")
$cityName = "Usak";
else $cityName = $this->translate(trim($data[0]), "en");
}
}
21 changes: 21 additions & 0 deletions tests/fixtures/braces/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

class Braces
{
public function transform(array $data): void
{
if (trim($data[1]) === "Türkiye") {
$countryName = "Turkey";
} else {
$countryName = $this->translate(trim($data[1]), "en");
}

if (trim($data[0]) === "Uşak") {
$cityName = "Usak";
} else {
$cityName = $this->translate(trim($data[0]), "en");
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit d69772a

Please sign in to comment.