Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dx_update' into dx_update
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Apr 26, 2024
2 parents 93b450b + a6b4b5a commit 09a5014
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 46 deletions.
2 changes: 1 addition & 1 deletion doc/rules/basic/braces.rst
Expand Up @@ -13,7 +13,7 @@ This rule is deprecated and will be removed in the next major version

You should use ``single_space_around_construct``, ``control_structure_braces``,
``control_structure_continuation_position``, ``declare_parentheses``,
``no_multiple_statements_per_line``, ``curly_braces_position``,
``no_multiple_statements_per_line``, ``braces_position``,
``statement_indentation`` and ``no_extra_blank_lines`` instead.

Configuration
Expand Down
20 changes: 10 additions & 10 deletions src/Fixer/Basic/BracesFixer.php
Expand Up @@ -51,9 +51,9 @@ final class BracesFixer extends AbstractProxyFixer implements ConfigurableFixerI
public const LINE_SAME = 'same';

/**
* @var null|CurlyBracesPositionFixer
* @var null|BracesPositionFixer
*/
private $curlyBracesPositionFixer;
private $bracesPositionFixer;

/**
* @var null|ControlStructureContinuationPositionFixer
Expand Down Expand Up @@ -156,7 +156,7 @@ public function configure(array $configuration): void
{
parent::configure($configuration);

$this->getCurlyBracesPositionFixer()->configure([
$this->getBracesPositionFixer()->configure([
'control_structures_opening_brace' => $this->translatePositionOption($this->configuration['position_after_control_structures']),
'functions_opening_brace' => $this->translatePositionOption($this->configuration['position_after_functions_and_oop_constructs']),
'anonymous_functions_opening_brace' => $this->translatePositionOption($this->configuration['position_after_anonymous_constructs']),
Expand Down Expand Up @@ -217,21 +217,21 @@ protected function createProxyFixers(): array
$singleSpaceAroundConstructFixer,
new ControlStructureBracesFixer(),
$noExtraBlankLinesFixer,
$this->getCurlyBracesPositionFixer(),
$this->getBracesPositionFixer(),
$this->getControlStructureContinuationPositionFixer(),
new DeclareParenthesesFixer(),
new NoMultipleStatementsPerLineFixer(),
new StatementIndentationFixer(true),
];
}

private function getCurlyBracesPositionFixer(): CurlyBracesPositionFixer
private function getBracesPositionFixer(): BracesPositionFixer
{
if (null === $this->curlyBracesPositionFixer) {
$this->curlyBracesPositionFixer = new CurlyBracesPositionFixer();
if (null === $this->bracesPositionFixer) {
$this->bracesPositionFixer = new BracesPositionFixer();
}

return $this->curlyBracesPositionFixer;
return $this->bracesPositionFixer;
}

private function getControlStructureContinuationPositionFixer(): ControlStructureContinuationPositionFixer
Expand All @@ -246,7 +246,7 @@ private function getControlStructureContinuationPositionFixer(): ControlStructur
private function translatePositionOption(string $option): string
{
return self::LINE_NEXT === $option
? CurlyBracesPositionFixer::NEXT_LINE_UNLESS_NEWLINE_AT_SIGNATURE_END
: CurlyBracesPositionFixer::SAME_LINE;
? BracesPositionFixer::NEXT_LINE_UNLESS_NEWLINE_AT_SIGNATURE_END
: BracesPositionFixer::SAME_LINE;
}
}
13 changes: 3 additions & 10 deletions tests/RuleSet/RuleSetTest.php
Expand Up @@ -23,6 +23,7 @@
use PhpCsFixer\RuleSet\RuleSet;
use PhpCsFixer\RuleSet\RuleSetDescriptionInterface;
use PhpCsFixer\RuleSet\RuleSets;
use PhpCsFixer\Tests\Test\TestCaseUtils;
use PhpCsFixer\Tests\TestCase;

/**
Expand Down Expand Up @@ -85,11 +86,7 @@ public function testIfAllRulesInSetsExists(string $setName, string $ruleName, $r
*/
public function testThatDefaultConfigIsNotPassed(string $setName, string $ruleName, $ruleConfig): void
{
$factory = new FixerFactory();
$factory->registerBuiltInFixers();
$factory->useRuleSet(new RuleSet([$ruleName => true]));

$fixer = current($factory->getFixers());
$fixer = TestCaseUtils::getFixerByName($ruleName);

if (!$fixer instanceof ConfigurableFixerInterface || \is_bool($ruleConfig)) {
$this->expectNotToPerformAssertions();
Expand Down Expand Up @@ -119,11 +116,7 @@ public function testThatDefaultConfigIsNotPassed(string $setName, string $ruleNa
*/
public function testThatThereIsNoDeprecatedFixerInRuleSet(string $setName, string $ruleName): void
{
$factory = new FixerFactory();
$factory->registerBuiltInFixers();
$factory->useRuleSet(new RuleSet([$ruleName => true]));

$fixer = current($factory->getFixers());
$fixer = TestCaseUtils::getFixerByName($ruleName);

self::assertNotInstanceOf(DeprecatedFixerInterface::class, $fixer, sprintf('RuleSet "%s" contains deprecated rule "%s".', $setName, $ruleName));
}
Expand Down
28 changes: 3 additions & 25 deletions tests/RuleSet/RuleSetsTest.php
Expand Up @@ -14,12 +14,11 @@

namespace PhpCsFixer\Tests\RuleSet;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
use PhpCsFixer\FixerFactory;
use PhpCsFixer\RuleSet\RuleSet;
use PhpCsFixer\RuleSet\RuleSets;
use PhpCsFixer\Tests\Test\TestCaseUtils;
use PhpCsFixer\Tests\TestCase;

/**
Expand Down Expand Up @@ -187,7 +186,7 @@ private static function assertPHPUnitVersionIsLargestAllowed(string $setName, st
{
$maximumVersionForRuleset = preg_replace('/^@PHPUnit(\d+)(\d)Migration:risky$/', '$1.$2', $setName);

$fixer = self::getFixerByName($ruleName);
$fixer = TestCaseUtils::getFixerByName($ruleName);

self::assertInstanceOf(ConfigurableFixerInterface::class, $fixer, sprintf('The fixer "%s" shall be configurable.', $fixer->getName()));

Expand Down Expand Up @@ -285,7 +284,7 @@ private function allInteger(array $values): bool
private function getDefaultPHPUnitTargetOfRule(string $ruleName): ?string
{
$targetVersion = null;
$fixer = self::getFixerByName($ruleName);
$fixer = TestCaseUtils::getFixerByName($ruleName);

if ($fixer instanceof ConfigurableFixerInterface) {
foreach ($fixer->getConfigurationDefinition()->getOptions() as $option) {
Expand All @@ -299,25 +298,4 @@ private function getDefaultPHPUnitTargetOfRule(string $ruleName): ?string

return $targetVersion;
}

private static function getFixerByName(string $name): AbstractFixer
{
$factory = new FixerFactory();
$factory->registerBuiltInFixers();
$factory->useRuleSet(new RuleSet([$name => true]));

$fixers = $factory->getFixers();

if (0 === \count($fixers)) {
throw new \RuntimeException('FixerFactory unexpectedly returned empty array.');
}

$fixer = current($fixers);

if (!$fixer instanceof AbstractFixer) {
throw new \RuntimeException(sprintf('Fixer class for "%s" rule does not extend "%s".', $name, AbstractFixer::class));
}

return $fixer;
}
}
21 changes: 21 additions & 0 deletions tests/Test/AbstractFixerTestCase.php
Expand Up @@ -322,6 +322,27 @@ final public function testDeprecatedFixersHaveCorrectSummary(): void
}
}

final public function testDeprecatedFixersDoNotHaveDeprecatedSuccessor(): void
{
if (!$this->fixer instanceof DeprecatedFixerInterface || [] === $this->fixer->getSuccessorsNames()) {
$this->addToAssertionCount(1);

return;
}

foreach ($this->fixer->getSuccessorsNames() as $successorName) {
self::assertNotInstanceOf(
DeprecatedFixerInterface::class,
TestCaseUtils::getFixerByName($successorName),
sprintf(
'Successor fixer `%s` for deprecated fixer `%s` is deprecated itself.',
$successorName,
$this->fixer->getName(),
)
);
}
}

/**
* Blur filter that find candidate fixer for performance optimization to use only `insertSlices` instead of multiple `insertAt` if there is no other collection manipulation.
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/Test/TestCaseUtils.php
Expand Up @@ -14,6 +14,9 @@

namespace PhpCsFixer\Tests\Test;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerFactory;

/**
* @internal
*/
Expand All @@ -38,4 +41,25 @@ public static function swapExpectedInputTestCases(iterable $cases): iterable
yield $case;
}
}

public static function getFixerByName(string $name): FixerInterface
{
static $fixers = null;

if (null === $fixers) {
$factory = new FixerFactory();
$factory->registerBuiltInFixers();

$fixers = [];
foreach ($factory->getFixers() as $fixer) {
$fixers[$fixer->getName()] = $fixer;
}
}

if (!\array_key_exists($name, $fixers)) {
throw new \InvalidArgumentException(sprintf('Fixer "%s" does not exist.', $name));
}

return $fixers[$name];
}
}

0 comments on commit 09a5014

Please sign in to comment.