Skip to content

Commit

Permalink
#48 - @kubawerlos custom fixers added (#47)
Browse files Browse the repository at this point in the history
* - readonlies fix

* #48 - custom fixers added
  • Loading branch information
krzysztofrewak committed Feb 20, 2022
1 parent c1e06d9 commit e9d1c81
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

jobs:
build:
name: "Checking the package: testing and linting"
runs-on: ubuntu-20.04

strategy:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"type": "library",
"require": {
"php": "^8.0",
"symplify/easy-coding-standard": "10.0.8"
"kubawerlos/php-cs-fixer-custom-fixers": "^3.7",
"symplify/easy-coding-standard": "10.0.21"
},
"require-dev": {
"composer/composer": "2.*",
Expand Down
18 changes: 18 additions & 0 deletions src/Configuration/Defaults/CommonAdditionalRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
use PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer;
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixerCustomFixers\Fixer\ConstructorEmptyBracesFixer;
use PhpCsFixerCustomFixers\Fixer\InternalClassCasingFixer;
use PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer;
use PhpCsFixerCustomFixers\Fixer\NoUselessCommentFixer;
use PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer;
use PhpCsFixerCustomFixers\Fixer\PromotedConstructorPropertyFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
use PhpCsFixerCustomFixers\Fixer\StringableInterfaceFixer;

class CommonAdditionalRules extends Rules implements AdditionalRules
{
Expand Down Expand Up @@ -62,5 +71,14 @@ class CommonAdditionalRules extends Rules implements AdditionalRules
],
],
NullableTypeDeclarationForDefaultNullValueFixer::class => null,
ConstructorEmptyBracesFixer::class => null,
InternalClassCasingFixer::class => null,
MultilinePromotedPropertiesFixer::class => null,
NoUselessCommentFixer::class => null,
PhpdocArrayStyleFixer::class => null,
PromotedConstructorPropertyFixer::class => null,
SingleSpaceAfterStatementFixer::class => null,
SingleSpaceBeforeStatementFixer::class => null,
StringableInterfaceFixer::class => null,
];
}
6 changes: 3 additions & 3 deletions src/Configuration/Defaults/CommonSetLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function get(): array
return $this->setLists;
}

public function add(string ...$setLists): self
public function add(string ...$setLists): static
{
foreach ($setLists as $setList) {
if (!in_array($setList, $this->setLists, true)) {
Expand All @@ -31,14 +31,14 @@ public function add(string ...$setLists): self
return $this;
}

public function clear(): self
public function clear(): static
{
$this->setLists = [];

return $this;
}

public function filter(string ...$setLists): self
public function filter(string ...$setLists): static
{
foreach ($this->setLists as $index => $setList) {
if (in_array($setList, $setLists, true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Configuration/Defaults/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function get(): array
return $this->paths;
}

public function add(string ...$paths): self
public function add(string ...$paths): static
{
foreach ($paths as $path) {
if (!in_array($path, $this->paths, true)) {
Expand All @@ -31,14 +31,14 @@ public function add(string ...$paths): self
return $this;
}

public function clear(): self
public function clear(): static
{
$this->paths = [];

return $this;
}

public function filter(string ...$paths): self
public function filter(string ...$paths): static
{
foreach ($this->paths as $index => $path) {
if (in_array($path, $paths, true)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Configuration/Defaults/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function get(): array
return $this->rules;
}

public function add(Rule ...$rules): self
public function add(Rule ...$rules): static
{
foreach ($rules as $rule) {
if (!in_array($rule->getFixerClassName(), array_keys($this->rules), true)) {
Expand All @@ -26,14 +26,14 @@ public function add(Rule ...$rules): self
return $this;
}

public function clear(): self
public function clear(): static
{
$this->rules = [];

return $this;
}

public function filter(string ...$rules): self
public function filter(string ...$rules): static
{
foreach (array_keys($this->rules) as $rule) {
if (in_array($rule, $rules, true)) {
Expand Down
12 changes: 4 additions & 8 deletions src/Configuration/Utils/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

class Rule
{
protected string $fixer;
protected ?array $options;

public function __construct(string $fixer, ?array $options = null)
{
$this->fixer = $fixer;
$this->options = $options;
}
public function __construct(
protected string $fixer,
protected ?array $options = null,
) {}

public function getFixerClassName(): string
{
Expand Down
8 changes: 7 additions & 1 deletion tests/codestyle/fixtures/readonlies/actual.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php

declare(strict_types=1);

class ReadonlyThing
{
protected readonly bool $true;

public function __construct(protected readonly string $string)
public function __construct(
protected readonly string $string,
protected readonly ?bool $nullable,
protected readonly ?bool $initializedNullable = null,
)
{
}
}
5 changes: 3 additions & 2 deletions tests/codestyle/fixtures/readonlies/expected.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ReadonlyThing

public function __construct(
protected readonly string $string,
) {
}
protected readonly ?bool $nullable,
protected readonly ?bool $initializedNullable = null,
) {}
}
45 changes: 45 additions & 0 deletions tests/unit/AdditionalRulesConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer;
use PhpCsFixer\Fixer\StringNotation\HeredocToNowdocFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixerCustomFixers\Fixer\ConstructorEmptyBracesFixer;
use PhpCsFixerCustomFixers\Fixer\InternalClassCasingFixer;
use PhpCsFixerCustomFixers\Fixer\MultilinePromotedPropertiesFixer;
use PhpCsFixerCustomFixers\Fixer\NoUselessCommentFixer;
use PhpCsFixerCustomFixers\Fixer\PhpdocArrayStyleFixer;
use PhpCsFixerCustomFixers\Fixer\PromotedConstructorPropertyFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceAfterStatementFixer;
use PhpCsFixerCustomFixers\Fixer\SingleSpaceBeforeStatementFixer;
use PhpCsFixerCustomFixers\Fixer\StringableInterfaceFixer;
use PHPUnit\Framework\TestCase;

class AdditionalRulesConfigurationTest extends TestCase
Expand Down Expand Up @@ -71,6 +80,15 @@ public function testAdditionalRulesConfiguration(): void
],
],
NullableTypeDeclarationForDefaultNullValueFixer::class => null,
ConstructorEmptyBracesFixer::class => null,
InternalClassCasingFixer::class => null,
MultilinePromotedPropertiesFixer::class => null,
NoUselessCommentFixer::class => null,
PhpdocArrayStyleFixer::class => null,
PromotedConstructorPropertyFixer::class => null,
SingleSpaceAfterStatementFixer::class => null,
SingleSpaceBeforeStatementFixer::class => null,
StringableInterfaceFixer::class => null,
],
$config->options()["rules"],
);
Expand Down Expand Up @@ -126,6 +144,15 @@ public function testFilteringAdditionalRulesConfiguration(): void
],
],
NullableTypeDeclarationForDefaultNullValueFixer::class => null,
ConstructorEmptyBracesFixer::class => null,
InternalClassCasingFixer::class => null,
MultilinePromotedPropertiesFixer::class => null,
NoUselessCommentFixer::class => null,
PhpdocArrayStyleFixer::class => null,
PromotedConstructorPropertyFixer::class => null,
SingleSpaceAfterStatementFixer::class => null,
SingleSpaceBeforeStatementFixer::class => null,
StringableInterfaceFixer::class => null,
],
$config->options()["rules"],
);
Expand Down Expand Up @@ -178,6 +205,15 @@ public function testExtendingAdditionalRulesConfiguration(): void
],
],
NullableTypeDeclarationForDefaultNullValueFixer::class => null,
ConstructorEmptyBracesFixer::class => null,
InternalClassCasingFixer::class => null,
MultilinePromotedPropertiesFixer::class => null,
NoUselessCommentFixer::class => null,
PhpdocArrayStyleFixer::class => null,
PromotedConstructorPropertyFixer::class => null,
SingleSpaceAfterStatementFixer::class => null,
SingleSpaceBeforeStatementFixer::class => null,
StringableInterfaceFixer::class => null,
HeredocToNowdocFixer::class => null,
],
$config->options()["rules"],
Expand Down Expand Up @@ -238,6 +274,15 @@ public function testExtendingWithOptionsAdditionalRulesConfiguration(): void
],
],
NullableTypeDeclarationForDefaultNullValueFixer::class => null,
ConstructorEmptyBracesFixer::class => null,
InternalClassCasingFixer::class => null,
MultilinePromotedPropertiesFixer::class => null,
NoUselessCommentFixer::class => null,
PhpdocArrayStyleFixer::class => null,
PromotedConstructorPropertyFixer::class => null,
SingleSpaceAfterStatementFixer::class => null,
SingleSpaceBeforeStatementFixer::class => null,
StringableInterfaceFixer::class => null,
NoMixedEchoPrintFixer::class => [
"use" => "echo",
],
Expand Down

0 comments on commit e9d1c81

Please sign in to comment.