Skip to content

Commit 7b39377

Browse files
committed
feat: Allow to enforce ellipsis instead of three dots
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>
1 parent bc9c53a commit 7b39377

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/Config.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@
44

55
namespace Nextcloud\CodingStandard;
66

7+
use Fidry\CpuCoreCounter\NumberOfCpuCoreNotFound;
8+
use Nextcloud\CodingStandard\Fixer\NoThreeDotsInStringFixer;
79
use PhpCsFixer\Config as Base;
810

911
class Config extends Base {
10-
public function __construct($name = 'default') {
12+
13+
/**
14+
* @inheritdoc
15+
* @param bool $enableStringRules Enable rules to improve string quality
16+
*/
17+
public function __construct($name = 'default',
18+
protected bool $enableStringRules = false,
19+
) {
1120
parent::__construct($name);
1221
$this->setIndent("\t");
22+
$this->registerCustomFixers([
23+
new NoThreeDotsInStringFixer(),
24+
]);
1325
}
1426

1527
public function getRules() : array {
@@ -72,6 +84,7 @@ public function getRules() : array {
7284
'elements' => ['property', 'method', 'const']
7385
],
7486
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
87+
'Nextcloud/no_three_dots_in_string' => $this->enableStringRules,
7588
];
7689
}
7790
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nextcloud\CodingStandard\Fixer;
6+
7+
use PhpCsFixer\Fixer\FixerInterface;
8+
use PhpCsFixer\FixerDefinition\CodeSample;
9+
use PhpCsFixer\FixerDefinition\FixerDefinition;
10+
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
11+
use PhpCsFixer\Tokenizer\Token;
12+
use PhpCsFixer\Tokenizer\Tokens;
13+
14+
final class NoThreeDotsInStringFixer implements FixerInterface {
15+
16+
public function isCandidate(Tokens $tokens): bool {
17+
return $tokens->isAnyTokenKindsFound([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE]);
18+
}
19+
20+
public function isRisky(): bool {
21+
return true;
22+
}
23+
24+
public function getName(): string {
25+
return 'Nextcloud/no_three_dots_in_string';
26+
}
27+
28+
public function getPriority(): int
29+
{
30+
return 0;
31+
}
32+
33+
public function supports(\SplFileInfo $file): bool {
34+
return true;
35+
}
36+
37+
public function getDefinition(): FixerDefinitionInterface {
38+
return new FixerDefinition(
39+
'There must be no three dots in strings, instead ellipsis shall be used.',
40+
[
41+
new CodeSample(
42+
"<?php \$a = 'Loading ...';\n"
43+
),
44+
],
45+
null,
46+
'Changing the characters in strings might affect string comparisons and outputs.'
47+
);
48+
}
49+
50+
public function fix(\SplFileInfo $file, Tokens $tokens): void {
51+
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
52+
/** @var Token $token */
53+
$token = $tokens[$index];
54+
55+
if (!$token->isGivenKind([T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE])) {
56+
continue;
57+
}
58+
59+
$content = str_replace('...', '', $token->getContent());
60+
if ($token->getContent() === $content) {
61+
continue;
62+
}
63+
64+
$tokens[$index] = new Token([$tokens[$index]->getId(), $content]);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)