Skip to content

Format AsCommand attributes #2786

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

Merged
merged 1 commit into from
Jun 12, 2025
Merged

Format AsCommand attributes #2786

merged 1 commit into from
Jun 12, 2025

Conversation

adriendupuis
Copy link
Contributor

@adriendupuis adriendupuis commented Jun 11, 2025

Question Answer
JIRA Ticket
Versions
Edition
  • I copied the code_samples/ directory in a Commerce 5.0.x-dev under DDEV
  • I added the following src/Fixer/AttributeFormatFixer.php
<?php

/**
 * @copyright Copyright (C) Ibexa AS. All rights reserved.
 * @license For full copyright and license information view LICENSE file distributed with this source code.
 */
declare(strict_types=1);

namespace App\Fixer;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class AttributeFormatFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
{
    public function getDefinition(): FixerDefinitionInterface
    {
        return new FixerDefinition(
            'Format attributes with new lines',
            [/*TODO*/]
        );
    }

    public function getName(): string
    {
        return 'App/' . parent::getName();
    }

    public function isCandidate(Tokens $tokens): bool
    {
        return \defined('T_ATTRIBUTE') && $tokens->isTokenKindFound(T_ATTRIBUTE);
    }

    public function isRisky(): bool
    {
        return true; // TODO
    }

    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
    {
        $index = 0;

        while (null !== $index = $tokens->getNextTokenOfKind($index, [[T_ATTRIBUTE]])) {
            $openingParenthesisIndex = $tokens->getNextTokenOfKind($index, ['(']);
            $closingParenthesisIndex = $tokens->getNextTokenOfKind($index, [')']);
            if ($openingParenthesisIndex + 1 < $closingParenthesisIndex) {
                $whitespaceIndex = $tokens->getNextTokenOfKind($openingParenthesisIndex, [[T_WHITESPACE]]);
                if ($openingParenthesisIndex + 1 < $whitespaceIndex) {
                    $tokens->insertAt($openingParenthesisIndex + 1, [
                        new Token($this->whitespacesConfig->getLineEnding()),
                        new Token($this->whitespacesConfig->getIndent()),
                    ]);
                    $commaIndex = $openingParenthesisIndex;
                    while ((null !== $commaIndex = $tokens->getNextTokenOfKind($commaIndex, [','])) && ($commaIndex < $closingParenthesisIndex)) {
                        $insertAt = $commaIndex + 1;
                        $whitespaceIndex = $tokens->getNextTokenOfKind($commaIndex, [[T_WHITESPACE]]);
                        if ($commaIndex + 1 === $whitespaceIndex) {
                            $tokens->clearAt($whitespaceIndex);
                            $insertAt = $commaIndex + 2;
                        }
                        $tokens->insertAt($insertAt, [
                            new Token($this->whitespacesConfig->getLineEnding()),
                            new Token($this->whitespacesConfig->getIndent()),
                        ]);
                    }
                    $closingParenthesisIndex = $tokens->getNextTokenOfKind($index, [')']);
                    $tokens->insertAt($closingParenthesisIndex, [
                        new Token($this->whitespacesConfig->getLineEnding()),
                    ]);
                }
            }
        }
    }
}
  • I added the following .php-cs-fixer.php
<?php

$factory = new Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory();

// You can omit the call below if you want Ibexa ruleset with no custom rules
$factory->withRules([
    'header_comment' => false,
    'App/attribute_format' => true,
]);
$config = $factory->buildConfig();
$config->setFinder(
    PhpCsFixer\Finder::create()
        //->in(__DIR__ . '/src')
        ->in(__DIR__ . '/code_samples')
        //->in(__DIR__ . '/tests')
        ->files()->name('*.php')
);
$config->setRiskyAllowed(true);
$config->registerCustomFixers([
    new App\Fixer\AttributeFormatFixer()
]);

return $config;
  • I run ddev exec rm .php-cs-fixer.cache; ddev exec php-cs-fixer fix --config=.php-cs-fixer.php -vv
  • I copied back the directory in to our beloved documentation. Only the commands has been changed. I check manually that new lines were OK.

Checklist

  • Text renders correctly
  • Text has been checked with vale
  • Description metadata is up to date
  • Redirects cover removed/moved pages
  • Code samples are working
  • PHP code samples have been fixed with PHP CS fixer
  • Added link to this PR in relevant JIRA ticket or code PR

Copy link

Preview of modified files: no change to preview.

@adriendupuis adriendupuis requested a review from mnocon June 11, 2025 14:11
@adriendupuis adriendupuis marked this pull request as ready for review June 11, 2025 14:11
Copy link

code_samples/ change report

Report's diff is too long to be displayed in a comment.

Download colorized diff

@mnocon
Copy link
Contributor

mnocon commented Jun 12, 2025

This is great, thank you! I think we could make the Fixer rule public at some point, it will be definitely useful for us to have it automated

@mnocon mnocon merged commit e1b98d1 into add-rector-ci Jun 12, 2025
7 of 8 checks passed
@mnocon mnocon deleted the add-rector-ci-ad-fix branch June 12, 2025 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants