/
FunctionHelper.php
562 lines (470 loc) · 16.3 KB
/
FunctionHelper.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php declare(strict_types = 1);
namespace SlevomatCodingStandard\Helpers;
use Generator;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use SlevomatCodingStandard\Helpers\Annotation\ParameterAnnotation;
use SlevomatCodingStandard\Helpers\Annotation\ReturnAnnotation;
use SlevomatCodingStandard\Helpers\Annotation\VariableAnnotation;
use function array_filter;
use function array_map;
use function array_merge;
use function array_pop;
use function array_reverse;
use function count;
use function in_array;
use function iterator_to_array;
use function preg_match;
use function preg_replace;
use function sprintf;
use function substr_count;
use const T_ANON_CLASS;
use const T_BITWISE_AND;
use const T_CLASS;
use const T_CLOSURE;
use const T_COLON;
use const T_ELLIPSIS;
use const T_ENUM;
use const T_FUNCTION;
use const T_INTERFACE;
use const T_NULLABLE;
use const T_RETURN;
use const T_SEMICOLON;
use const T_STRING;
use const T_TRAIT;
use const T_USE;
use const T_VARIABLE;
use const T_WHITESPACE;
use const T_YIELD;
use const T_YIELD_FROM;
/**
* @internal
*/
class FunctionHelper
{
public const LINE_INCLUDE_COMMENT = 1;
public const LINE_INCLUDE_WHITESPACE = 2;
public const SPECIAL_FUNCTIONS = [
'array_key_exists',
'array_slice',
'boolval',
'call_user_func',
'call_user_func_array',
'chr',
'count',
'doubleval',
'defined',
'floatval',
'func_get_args',
'func_num_args',
'get_called_class',
'get_class',
'gettype',
'in_array',
'intval',
'is_array',
'is_bool',
'is_double',
'is_float',
'is_long',
'is_int',
'is_integer',
'is_null',
'is_object',
'is_real',
'is_resource',
'is_string',
'ord',
'sizeof',
'strlen',
'strval',
];
public static function getTypeLabel(File $phpcsFile, int $functionPointer): string
{
return self::isMethod($phpcsFile, $functionPointer) ? 'Method' : 'Function';
}
public static function getName(File $phpcsFile, int $functionPointer): string
{
$tokens = $phpcsFile->getTokens();
return $tokens[TokenHelper::findNext(
$phpcsFile,
T_STRING,
$functionPointer + 1,
$tokens[$functionPointer]['parenthesis_opener']
)]['content'];
}
public static function getFullyQualifiedName(File $phpcsFile, int $functionPointer): string
{
$name = self::getName($phpcsFile, $functionPointer);
$namespace = NamespaceHelper::findCurrentNamespaceName($phpcsFile, $functionPointer);
if (self::isMethod($phpcsFile, $functionPointer)) {
foreach (array_reverse(
$phpcsFile->getTokens()[$functionPointer]['conditions'],
true
) as $conditionPointer => $conditionTokenCode) {
if ($conditionTokenCode === T_ANON_CLASS) {
return sprintf('class@anonymous::%s', $name);
}
if (in_array($conditionTokenCode, [T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], true)) {
$name = sprintf(
'%s%s::%s',
NamespaceHelper::NAMESPACE_SEPARATOR,
ClassHelper::getName($phpcsFile, $conditionPointer),
$name
);
break;
}
}
return $namespace !== null ? sprintf('%s%s%s', NamespaceHelper::NAMESPACE_SEPARATOR, $namespace, $name) : $name;
}
return $namespace !== null
? sprintf('%s%s%s%s', NamespaceHelper::NAMESPACE_SEPARATOR, $namespace, NamespaceHelper::NAMESPACE_SEPARATOR, $name)
: $name;
}
public static function isAbstract(File $phpcsFile, int $functionPointer): bool
{
return !isset($phpcsFile->getTokens()[$functionPointer]['scope_opener']);
}
public static function isMethod(File $phpcsFile, int $functionPointer): bool
{
$functionPointerConditions = $phpcsFile->getTokens()[$functionPointer]['conditions'];
if ($functionPointerConditions === []) {
return false;
}
$lastFunctionPointerCondition = array_pop($functionPointerConditions);
return in_array($lastFunctionPointerCondition, Tokens::$ooScopeTokens, true);
}
public static function findClassPointer(File $phpcsFile, int $functionPointer): ?int
{
$tokens = $phpcsFile->getTokens();
if ($tokens[$functionPointer]['code'] === T_CLOSURE) {
return null;
}
foreach (array_reverse($tokens[$functionPointer]['conditions'], true) as $conditionPointer => $conditionTokenCode) {
if (!in_array($conditionTokenCode, Tokens::$ooScopeTokens, true)) {
continue;
}
return $conditionPointer;
}
return null;
}
/**
* @return string[]
*/
public static function getParametersNames(File $phpcsFile, int $functionPointer): array
{
$tokens = $phpcsFile->getTokens();
$parametersNames = [];
for ($i = $tokens[$functionPointer]['parenthesis_opener'] + 1; $i < $tokens[$functionPointer]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE) {
continue;
}
$parametersNames[] = $tokens[$i]['content'];
}
return $parametersNames;
}
/**
* @return (TypeHint|null)[]
*/
public static function getParametersTypeHints(File $phpcsFile, int $functionPointer): array
{
$tokens = $phpcsFile->getTokens();
$parametersTypeHints = [];
for ($i = $tokens[$functionPointer]['parenthesis_opener'] + 1; $i < $tokens[$functionPointer]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE) {
continue;
}
$parameterName = $tokens[$i]['content'];
$pointerBeforeVariable = TokenHelper::findPreviousExcluding(
$phpcsFile,
array_merge(TokenHelper::$ineffectiveTokenCodes, [T_BITWISE_AND, T_ELLIPSIS]),
$i - 1
);
if (!in_array($tokens[$pointerBeforeVariable]['code'], TokenHelper::getTypeHintTokenCodes(), true)) {
$parametersTypeHints[$parameterName] = null;
continue;
}
$typeHintEndPointer = $pointerBeforeVariable;
$typeHintStartPointer = TypeHintHelper::getStartPointer($phpcsFile, $typeHintEndPointer);
$pointerBeforeTypeHint = TokenHelper::findPreviousEffective($phpcsFile, $typeHintStartPointer - 1);
$isNullable = $tokens[$pointerBeforeTypeHint]['code'] === T_NULLABLE;
if ($isNullable) {
$typeHintStartPointer = $pointerBeforeTypeHint;
}
$typeHint = TokenHelper::getContent($phpcsFile, $typeHintStartPointer, $typeHintEndPointer);
/** @var string $typeHint */
$typeHint = preg_replace('~\s+~', '', $typeHint);
if (!$isNullable) {
$isNullable = preg_match('~(?:^|\|)null(?:\||$)~i', $typeHint) === 1;
}
$parametersTypeHints[$parameterName] = new TypeHint($typeHint, $isNullable, $typeHintStartPointer, $typeHintEndPointer);
}
return $parametersTypeHints;
}
public static function returnsValue(File $phpcsFile, int $functionPointer): bool
{
$tokens = $phpcsFile->getTokens();
$firstPointerInScope = $tokens[$functionPointer]['scope_opener'] + 1;
for ($i = $firstPointerInScope; $i < $tokens[$functionPointer]['scope_closer']; $i++) {
if (!in_array($tokens[$i]['code'], [T_YIELD, T_YIELD_FROM], true)) {
continue;
}
if (!ScopeHelper::isInSameScope($phpcsFile, $i, $firstPointerInScope)) {
continue;
}
return true;
}
for ($i = $firstPointerInScope; $i < $tokens[$functionPointer]['scope_closer']; $i++) {
if ($tokens[$i]['code'] !== T_RETURN) {
continue;
}
if (!ScopeHelper::isInSameScope($phpcsFile, $i, $firstPointerInScope)) {
continue;
}
$nextEffectiveTokenPointer = TokenHelper::findNextEffective($phpcsFile, $i + 1);
return $tokens[$nextEffectiveTokenPointer]['code'] !== T_SEMICOLON;
}
return false;
}
public static function findReturnTypeHint(File $phpcsFile, int $functionPointer): ?TypeHint
{
$tokens = $phpcsFile->getTokens();
$nextPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$functionPointer]['parenthesis_closer'] + 1);
if ($tokens[$nextPointer]['code'] === T_USE) {
$useParenthesisOpener = TokenHelper::findNextEffective($phpcsFile, $nextPointer + 1);
$colonPointer = TokenHelper::findNextEffective($phpcsFile, $tokens[$useParenthesisOpener]['parenthesis_closer'] + 1);
} else {
$colonPointer = $nextPointer;
}
if ($tokens[$colonPointer]['code'] !== T_COLON) {
return null;
}
$typeHintStartPointer = TokenHelper::findNextEffective($phpcsFile, $colonPointer + 1);
$nullable = $tokens[$typeHintStartPointer]['code'] === T_NULLABLE;
$pointerAfterTypeHint = self::isAbstract($phpcsFile, $functionPointer)
? TokenHelper::findNext($phpcsFile, T_SEMICOLON, $typeHintStartPointer + 1)
: $tokens[$functionPointer]['scope_opener'];
$typeHintEndPointer = TokenHelper::findPreviousEffective($phpcsFile, $pointerAfterTypeHint - 1);
$typeHint = TokenHelper::getContent($phpcsFile, $typeHintStartPointer, $typeHintEndPointer);
/** @var string $typeHint */
$typeHint = preg_replace('~\s+~', '', $typeHint);
if (!$nullable) {
$nullable = preg_match('~(?:^|\|)null(?:\||$)~i', $typeHint) === 1;
}
return new TypeHint($typeHint, $nullable, $typeHintStartPointer, $typeHintEndPointer);
}
public static function hasReturnTypeHint(File $phpcsFile, int $functionPointer): bool
{
return self::findReturnTypeHint($phpcsFile, $functionPointer) !== null;
}
/**
* @return ParameterAnnotation[]
*/
public static function getParametersAnnotations(File $phpcsFile, int $functionPointer): array
{
/** @var ParameterAnnotation[] $parametersAnnotations */
$parametersAnnotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $functionPointer, '@param');
return $parametersAnnotations;
}
/**
* @return array<string, ParameterAnnotation|VariableAnnotation>
*/
public static function getValidParametersAnnotations(File $phpcsFile, int $functionPointer): array
{
$tokens = $phpcsFile->getTokens();
$parametersAnnotations = [];
if (self::getName($phpcsFile, $functionPointer) === '__construct') {
for ($i = $tokens[$functionPointer]['parenthesis_opener'] + 1; $i < $tokens[$functionPointer]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE) {
continue;
}
/** @var VariableAnnotation[] $varAnnotations */
$varAnnotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $i, '@var');
if ($varAnnotations === []) {
continue;
}
$parametersAnnotations[$tokens[$i]['content']] = $varAnnotations[0];
}
}
foreach (self::getParametersAnnotations($phpcsFile, $functionPointer) as $parameterAnnotation) {
if ($parameterAnnotation->getContent() === null) {
continue;
}
if ($parameterAnnotation->isInvalid()) {
continue;
}
$parametersAnnotations[$parameterAnnotation->getParameterName()] = $parameterAnnotation;
}
return $parametersAnnotations;
}
/**
* @return array<string, ParameterAnnotation|VariableAnnotation>
*/
public static function getValidPrefixedParametersAnnotations(File $phpcsFile, int $functionPointer): array
{
$tokens = $phpcsFile->getTokens();
$parametersAnnotations = [];
foreach (AnnotationHelper::PREFIXES as $prefix) {
if (self::getName($phpcsFile, $functionPointer) === '__construct') {
for ($i = $tokens[$functionPointer]['parenthesis_opener'] + 1; $i < $tokens[$functionPointer]['parenthesis_closer']; $i++) {
if ($tokens[$i]['code'] !== T_VARIABLE) {
continue;
}
/** @var VariableAnnotation[] $varAnnotations */
$varAnnotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $i, sprintf('@%s-var', $prefix));
if ($varAnnotations === []) {
continue;
}
$parametersAnnotations[$tokens[$i]['content']] = $varAnnotations[0];
}
}
/** @var ParameterAnnotation[] $annotations */
$annotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $functionPointer, sprintf('@%s-param', $prefix));
foreach ($annotations as $parameterAnnotation) {
if ($parameterAnnotation->getContent() === null) {
continue;
}
if ($parameterAnnotation->isInvalid()) {
continue;
}
$parametersAnnotations[$parameterAnnotation->getParameterName()] = $parameterAnnotation;
}
}
return $parametersAnnotations;
}
public static function findReturnAnnotation(File $phpcsFile, int $functionPointer): ?ReturnAnnotation
{
/** @var ReturnAnnotation[] $returnAnnotations */
$returnAnnotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $functionPointer, '@return');
if (count($returnAnnotations) === 0) {
return null;
}
return $returnAnnotations[0];
}
/**
* @return ReturnAnnotation[]
*/
public static function getValidPrefixedReturnAnnotations(File $phpcsFile, int $functionPointer): array
{
$returnAnnotations = [];
foreach (AnnotationHelper::PREFIXES as $prefix) {
/** @var ReturnAnnotation[] $annotations */
$annotations = AnnotationHelper::getAnnotationsByName($phpcsFile, $functionPointer, sprintf('@%s-return', $prefix));
foreach ($annotations as $annotation) {
if (!$annotation->isInvalid()) {
$returnAnnotations[] = $annotation;
break;
}
}
}
return $returnAnnotations;
}
/**
* @return string[]
*/
public static function getAllFunctionNames(File $phpcsFile): array
{
$previousFunctionPointer = 0;
return array_map(
static function (int $functionPointer) use ($phpcsFile): string {
return self::getName($phpcsFile, $functionPointer);
},
array_filter(
iterator_to_array(self::getAllFunctionOrMethodPointers($phpcsFile, $previousFunctionPointer)),
static function (int $functionOrMethodPointer) use ($phpcsFile): bool {
return !self::isMethod($phpcsFile, $functionOrMethodPointer);
}
)
);
}
/**
* @param int $flags optional bitmask of self::LINE_INCLUDE_* constants
*/
public static function getFunctionLengthInLines(File $file, int $functionPosition, int $flags = 0): int
{
if (self::isAbstract($file, $functionPosition)) {
return 0;
}
$includeWhitespace = ($flags & self::LINE_INCLUDE_WHITESPACE) === self::LINE_INCLUDE_WHITESPACE;
$includeComments = ($flags & self::LINE_INCLUDE_COMMENT) === self::LINE_INCLUDE_COMMENT;
$tokens = $file->getTokens();
$token = $tokens[$functionPosition];
$tokenOpenerPosition = $token['scope_opener'];
$tokenCloserPosition = $token['scope_closer'];
$tokenOpenerLine = $tokens[$tokenOpenerPosition]['line'];
$tokenCloserLine = $tokens[$tokenCloserPosition]['line'];
$lineCount = 0;
$lastCommentLine = null;
$previousIncludedPosition = null;
for ($position = $tokenOpenerPosition; $position <= $tokenCloserPosition - 1; $position++) {
$token = $tokens[$position];
if ($includeComments === false) {
if (in_array($token['code'], Tokens::$commentTokens, true)) {
if (
$previousIncludedPosition !== null &&
substr_count($token['content'], $file->eolChar) > 0 &&
$token['line'] === $tokens[$previousIncludedPosition]['line']
) {
// Comment with linebreak starting on same line as included Token
$lineCount++;
}
// Don't include comment
$lastCommentLine = $token['line'];
continue;
}
if (
$previousIncludedPosition !== null &&
$token['code'] === T_WHITESPACE &&
$token['line'] === $lastCommentLine &&
$token['line'] !== $tokens[$previousIncludedPosition]['line']
) {
// Whitespace after block comment... still on comment line...
// Ignore along with the comment
continue;
}
}
if ($token['code'] === T_WHITESPACE) {
$nextNonWhitespacePosition = $file->findNext(T_WHITESPACE, $position + 1, $tokenCloserPosition + 1, true);
if (
$includeWhitespace === false &&
$token['column'] === 1 &&
$nextNonWhitespacePosition !== false &&
$tokens[$nextNonWhitespacePosition]['line'] !== $token['line']
) {
// This line is nothing but whitepace
$position = $nextNonWhitespacePosition - 1;
continue;
}
if ($previousIncludedPosition === $tokenOpenerPosition && $token['line'] === $tokenOpenerLine) {
// Don't linclude line break after opening "{"
// Unless there was code or an (included) comment following the "{"
continue;
}
}
if ($token['code'] !== T_WHITESPACE) {
$previousIncludedPosition = $position;
}
$newLineFoundCount = substr_count($token['content'], $file->eolChar);
$lineCount += $newLineFoundCount;
}
if ($tokens[$previousIncludedPosition]['line'] === $tokenCloserLine) {
// There is code or comment on the closing "}" line...
$lineCount++;
}
return $lineCount;
}
/**
* @return Generator<int>
*/
private static function getAllFunctionOrMethodPointers(File $phpcsFile, int &$previousFunctionPointer): Generator
{
do {
$nextFunctionPointer = TokenHelper::findNext($phpcsFile, T_FUNCTION, $previousFunctionPointer + 1);
if ($nextFunctionPointer === null) {
break;
}
$previousFunctionPointer = $nextFunctionPointer;
yield $nextFunctionPointer;
} while (true);
}
}