|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify |
|
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ZenifyCodingStandard\Helper\Commenting; |
|
11
|
|
|
|
|
12
|
|
|
use PHP_CodeSniffer_File; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Inspired by https://github.com/slevomat/coding-standard/blob/4f81f58625bf86bd91f7fc6f8e4d12160bf03c7c/SlevomatCodingStandard/Helpers/FunctionHelper.php |
|
17
|
|
|
*/ |
|
18
|
|
|
final class FunctionHelper |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return string|NULL |
|
23
|
|
|
*/ |
|
24
|
2 |
|
public static function findReturnTypeHint(PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer) |
|
25
|
|
|
{ |
|
26
|
2 |
|
$tokens = $codeSnifferFile->getTokens(); |
|
27
|
2 |
|
$isAbstract = self::isAbstract($codeSnifferFile, $functionPointer); |
|
28
|
2 |
|
$colonToken = $isAbstract |
|
29
|
2 |
|
? $codeSnifferFile->findNext( |
|
30
|
2 |
|
T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, NULL, FALSE, NULL, TRUE |
|
31
|
|
|
) |
|
32
|
2 |
|
: $codeSnifferFile->findNext( |
|
33
|
2 |
|
T_COLON, $tokens[$functionPointer]['parenthesis_closer'] + 1, $tokens[$functionPointer]['scope_opener'] - 1 |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
2 |
|
if ($colonToken === FALSE) { |
|
37
|
2 |
|
return NULL; |
|
38
|
|
|
} |
|
39
|
2 |
|
$returnTypeHint = NULL; |
|
40
|
2 |
|
$nextToken = $colonToken; |
|
41
|
|
|
|
|
42
|
|
|
do { |
|
43
|
2 |
|
$nextToken = $isAbstract |
|
44
|
|
|
? $codeSnifferFile->findNext([T_WHITESPACE, T_COMMENT, T_SEMICOLON], $nextToken + 1, NULL, TRUE, NULL, TRUE) |
|
45
|
2 |
|
: $codeSnifferFile->findNext( |
|
46
|
2 |
|
[T_WHITESPACE, T_COMMENT], $nextToken + 1, $tokens[$functionPointer]['scope_opener'] - 1, TRUE |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
2 |
|
$isTypeHint = $nextToken !== FALSE; |
|
50
|
2 |
|
if ($isTypeHint) { |
|
51
|
2 |
|
$returnTypeHint .= $tokens[$nextToken]['content']; |
|
52
|
|
|
} |
|
53
|
2 |
|
} while ($isTypeHint); |
|
54
|
|
|
|
|
55
|
2 |
|
return $returnTypeHint; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
2 |
|
public static function isAbstract(PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): bool |
|
60
|
|
|
{ |
|
61
|
2 |
|
if ( ! isset($codeSnifferFile->getTokens()[$functionPointer]['scope_opener'])) { |
|
62
|
|
|
return TRUE; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
if ($codeSnifferFile->getTokens()[$functionPointer]['scope_opener'] >= 39) { |
|
66
|
2 |
|
return TRUE; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return FALSE; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|