Skip to content

Commit

Permalink
fix: implement new configurable fixer api (#213)
Browse files Browse the repository at this point in the history
* refactor: apply new coding standards

* fix compatibility with 3.59.3

* fix compatibility with 3.59.3 (#210)

Co-authored-by: Joris Mak <[email protected]>

* refactor: apply new coding standards

---------

Co-authored-by: Joris Mak <[email protected]>
Co-authored-by: Joris Mak <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2024
1 parent a0c9d6d commit 68613e1
Show file tree
Hide file tree
Showing 24 changed files with 96 additions and 65 deletions.
18 changes: 14 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,29 @@ workflows:
- documentation:
matrix:
parameters:
php-version: ["8.2"]
php-version:
- "8.3"
- tests:
matrix:
parameters:
php-version: ["8.0", "8.1", "8.2"]
php-version:
- "8.1"
- "8.2"
- "8.3"
- tests-with-future-mode:
matrix:
parameters:
php-version: ["8.0", "8.1", "8.2"]
php-version:
- "8.1"
- "8.2"
- "8.3"
- tests-with-lowest-dependencies:
matrix:
parameters:
php-version: ["8.0", "8.1", "8.2"]
php-version:
- "8.1"
- "8.2"
- "8.3"
- release-test
- release:
requires:
Expand Down
8 changes: 5 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use PedroTroller\CS\Fixer\Fixers;
use PedroTroller\CS\Fixer\RuleSetFactory;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;

return (new PhpCsFixer\Config())
return (new Config())
->setRiskyAllowed(true)
->setRules(
RuleSetFactory::create()
->per(2, true)
->phpCsFixer(true)
->php(8.0, true)
->php(8.1, true)
->pedrotroller(true)
->enable('align_multiline_comment')
->enable('array_indentation')
Expand All @@ -34,7 +36,7 @@
->setUsingCache(false)
->registerCustomFixers(new Fixers())
->setFinder(
PhpCsFixer\Finder::create()
Finder::create()
->in(__DIR__)
->append([__FILE__, __DIR__.'/bin/doc'])
)
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ Prohibited functions MUST BE commented on as prohibited

### Available options

- `functions` (*optional*): The function names to be marked how prohibited
- default: `var_dump`, `dump`, `die`

- `comment` (*optional*): The prohibition message to put in the comment
- default: `@TODO remove this line`

- `functions` (*optional*): The function names to be marked how prohibited
- default: `var_dump`, `dump`, `die`

### Configuration examples

```php
Expand Down Expand Up @@ -511,18 +511,18 @@ If the declaration of a method is too long, the arguments of this method MUST BE

### Available options

- `max-args` (*optional*): The maximum number of arguments allowed with splitting the arguments into several lines (use `false` to disable this feature)
- default: `3`

- `max-length` (*optional*): The maximum number of characters allowed with splitting the arguments into several lines
- default: `120`

- `automatic-argument-merge` (*optional*): If both conditions are met (the line is not too long and there are not too many arguments), then the arguments are put back inline
- default: `true`

- `inline-attributes` (*optional*): In the case of a split, the declaration of the attributes of the arguments of the method will be on the same line as the arguments themselves
- default: `false`

- `max-args` (*optional*): The maximum number of arguments allowed with splitting the arguments into several lines (use `false` to disable this feature)
- default: `3`

- `max-length` (*optional*): The maximum number of characters allowed with splitting the arguments into several lines
- default: `120`

### Configuration examples

```php
Expand Down
2 changes: 1 addition & 1 deletion bin/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

final class Utils
{
public static function arrayToString(array $array = null)
public static function arrayToString(?array $array = null)
{
if (null === $array) {
return;
Expand Down
5 changes: 4 additions & 1 deletion bin/doc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use PedroTroller\CS\Fixer\AbstractFixer;
use PedroTroller\CS\Fixer\Fixers;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
Expand Down Expand Up @@ -80,7 +83,7 @@ $fixers = array_map(static function (AbstractFixer $fixer) {
];
}, $samples),
];
}, iterator_to_array(new PedroTroller\CS\Fixer\Fixers()));
}, [...(new Fixers())]);

$loader = new FilesystemLoader([__DIR__]);
$twig = new Environment($loader);
Expand Down
21 changes: 11 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
"description": "PHP-CS-FIXER : my custom fixers",
"license": "MIT",
"require": {
"php": "^8.0"
"php": "^8.1",
"friendsofphp/php-cs-fixer": ">=3.59.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.28",
"friendsofphp/php-cs-fixer": "^3.60",
"phpspec/phpspec": "^7.0",
"sebastian/diff": "^4.0",
"twig/twig": "^3.3",
"webmozart/assert": "^1.10"
},
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"PedroTroller\\CS\\Fixer\\": "src/PedroTroller/CS/Fixer"
Expand All @@ -31,19 +34,17 @@
"dev-master": "3.x-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"tests": [
"tests\\Runner::run",
"tests\\Orchestra::run",
"phpspec run -fpretty"
"lint": [
"@php-cs-fixer"
],
"php-cs-fixer": [
"php-cs-fixer fix --dry-run -vvv --diff"
],
"lint": [
"@php-cs-fixer"
"tests": [
"tests\\Runner::run",
"tests\\Orchestra::run",
"phpspec run -fpretty"
]
}
}
4 changes: 2 additions & 2 deletions src/PedroTroller/CS/Fixer/AbstractFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function isCandidate(Tokens $tokens): bool

public function getName(): string
{
return sprintf('PedroTroller/%s', parent::getName());
return \sprintf('PedroTroller/%s', parent::getName());
}

/**
Expand All @@ -45,7 +45,7 @@ public function getDefinition(): FixerDefinitionInterface
return new FixerDefinition(
$this->getDocumentation(),
array_map(
fn (array $configutation = null) => new CodeSample($this->getSampleCode(), $configutation),
fn (?array $configutation = null) => new CodeSample($this->getSampleCode(), $configutation),
$this->getSampleConfigurations()
)
);
Expand Down
5 changes: 4 additions & 1 deletion src/PedroTroller/CS/Fixer/Behat/OrderBehatStepsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use PedroTroller\CS\Fixer\Priority;
use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
use PhpCsFixer\Tokenizer\Tokens;

final class OrderBehatStepsFixer extends AbstractOrderedClassElementsFixer implements ConfigurableFixerInterface
{
use ConfigurableFixerTrait;

public const ANNOTATION_PRIORITIES = [
'@BeforeSuite',
'@AfterSuite',
Expand Down Expand Up @@ -126,7 +129,7 @@ public function getDocumentation(): string
return 'Step definition methods in Behat contexts MUST BE ordered by annotation and method name.';
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
public function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('instanceof', 'Parent class or interface of your behat context classes.'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ private function getMethodsNames(array $elements): array
$methods = [];

foreach ($this->getPropertiesNames($elements) as $name) {
$methods[] = sprintf('get%s', ucfirst($name));
$methods[] = sprintf('is%s', ucfirst($name));
$methods[] = sprintf('has%s', ucfirst($name));
$methods[] = \sprintf('get%s', ucfirst($name));
$methods[] = \sprintf('is%s', ucfirst($name));
$methods[] = \sprintf('has%s', ucfirst($name));
$methods[] = lcfirst($name);
$methods[] = sprintf('set%s', ucfirst($name));
$methods[] = \sprintf('set%s', ucfirst($name));
}

return $methods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ private function cleanupMessage(Token $token): Token
return $token;
}

return new Token([T_CONSTANT_ENCAPSED_STRING, sprintf('%s%s.%s', $quotes, implode('', $chars), $quotes)]);
return new Token([T_CONSTANT_ENCAPSED_STRING, \sprintf('%s%s.%s', $quotes, implode('', $chars), $quotes)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PedroTroller\CS\Fixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
Expand All @@ -15,6 +16,8 @@

final class ForbiddenFunctionsFixer extends AbstractFixer implements ConfigurableFixerInterface
{
use ConfigurableFixerTrait;

public function getSampleCode(): string
{
return <<<'PHP'
Expand Down Expand Up @@ -53,7 +56,7 @@ public function getDocumentation(): string
return 'Prohibited functions MUST BE commented on as prohibited';
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
public function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('functions', 'The function names to be marked how prohibited'))
Expand Down Expand Up @@ -86,7 +89,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void

if (\in_array($token->getContent(), $this->configuration['functions'], true)) {
$end = $this->analyze($tokens)->getEndOfTheLine($index);
$tokens[$end] = new Token([T_WHITESPACE, sprintf(' // %s%s', $this->configuration['comment'], $tokens[$end]->getContent())]);
$tokens[$end] = new Token([T_WHITESPACE, \sprintf(' // %s%s', $this->configuration['comment'], $tokens[$end]->getContent())]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PedroTroller\CS\Fixer\Priority;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand All @@ -19,6 +20,8 @@

final class LineBreakBetweenMethodArgumentsFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface
{
use ConfigurableFixerTrait;

public const T_TYPEHINT_SEMI_COLON = 10025;

public function getPriority(): int
Expand Down Expand Up @@ -81,7 +84,7 @@ public function fun3(
SPEC;
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
public function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('max-args', 'The maximum number of arguments allowed with splitting the arguments into several lines (use `false` to disable this feature)'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ private function formatComments(array $comments, string $indentation): string
$comments = implode("\n", $comments);
$comments = trim($comments, " \n");

return sprintf("/**\n%s %s\n%s */", $indentation, $comments, $indentation);
return \sprintf("/**\n%s %s\n%s */", $indentation, $comments, $indentation);
}
}
5 changes: 4 additions & 1 deletion src/PedroTroller/CS/Fixer/DoctrineMigrationsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpCsFixer\Fixer\ClassNotation\ClassAttributesSeparationFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer;
use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand All @@ -17,6 +18,8 @@

final class DoctrineMigrationsFixer extends AbstractFixer implements ConfigurableFixerInterface
{
use ConfigurableFixerTrait;

/**
* @var string[]
*/
Expand Down Expand Up @@ -100,7 +103,7 @@ public function getPriority(): int
return Priority::before(ClassAttributesSeparationFixer::class, NoEmptyPhpdocFixer::class, NoExtraBlankLinesFixer::class);
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
public function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('instanceof', 'The parent class of which Doctrine migrations extend'))
Expand Down
7 changes: 5 additions & 2 deletions src/PedroTroller/CS/Fixer/PhpspecFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpCsFixer\Fixer\ClassNotation\VisibilityRequiredFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConfigurableFixerTrait;
use PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer;
use PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
Expand All @@ -17,6 +18,8 @@

final class PhpspecFixer extends AbstractOrderedClassElementsFixer implements ConfigurableFixerInterface
{
use ConfigurableFixerTrait;

public function getSampleConfigurations(): array
{
return [
Expand Down Expand Up @@ -101,7 +104,7 @@ public function getPriority(): int
);
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
public function createConfigurationDefinition(): FixerConfigurationResolverInterface
{
return new FixerConfigurationResolver([
(new FixerOptionBuilder('instanceof', 'Parent classes of your spec classes.'))
Expand Down Expand Up @@ -262,7 +265,7 @@ private function filterElementsByMethodName(string $regex, array $elements): arr
$filter = [];

foreach ($this->filterElementsByType('method', $elements) as $index => $method) {
if (0 !== preg_match(sprintf('/^%s$/', $regex), $method['methodName'])) {
if (0 !== preg_match(\sprintf('/^%s$/', $regex), $method['methodName'])) {
$filter[$index] = $method;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/PedroTroller/CS/Fixer/RuleSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function create(array $rules = []): self
);
}

public function per(int|float $version = null, bool $risky = false): self
public function per(null|float|int $version = null, bool $risky = false): self
{
$candidates = null !== $version
? ['@PER-CS'.number_format($version, 1, '.', '')]
Expand Down Expand Up @@ -198,7 +198,7 @@ public function pedrotroller(bool $risky = false): self
));
}

public function enable(string $name, array $config = null): self
public function enable(string $name, ?array $config = null): self
{
return self::create(array_merge(
$this->rules,
Expand Down
Loading

0 comments on commit 68613e1

Please sign in to comment.