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

feat: Allow to enforce ellipsis instead of three dots #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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]);
}
}
}
Loading