Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- common ruleset update #90

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.0",
"symfony/console": "^6.0"
},
"authors": [
Expand Down
12 changes: 2 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,8 @@ or following to fix found errors:
composer csf
```

#### Upgrading guide from 0.x
With version 1.x we removed `symplify/easy-coding-standard` dependency in the project. The checklist for updating old projects is as follows:

- [ ] update the main dependency `blumilksoftware/codestyle` to version `^1.0` in `composer.json` file
- [ ] run `composer update blumilksoftware/codestyle -W`
- [ ] rename `ecs.php` to `codestyle.php`
- [ ] update scripts in `composer.json` file
- [ ] update scripts in Github Actions
- [ ] the constructor of `Blumilk\Codestyle\Config` lost two parameters: `$sets` and `$skipped`; all manipulations of rules should be done on base `$rules` list
- [ ] `Blumilk\Codestyle\Configuration\Defaults\LaravelPaths` returns a default Laravel 9 directory schema; for Laravel 8 additional parameter `LaravelPaths::LARAVEL_8_PATHS` should be added
#### Upgrading guide
Upgrading guide is available in [upgrading.md](./upgrading.md) file.

### Contributing
In cloned or forked repository, run:
Expand Down
68 changes: 59 additions & 9 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Blumilk\Codestyle\Fixers\DoubleQuoteFixer;
use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer;
use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use PhpCsFixer\Fixer\ArrayNotation\NoTrailingCommaInSinglelineArrayFixer;
use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer;
use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
use PhpCsFixer\Fixer\Casing\LowercaseStaticReferenceFixer;
use PhpCsFixer\Fixer\Casing\MagicConstantCasingFixer;
use PhpCsFixer\Fixer\Casing\MagicMethodCasingFixer;
use PhpCsFixer\Fixer\Casing\NativeFunctionCasingFixer;
use PhpCsFixer\Fixer\CastNotation\CastSpacesFixer;
use PhpCsFixer\Fixer\CastNotation\LowercaseCastFixer;
use PhpCsFixer\Fixer\CastNotation\ShortScalarCastFixer;
Expand All @@ -25,6 +27,7 @@
use PhpCsFixer\Fixer\ClassNotation\SingleClassElementPerStatementFixer;
use PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer;
use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer;
use PhpCsFixer\Fixer\Comment\NoEmptyCommentFixer;
use PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer;
use PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer;
Expand All @@ -34,6 +37,7 @@
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;
use PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer;
Expand All @@ -52,9 +56,12 @@
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;
use PhpCsFixer\Fixer\Operator\ConcatSpaceFixer;
use PhpCsFixer\Fixer\Operator\NewWithBracesFixer;
use PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer;
use PhpCsFixer\Fixer\Operator\ObjectOperatorWithoutWhitespaceFixer;
use PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer;
use PhpCsFixer\Fixer\Operator\TernaryOperatorSpacesFixer;
Expand All @@ -75,6 +82,9 @@
use PhpCsFixer\Fixer\PhpUnit\PhpUnitMethodCasingFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitSetUpTearDownVisibilityFixer;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer;
use PhpCsFixer\Fixer\ReturnNotation\NoUselessReturnFixer;
use PhpCsFixer\Fixer\ReturnNotation\SimplifiedNullReturnFixer;
use PhpCsFixer\Fixer\Semicolon\MultilineWhitespaceBeforeSemicolonsFixer;
use PhpCsFixer\Fixer\Semicolon\NoEmptyStatementFixer;
use PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer;
use PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer;
Expand All @@ -84,12 +94,14 @@
use PhpCsFixer\Fixer\StringNotation\SimpleToComplexStringVariableFixer;
use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer;
use PhpCsFixer\Fixer\Whitespace\LineEndingFixer;
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 PhpCsFixerCustomFixers\Fixer\CommentedOutFunctionFixer;
use PhpCsFixerCustomFixers\Fixer\ConstructorEmptyBracesFixer;
use PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer;
Expand All @@ -113,14 +125,19 @@ class CommonRules extends Rules
ArrayIndentationFixer::class => true,
TrimArraySpacesFixer::class => true,
WhitespaceAfterCommaInArrayFixer::class => true,
NoTrailingCommaInSinglelineArrayFixer::class => true,
ArraySyntaxFixer::class => ["syntax" => "short"],
ArraySyntaxFixer::class => [
"syntax" => "short",
],
PhpUnitMethodCasingFixer::class => true,
FunctionToConstantFixer::class => true,
ExplicitIndirectVariableFixer::class => true,
SingleClassElementPerStatementFixer::class => ["elements" => ["const", "property"]],
SingleClassElementPerStatementFixer::class => [
"elements" => ["const", "property"],
],
NewWithBracesFixer::class => true,
ClassDefinitionFixer::class => ["single_line" => true],
ClassDefinitionFixer::class => [
"single_line" => true,
],
StandardizeIncrementFixer::class => true,
SelfAccessorFixer::class => true,
MagicConstantCasingFixer::class => true,
Expand All @@ -135,14 +152,25 @@ class CommonRules extends Rules
PhpdocTypesFixer::class => true,
PhpdocReturnSelfReferenceFixer::class => true,
PhpdocVarWithoutNameFixer::class => true,
NoSuperfluousPhpdocTagsFixer::class => ["remove_inheritdoc" => true, "allow_mixed" => true],
NoSuperfluousPhpdocTagsFixer::class => [
"remove_inheritdoc" => true,
"allow_mixed" => true,
],
SingleBlankLineBeforeNamespaceFixer::class => true,
PhpUnitTestAnnotationFixer::class => true,
PhpUnitSetUpTearDownVisibilityFixer::class => true,
BlankLineAfterOpeningTagFixer::class => true,
MethodChainingIndentationFixer::class => true,
ConcatSpaceFixer::class => ["spacing" => "one"],
BinaryOperatorSpacesFixer::class => ["operators" => ["=>" => "single_space", "=" => "single_space", "&" => "no_space"]],
ConcatSpaceFixer::class => [
"spacing" => "one",
],
BinaryOperatorSpacesFixer::class => [
"operators" => [
"=>" => "single_space",
"=" => "single_space",
"&" => "no_space",
],
],
SingleTraitInsertPerStatementFixer::class => true,
FunctionTypehintSpaceFixer::class => true,
NoBlankLinesAfterClassOpeningFixer::class => true,
Expand Down Expand Up @@ -183,10 +211,19 @@ class CommonRules extends Rules
],
NoExtraBlankLinesFixer::class => [
"tokens" => [
"extra",
"attribute",
"break",
"case",
"continue",
"curly_brace_block",
"default",
"extra",
"parenthesis_brace_block",
"return",
"square_brace_block",
"switch",
"throw",
"use",
],
],
TrailingCommaInMultilineFixer::class => [
Expand Down Expand Up @@ -251,5 +288,18 @@ class CommonRules extends Rules
SingleLineCommentSpacingFixer::class => true,
BlankLineAfterNamespaceFixer::class => true,
SimpleToComplexStringVariableFixer::class => true,
NoEmptyCommentFixer::class => true,
NoTrailingCommaInSinglelineFixer::class => true,
MagicMethodCasingFixer::class => true,
NativeFunctionCasingFixer::class => true,
LambdaNotUsedImportFixer::class => true,
NoHomoglyphNamesFixer::class => true,
AssignNullCoalescingToCoalesceEqualFixer::class => true,
NoUselessNullsafeOperatorFixer::class => true,
NoUselessReturnFixer::class => true,
SimplifiedNullReturnFixer::class => true,
MultilineWhitespaceBeforeSemicolonsFixer::class => true,
LineEndingFixer::class => true,
StatementIndentationFixer::class => true,
];
}
110 changes: 59 additions & 51 deletions tests/codestyle/CodestyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,83 +20,75 @@ protected function tearDown(): void
}

/**
* @dataProvider providePhp80Fixtures
* @requires PHP >= 8.0
* @throws Exception
*/
public function testPhp80Fixtures(): void
public function testPhp80Fixtures(string $name): void
{
$fixtures = [
"noExtraBlankLines",
"nullableTypeForDefaultNull",
"operatorSpacing",
"singleQuotes",
"strictTypes",
"trailingCommas",
"unionTypes",
"references",
"classAttributesSeparation",
"uselessParenthesis",
"laravelMigrations",
"phpdocs",
"yodaStyle",
"objectOperators",
"anonymousFunctions",
"namespaces",
];

foreach ($fixtures as $fixture) {
$this->testFixture($fixture);
}
$this->testFixture($name);
}

/**
* @dataProvider providePhp81Fixtures
* @requires PHP >= 8.1
* @throws Exception
*/
public function testPhp81Fixtures(): void
public function testPhp81Fixtures(string $name): void
{
$fixtures = [
"enums",
"readonlies",
];

foreach ($fixtures as $fixture) {
$this->testFixture($fixture);
}
$this->testFixture($name);
}

/**
* @dataProvider providePhp82Fixtures
* @requires PHP >= 8.2
* @throws Exception
*/
public function testPhp82Fixtures(): void
public function testPhp82Fixtures(string $name): void
{
$fixtures = [
"php82",
];

foreach ($fixtures as $fixture) {
$this->testFixture($fixture);
}
$this->testFixture($name);
}

/**
* @throws Exception
*/
protected function runFixer(bool $fix = false): bool
public static function providePhp80Fixtures(): array
{
$dryRun = $fix ? "" : "--dry-run";

$application = new Application();
$application->setAutoExit(false);
return [
["noExtraBlankLines"],
["nullableTypeForDefaultNull"],
["operatorSpacing"],
["singleQuotes"],
["strictTypes"],
["trailingCommas"],
["unionTypes"],
["references"],
["classAttributesSeparation"],
["uselessParenthesis"],
["laravelMigrations"],
["phpdocs"],
["yodaStyle"],
["objectOperators"],
["anonymousFunctions"],
["namespaces"],
["emptyLines"],
];
}

$output = new BufferedOutput();
$result = $application->run(new StringInput("fix {$dryRun} --diff --config ./tests/codestyle/config.php"), $output);
public static function providePhp81Fixtures(): array
{
return [
["enums"],
["readonlies"],
];
}

return $result === 0;
public static function providePhp82Fixtures(): array
{
return [
["php82"],
];
}

/**
* @dataProvider providePhp80Fixtures
* @throws Exception
*/
protected function testFixture(string $name): void
Expand All @@ -120,6 +112,22 @@ protected function testFixture(string $name): void
);
}

/**
* @throws Exception
*/
protected function runFixer(bool $fix = false): bool
{
$dryRun = $fix ? "" : "--dry-run";

$application = new Application();
$application->setAutoExit(false);

$output = new BufferedOutput();
$result = $application->run(new StringInput("fix {$dryRun} --diff --config ./tests/codestyle/config.php"), $output);

return $result === 0;
}

protected function clearTempDirectory(): void
{
$files = glob(__DIR__ . "/tmp/*.php");
Expand Down
18 changes: 18 additions & 0 deletions tests/codestyle/fixtures/emptyLines/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

return [
"default" => env("DB_CONNECTION", "mysql"),


"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],
]
];
17 changes: 17 additions & 0 deletions tests/codestyle/fixtures/emptyLines/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

return [
"default" => env("DB_CONNECTION", "mysql"),

"connections" => [
"sqlite" => [
"driver" => "sqlite",
"url" => env("DATABASE_URL"),
"database" => env("DB_DATABASE", database_path("database.sqlite")),
"prefix" => "",
"foreign_key_constraints" => env("DB_FOREIGN_KEYS", true),
],
],
];
2 changes: 1 addition & 1 deletion tests/codestyle/fixtures/objectOperators/expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ObjectOperatorTest
public function get(): void
{
$array = $this->array;
if ($this?->nullable) {
if ($this->nullable) {
unset($array);
}
}
Expand Down
Loading