Skip to content

Commit

Permalink
feat: Allow to enforce ellipsis instead of three dots
Browse files Browse the repository at this point in the history
Our translation and design guidelines say to use ellipsis instead of three dots,
yet some code is using three dots instead of the ellipsis character.
This adds a custom fixer and rule to allow auto-fixing those strings.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Aug 30, 2024
1 parent bc9c53a commit 0ceffd5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@

namespace Nextcloud\CodingStandard;

use Nextcloud\CodingStandard\Fixer\NoThreeDotsInStringFixer;
use PhpCsFixer\Config as Base;

class Config extends Base {
public function __construct($name = 'default') {

/**
* @inheritdoc
* @param bool $enableStringRules Enable rules to improve string quality
*/
public function __construct($name = 'default',
protected bool $enableStringRules = false,
) {
parent::__construct($name);
$this->setIndent("\t");
$this->registerCustomFixers([
new NoThreeDotsInStringFixer(),
]);
}

public function getRules() : array {
Expand Down Expand Up @@ -72,6 +83,7 @@ public function getRules() : array {
'elements' => ['property', 'method', 'const']
],
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
'Nextcloud/no_three_dots_in_string' => $this->enableStringRules,
];
}
}
66 changes: 66 additions & 0 deletions src/Fixer/NoThreeDotsInStringFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Nextcloud\CodingStandard\Fixer;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class NoThreeDotsInStringFixer implements FixerInterface {

public function isCandidate(Tokens $tokens): bool {
return $tokens->isAnyTokenKindsFound([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE]);
}

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

public function getName(): string {
return 'Nextcloud/no_three_dots_in_string';
}

public function getPriority(): int {
return 0;
}

public function supports(\SplFileInfo $file): bool {
return true;
}

public function getDefinition(): FixerDefinitionInterface {
return new FixerDefinition(
'There must be no three dots in strings, instead ellipsis shall be used.',
[
new CodeSample(
"<?php \$a = 'Loading ...';\n"
),
],
null,
'Changing the characters in strings might affect string comparisons and outputs.'
);
}

public function fix(\SplFileInfo $file, Tokens $tokens): void {
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
/** @var Token $token */
$token = $tokens[$index];

if (!$token->isGivenKind([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE])) {
continue;
}

$content = str_replace('...', '', $token->getContent());
if ($token->getContent() === $content) {
continue;
}

$tokens[$index] = new Token([$tokens[$index]->getId(), $content]);
}
}
}

0 comments on commit 0ceffd5

Please sign in to comment.