Skip to content

Commit 2bebb44

Browse files
committed
CORE-2120
Refactor helper methods in TypeHintingArgumentsFixer Extract commonly used logic into helper methods for better readability and maintainability. This change separates complex conditions and processes from the main logic, making the code more modular and easier to understand.
1 parent 06f5530 commit 2bebb44

File tree

2 files changed

+91
-54
lines changed

2 files changed

+91
-54
lines changed

src/Fixer/PhpBasic/CodeStyle/DefaultValuesInConstructorFixer.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,21 @@ private function insertDefaultPropertiesInConstruct(
211211

212212
private function isEndOfPropertiesDeclaration(int $key, Tokens $tokens, Token $token): bool
213213
{
214-
if ($token->equals(';')) {
215-
$nextMeaningfulToken = $tokens->getNextMeaningfulToken($key);
214+
if (!$token->equals(';')) {
215+
return false;
216+
}
216217

217-
if (!$tokens[$nextMeaningfulToken]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])) {
218-
return true;
219-
}
218+
$nextMeaningfulToken = $tokens->getNextMeaningfulToken($key);
220219

221-
for ($i = 0; $i < 2; $i++) {
222-
$nextMeaningfulToken = $tokens->getNextNonWhitespace($nextMeaningfulToken);
220+
if (!$tokens[$nextMeaningfulToken]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])) {
221+
return true;
222+
}
223223

224-
if ($tokens[$nextMeaningfulToken]->isGivenKind(T_FUNCTION)) {
225-
return true;
226-
}
224+
for ($i = 0; $i < 2; $i++) {
225+
$nextMeaningfulToken = $tokens->getNextNonWhitespace($nextMeaningfulToken);
226+
227+
if ($tokens[$nextMeaningfulToken]->isGivenKind(T_FUNCTION)) {
228+
return true;
227229
}
228230
}
229231

src/Fixer/PhpBasic/Feature/TypeHintingArgumentsFixer.php

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,14 @@ private function validateObjectArguments(
159159
if (!$tokens[$i]->isGivenKind(T_VARIABLE)) {
160160
continue;
161161
}
162+
162163
$previousTokenIndex = $tokens->getPrevMeaningfulToken($i);
163164
if (!$tokens[$previousTokenIndex]->isGivenKind(T_STRING)) {
164165
foreach ($docBlock->getAnnotationsOfType('param') as $annotation) {
165166
$variable = $tokens[$i]->getContent();
166167
if (
167-
!preg_match(
168-
'#^[^$]+@param\s([^$].*?[^\s])\s\\' . $variable . '#m',
169-
$annotation->getContent(),
170-
)
171-
|| preg_match(
172-
'#^[^$]+@param\s(.*?\[\])\s\\' . $variable . '#m',
173-
$annotation->getContent(),
174-
)
168+
$this->isAnnotationWrongFormat($variable, $annotation->getContent())
169+
|| $this->isAnnotationArray($variable, $annotation->getContent())
175170
) {
176171
continue;
177172
}
@@ -187,12 +182,7 @@ private function validateObjectArguments(
187182
$nullableFound = false;
188183
$nullFound = in_array('null', $argumentTypes, true);
189184
if (!$nullFound) {
190-
foreach ($argumentTypes as $argumentType) {
191-
if (str_contains($argumentType, '?')) {
192-
$nullableFound = true;
193-
break;
194-
}
195-
}
185+
$nullableFound = $this->isNullableFound($argumentTypes);
196186
}
197187

198188
if (
@@ -219,43 +209,88 @@ private function validateObjectArguments(
219209
}
220210

221211
if ($nullFound) {
222-
/** @var Token[] $variables */
223-
$variables = (clone $tokens)->findGivenKind(
224-
T_VARIABLE,
212+
$this->processIfNullFound(
213+
$tokens,
225214
$parenthesesStartIndex,
226215
$currentParenthesesEndIndex,
216+
$variable
227217
);
228-
$variablePosition = null;
229-
foreach ($variables as $key => $variableToken) {
230-
$expectedEqual = $tokens->getNextMeaningfulToken($key);
231-
$expectedNull = $tokens->getNextMeaningfulToken($expectedEqual);
232-
if (
233-
$tokens[$expectedEqual]->getContent() === '='
234-
&& $tokens[$expectedNull]->getContent() === 'null'
235-
) {
236-
continue;
237-
}
238-
239-
if ($variableToken->getContent() === $variable) {
240-
$variablePosition = $key;
241-
break;
242-
}
243-
}
244-
245-
if ($variablePosition !== null) {
246-
$tokens->insertSlices([
247-
++$variablePosition => [
248-
new Token([T_WHITESPACE, ' ']),
249-
new Token('='),
250-
new Token([T_WHITESPACE, ' ']),
251-
new Token([T_STRING, 'null']),
252-
],
253-
]);
254-
}
255218
}
256219
break;
257220
}
258221
}
259222
}
260223
}
224+
225+
private function isAnnotationWrongFormat(string $variable, string $content): bool
226+
{
227+
return preg_match(
228+
'#^[^$]+@param\s([^$].*?[^\s])\s\\' . $variable . '#m',
229+
$content,
230+
) !== 1;
231+
}
232+
233+
private function isAnnotationArray(string $variable, string $content): bool
234+
{
235+
return preg_match(
236+
'#^[^$]+@param\s(.*?\[\])\s\\' . $variable . '#m',
237+
$content,
238+
) === 1;
239+
}
240+
241+
private function processIfNullFound(
242+
Tokens $tokens,
243+
int $parenthesesStartIndex,
244+
int $currentParenthesesEndIndex,
245+
string $variable
246+
) {
247+
/** @var Token[] $variables */
248+
$variables = (clone $tokens)->findGivenKind(
249+
T_VARIABLE,
250+
$parenthesesStartIndex,
251+
$currentParenthesesEndIndex,
252+
);
253+
$variablePosition = null;
254+
foreach ($variables as $key => $variableToken) {
255+
$expectedEqual = $tokens->getNextMeaningfulToken($key);
256+
$expectedNull = $tokens->getNextMeaningfulToken($expectedEqual);
257+
if (
258+
$tokens[$expectedEqual]->getContent() === '='
259+
&& $tokens[$expectedNull]->getContent() === 'null'
260+
) {
261+
continue;
262+
}
263+
264+
if ($variableToken->getContent() === $variable) {
265+
$variablePosition = $key;
266+
break;
267+
}
268+
}
269+
270+
if ($variablePosition !== null) {
271+
$tokens->insertSlices([
272+
++$variablePosition => [
273+
new Token([T_WHITESPACE, ' ']),
274+
new Token('='),
275+
new Token([T_WHITESPACE, ' ']),
276+
new Token([T_STRING, 'null']),
277+
],
278+
]);
279+
}
280+
}
281+
282+
/**
283+
* @param string[] $argumentTypes
284+
* @return bool
285+
*/
286+
private function isNullableFound(array $argumentTypes): bool
287+
{
288+
foreach ($argumentTypes as $argumentType) {
289+
if (str_contains($argumentType, '?')) {
290+
return true;
291+
}
292+
}
293+
294+
return false;
295+
}
261296
}

0 commit comments

Comments
 (0)