-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMissingParamNameMalformWorker.php
More file actions
144 lines (118 loc) · 4.51 KB
/
MissingParamNameMalformWorker.php
File metadata and controls
144 lines (118 loc) · 4.51 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace Symplify\CodingStandard\TokenRunner\DocBlock\MalformWorker;
use Nette\Utils\Strings;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\DocBlock\Line;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use Symplify\CodingStandard\TokenAnalyzer\DocblockRelatedParamNamesResolver;
use Symplify\CodingStandard\TokenRunner\Contract\DocBlock\MalformWorkerInterface;
final readonly class MissingParamNameMalformWorker implements MalformWorkerInterface
{
/**
* @var string
* @see https://regex101.com/r/QtWnWv/6
*/
private const PARAM_WITHOUT_NAME_REGEX = '#@param ([^${<]*?)( ([^$]*?))?\n#';
/**
* @var string
* @see https://regex101.com/r/58YJNy/1
*/
private const PARAM_ANNOTATOIN_START_REGEX = '@param ';
/**
* @var string
* @see https://regex101.com/r/JhugsI/1
*/
private const PARAM_WITH_NAME_REGEX = '#@param(.*?)\$[\w]+(.*?)\n#';
public function __construct(
private DocblockRelatedParamNamesResolver $docblockRelatedParamNamesResolver
) {
}
/**
* @param Tokens<Token> $tokens
*/
public function work(string $docContent, Tokens $tokens, int $position): string
{
$argumentNames = $this->docblockRelatedParamNamesResolver->resolve($tokens, $position);
if ($argumentNames === []) {
return $docContent;
}
$missingArgumentNames = $this->filterOutExistingParamNames($docContent, $argumentNames);
if ($missingArgumentNames === []) {
return $docContent;
}
$docBlock = new DocBlock($docContent);
$this->completeMissingArgumentNames($missingArgumentNames, $argumentNames, $docBlock);
return $docBlock->getContent();
}
/**
* @param string[] $functionArgumentNames
* @return string[]
*/
private function filterOutExistingParamNames(string $docContent, array $functionArgumentNames): array
{
foreach ($functionArgumentNames as $key => $functionArgumentName) {
$pattern = '# ' . preg_quote($functionArgumentName, '#') . '\b#';
if (Strings::match($docContent, $pattern)) {
unset($functionArgumentNames[$key]);
}
}
return array_values($functionArgumentNames);
}
/**
* @param string[] $missingArgumentNames
* @param string[] $argumentNames
*/
private function completeMissingArgumentNames(
array $missingArgumentNames,
array $argumentNames,
DocBlock $docBlock
): void {
foreach ($missingArgumentNames as $key => $missingArgumentName) {
$newArgumentName = $this->resolveNewArgumentName($argumentNames, $missingArgumentName, $key);
$lines = $docBlock->getLines();
foreach ($lines as $line) {
if ($this->shouldSkipLine($line)) {
continue;
}
$newLineContent = $this->createNewLineContent($newArgumentName, $line);
$line->setContent($newLineContent);
continue 2;
}
}
}
/**
* @param string[] $argumentNames
*/
private function resolveNewArgumentName(array $argumentNames, string $missingArgumentName, int $key): string
{
if (array_search($missingArgumentName, $argumentNames, true)) {
return $missingArgumentName;
}
return $argumentNames[$key];
}
private function shouldSkipLine(Line $line): bool
{
if (! \str_contains($line->getContent(), self::PARAM_ANNOTATOIN_START_REGEX)) {
return true;
}
// already has a param name
if (Strings::match($line->getContent(), self::PARAM_WITH_NAME_REGEX)) {
return true;
}
$match = Strings::match($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX);
return $match === null;
}
private function createNewLineContent(string $newArgumentName, Line $line): string
{
// @see https://regex101.com/r/4FL49H/1
$missingDollarSignPattern = '#(@param\s+([\w\|\[\]\\\\]+\s)?)(' . ltrim($newArgumentName, '$') . ')#';
// missing \$ case - possibly own worker
if (Strings::match($line->getContent(), $missingDollarSignPattern)) {
return Strings::replace($line->getContent(), $missingDollarSignPattern, '$1$$3');
}
$replacement = '@param $1 ' . $newArgumentName . '$2' . "\n";
return Strings::replace($line->getContent(), self::PARAM_WITHOUT_NAME_REGEX, $replacement);
}
}