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

fix: priority with single_line_empty_body #206

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
->setRiskyAllowed(true)
->setRules(
RuleSetFactory::create()
->per(2, true)
->phpCsFixer(true)
->php(8.0, true)
->pedrotroller(true)
Expand Down
2 changes: 1 addition & 1 deletion bin/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function arrayToString(array $array = null)
if (array_values($array) === $array) {
$string .= implode(', ', array_map([self::class, 'valueToString'], $array));
} else {
$string .= implode(', ', array_map(fn ($value, $key) => '\''.$key.'\' => '.self::valueToString($value), $array, array_keys($array)));
$string .= implode(', ', array_map(static fn ($value, $key) => '\''.$key.'\' => '.self::valueToString($value), $array, array_keys($array)));
}

$string .= ' ]';
Expand Down
12 changes: 6 additions & 6 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include sprintf('%s/../vendor/autoload.php', __DIR__);

include 'Utils.php';

$fixers = array_map(function (AbstractFixer $fixer) {
$fixers = array_map(static function (AbstractFixer $fixer) {
$samples = $fixer->getDefinition()->getCodeSamples();

return [
Expand All @@ -25,7 +25,7 @@ $fixers = array_map(function (AbstractFixer $fixer) {
'deprecated' => $fixer->isDeprecated(),
'replacement' => $fixer->getDeprecationReplacement(),
'options' => array_map(
function (FixerOptionInterface $option) {
static function (FixerOptionInterface $option) {
return [
'name' => $option->getName(),
'description' => $option->getDescription(),
Expand All @@ -39,7 +39,7 @@ $fixers = array_map(function (AbstractFixer $fixer) {
? $fixer->getConfigurationDefinition()->getOptions()
: []
),
'samples' => array_map(function (CodeSample $sample) use ($fixer) {
'samples' => array_map(static function (CodeSample $sample) use ($fixer) {
if ($fixer instanceof ConfigurableFixerInterface) {
$fixer->configure($sample->getConfiguration());
}
Expand All @@ -57,13 +57,13 @@ $fixers = array_map(function (AbstractFixer $fixer) {
}

while (strlen($line) < 80 + 1) {
$line = $line.' ';
$line .= ' ';
}

if (0 === $num) {
$line = $line.'// 80 chars';
$line .= '// 80 chars';
} else {
$line = $line.'//';
$line .= '//';
}

$diff[$num] = $line;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"php": "^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.17",
"friendsofphp/php-cs-fixer": "^3.28",
"phpspec/phpspec": "^7.0",
"sebastian/diff": "^4.0",
"twig/twig": "^3.3",
Expand Down
6 changes: 5 additions & 1 deletion doc/rule-set-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ return PhpCsFixer\Config::create()
->setRules(RuleSetFactory::create()
->symfony() // Activate the @Symfony ruleset
->phpCsFixer() // Activate the @PhpCsFixer ruleset
->php(5.6, true) // Activate php 5.6 risky rules
->php(8.2, true) // Activate php 8.2 risky rules
->pedrotroller(true) // Activate my own ruleset (with risky rules)
->enable('ordered_imports') // Add an other rule
->disable('yoda_style') // Disable a rule
Expand All @@ -26,6 +26,10 @@ return PhpCsFixer\Config::create()

## Methods

### `->per([int|float $version = null, [bool $risky = false]])`

Activate the `@PER` (`@PER-CS1.0`, `@PER-CS1.0:risky`, `@PER-CS2.0`, `@PER-CS2.0:risky`, ...) rule.

### `->psr0()`

Activate the `@psr0` rule.
Expand Down
48 changes: 35 additions & 13 deletions spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,46 @@

final class RuleSetFactorySpec extends ObjectBehavior
{
function let()
{
$this->beConstructedThrough('create');
}

function it_is_initializable()
{
$this->shouldHaveType(RuleSetFactory::class);
}

function it_adds_a_per_set()
{
$this->per()->getRules()->shouldReturn(['@PER' => true]);
}

function it_adds_a_per_risky_set()
{
$this->per(risky: true)->getRules()->shouldReturn(['@PER:risky' => true]);
}

function it_adds_a_per1_0_set()
{
$this->per(1)->getRules()->shouldReturn(['@PER-CS1.0' => true]);
}

function it_adds_a_per1_0_risky_set()
{
$this->per(1, true)->getRules()->shouldReturn(['@PER-CS1.0:risky' => true]);
}

function it_adds_a_per2_0_set()
{
$this->per(2)->getRules()->shouldReturn(['@PER-CS2.0' => true]);
}

function it_adds_a_per2_0_risky_set()
{
$this->per(2, true)->getRules()->shouldReturn(['@PER-CS2.0:risky' => true]);
}

function it_adds_a_psr0_set()
{
$this->psr0()->getRules()->shouldReturn(['@psr0' => true]);
Expand Down Expand Up @@ -144,15 +179,6 @@ function it_adds_a_php_version_support()
]);
}

function it_can_also_parse_versions_as_string()
{
$this->php('5.6.2')->getRules()->shouldReturn([
'@PHP54Migration' => true,
'array_syntax' => ['syntax' => 'short'],
'list_syntax' => ['syntax' => 'long'],
]);
}

function it_adds_a_phpunit_version_support()
{
$this->phpUnit(2.0, false)->getRules()->shouldReturn([]);
Expand Down Expand Up @@ -310,8 +336,6 @@ function it_adds_my_own_fixer_set()
$rules[$fixer->getName()] = true;
}

ksort($rules);

$this->pedrotroller(true)->getRules()->shouldReturn($rules);
}

Expand All @@ -327,8 +351,6 @@ function it_adds_my_own_fixer_set_except_privates()
$rules[$fixer->getName()] = true;
}

ksort($rules);

$this->pedrotroller(false)->getRules()->shouldReturn($rules);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ private function getMethodsNames(array $elements): array

private function getPropertiesNames(array $elements): array
{
$properties = array_filter($elements, fn ($element) => 'property' === $element['type']);
$properties = array_filter($elements, static fn ($element) => 'property' === $element['type']);

return array_map(fn ($element) => ltrim($element['propertyName'], '$'), $properties);
return array_map(static fn ($element) => ltrim($element['propertyName'], '$'), $properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PedroTroller\CS\Fixer\AbstractFixer;
use PedroTroller\CS\Fixer\Priority;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
Expand All @@ -22,7 +24,11 @@ final class LineBreakBetweenMethodArgumentsFixer extends AbstractFixer implement

public function getPriority(): int
{
return Priority::after(BracesFixer::class);
return min(
Priority::after(BracesFixer::class),
Priority::after(MethodArgumentSpaceFixer::class),
Priority::after(SingleLineEmptyBodyFixer::class),
);
}

public function getSampleConfigurations(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function formatComments(array $comments, string $indentation): string
array_pop($comments);
}

$comments = array_map(fn ($comment) => rtrim($indentation.' * '.ltrim($comment, ' /')), $comments);
$comments = array_map(static fn ($comment) => rtrim($indentation.' * '.ltrim($comment, ' /')), $comments);

$comments = implode("\n", $comments);
$comments = trim($comments, " \n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
$possible = array_merge($possible, array_keys($ends));
}

$possible = array_filter($possible, fn ($value) => null !== $value);
$possible = array_filter($possible, static fn ($value) => null !== $value);

if (empty($possible)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/PedroTroller/CS/Fixer/Fixers.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getIterator(): Generator
;

$files = array_map(
fn ($file) => $file->getPathname(),
static fn ($file) => $file->getPathname(),
iterator_to_array($finder)
);

Expand Down
8 changes: 3 additions & 5 deletions src/PedroTroller/CS/Fixer/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

final class Priority
{
private function __construct()
{
}
private function __construct() {}

/**
* @param array<int, mixed> $classes
Expand All @@ -18,7 +16,7 @@ private function __construct()
public static function before(...$classes)
{
$priorities = array_map(
fn ($class) => (new $class())->getPriority(),
static fn ($class) => (new $class())->getPriority(),
$classes
);

Expand All @@ -33,7 +31,7 @@ public static function before(...$classes)
public static function after(...$classes)
{
$priorities = array_map(
fn ($class) => (new $class())->getPriority(),
static fn ($class) => (new $class())->getPriority(),
$classes
);

Expand Down
Loading