Skip to content

Commit

Permalink
- importing FQCNs (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofrewak authored Feb 12, 2024
1 parent 12f2e2f commit 4c5bf75
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
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.23",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.16"
"friendsofphp/php-cs-fixer": "^3.49",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.20"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
Expand Down
10 changes: 9 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Config
protected Paths $paths;
protected Rules $rules;
protected string $rootPath;
protected bool $withRiskyFixers = true;

public function __construct(
?Paths $paths = null,
Expand Down Expand Up @@ -52,7 +53,7 @@ public function config(): PhpCsFixerConfig
->setUsingCache(false)
->registerCustomFixers(new PhpCsFixerCustomFixers())
->registerCustomFixers($this->getCustomFixers())
->setRiskyAllowed(true)
->setRiskyAllowed($this->withRiskyFixers)
->setRules($rules);
}

Expand All @@ -72,6 +73,13 @@ public function purgeMode(): static
return $this;
}

public function withoutRiskyFixers(): static
{
$this->withRiskyFixers = false;

return $this;
}

protected function getAllFiles(array &$paths, string $path): void
{
if (is_file($path) || !is_dir($path)) {
Expand Down
22 changes: 13 additions & 9 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer;
use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer;
use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer;
use PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer;
use PhpCsFixer\Fixer\Basic\BracesPositionFixer;
use PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer;
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer;
Expand All @@ -37,8 +37,8 @@
use PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer;
use PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer;
use PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer;
use PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer;
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;
Expand All @@ -54,6 +54,7 @@
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
use PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer;
use PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer;
use PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer;
use PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer;
use PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer;
Expand All @@ -66,7 +67,7 @@
use PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer;
use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer;
use PhpCsFixer\Fixer\Operator\ConcatSpaceFixer;
use PhpCsFixer\Fixer\Operator\NewWithBracesFixer;
use PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer;
use PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer;
use PhpCsFixer\Fixer\Operator\ObjectOperatorWithoutWhitespaceFixer;
use PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer;
Expand Down Expand Up @@ -101,7 +102,7 @@
use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer;
use PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer;
use PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer;
use PhpCsFixer\Fixer\Whitespace\CompactNullableTypeDeclarationFixer;
use PhpCsFixer\Fixer\Whitespace\LineEndingFixer;
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
Expand Down Expand Up @@ -141,7 +142,7 @@ class CommonRules extends Rules
SingleClassElementPerStatementFixer::class => [
"elements" => ["const", "property"],
],
NewWithBracesFixer::class => true,
NewWithParenthesesFixer::class => true,
ClassDefinitionFixer::class => [
"single_line" => true,
],
Expand Down Expand Up @@ -194,15 +195,17 @@ class CommonRules extends Rules
NoUnusedImportsFixer::class => true,
NoEmptyStatementFixer::class => true,
NoUnneededControlParenthesesFixer::class => true,
NoUnneededCurlyBracesFixer::class => true,
NoUnneededBracesFixer::class => true,
DeclareStrictTypesFixer::class => true,
CastSpacesFixer::class => [
"space" => "none",
],
DoubleQuoteFixer::class => true,
VoidReturnFixer::class => true,
UseArrowFunctionsFixer::class => true,
FullyQualifiedStrictTypesFixer::class => true,
FullyQualifiedStrictTypesFixer::class => [
"import_symbols" => true,
],
OrderedImportsFixer::class => [
"sort_algorithm" => "alpha",
"imports_order" => ["class", "function", "const"],
Expand Down Expand Up @@ -255,7 +258,7 @@ class CommonRules extends Rules
NoLeadingImportSlashFixer::class => true,
LowercaseCastFixer::class => true,
LowercaseStaticReferenceFixer::class => true,
CompactNullableTypehintFixer::class => true,
CompactNullableTypeDeclarationFixer::class => true,
DeclareEqualNormalizeFixer::class => true,
ShortScalarCastFixer::class => true,
CleanNamespaceFixer::class => true,
Expand Down Expand Up @@ -328,11 +331,12 @@ class CommonRules extends Rules
TypeDeclarationSpacesFixer::class => true,
ControlStructureBracesFixer::class => true,
ControlStructureContinuationPositionFixer::class => true,
CurlyBracesPositionFixer::class => [
BracesPositionFixer::class => [
"anonymous_functions_opening_brace" => "same_line",
],
LowercaseKeywordsFixer::class => true,
NoMultilineWhitespaceAroundDoubleArrowFixer::class => true,
CompactEmptyArrayFixer::class => true,
ClassKeywordFixer::class => true,
];
}
1 change: 1 addition & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static function providePhp80Fixtures(): array
["lowercaseKeywords"],
["noMultilineWhitespaceAroundDoubleArrow"],
["compactArray"],
["classesImport"],
];
}

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

class TestException extends \Exception
{
protected string $rules = "\Blumilk\Codestyle\Configuration\Defaults\LaravelPaths";

public function rules(\Blumilk\Codestyle\Configuration\Defaults\CommonRules $rules): void
{
echo 123;
}
}
16 changes: 16 additions & 0 deletions tests/fixtures/classesImport/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Configuration\Defaults\CommonRules;
use Blumilk\Codestyle\Configuration\Defaults\LaravelPaths;

class TestException extends Exception
{
protected string $rules = LaravelPaths::class;

public function rules(CommonRules $rules): void
{
echo 123;
}
}

0 comments on commit 4c5bf75

Please sign in to comment.