-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpaceInGenericsFixer.php
More file actions
91 lines (73 loc) · 2.44 KB
/
SpaceInGenericsFixer.php
File metadata and controls
91 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
namespace Worksome\CodingStyle\PhpCsFixer;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
use const T_DOC_COMMENT;
/** @link https://github.com/kubawerlos/php-cs-fixer-custom-fixers Modified from "PhpdocTypesCommaSpacesFixer" */
class SpaceInGenericsFixer implements FixerInterface
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'PHPDoc generic types must contain a single space after the comma.',
[new CodeSample("<?php /** @var array<class-string,bool> */\n")]
);
}
protected function fixType(string $type): string
{
return Preg::replace('/,(?!\\R)\\s*/', ', ', Preg::replace('/\\h*,/', ',', $type));
}
public function fix(SplFileInfo $file, Tokens $tokens): void
{
for ($index = $tokens->count() - 1; $index > 0; $index--) {
if (! $tokens[$index]->isGivenKind([T_DOC_COMMENT])) {
continue;
}
$docBlock = new DocBlock($tokens[$index]->getContent());
foreach ($docBlock->getAnnotations() as $annotation) {
if (! $annotation->supportTypes()) {
continue;
}
$types = $annotation->getTypes();
if ($types === []) {
continue;
}
$types = \array_map(fn (string $x): string => $this->fixType($x), $types);
$annotation->setTypes($types);
}
$newContent = $docBlock->getContent();
if ($newContent === $tokens[$index]->getContent()) {
continue;
}
$tokens[$index] = new Token([T_DOC_COMMENT, $newContent]);
}
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
public function isRisky(): bool
{
return false;
}
public function getName(): string
{
return 'Worksome/space_in_generics';
}
public function getPriority(): int
{
return 0;
}
public function supports(SplFileInfo $file): bool
{
return true;
}
}