|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewGeneratorReturnSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.0 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHPCompatibility |
|
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace PHPCompatibility\Sniffs\PHP; |
|
13
|
|
|
|
|
14
|
|
|
use PHPCompatibility\Sniff; |
|
15
|
|
|
use PHPCompatibility\PHPCSHelper; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewGeneratorReturnSniff. |
|
19
|
|
|
* |
|
20
|
|
|
* As of PHP 7.0, a return statement can be used within a generator for a final expression to be returned. |
|
21
|
|
|
* |
|
22
|
|
|
* PHP version 7.0 |
|
23
|
|
|
* |
|
24
|
|
|
* @category PHP |
|
25
|
|
|
* @package PHPCompatibility |
|
26
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class NewGeneratorReturnSniff extends Sniff |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns an array of tokens this test wants to listen for. |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function register() |
|
37
|
|
|
{ |
|
38
|
|
|
$targets = array(); |
|
39
|
|
|
|
|
40
|
|
|
/* |
|
41
|
|
|
* The `yield` keyword was introduced in PHP 5.5 with the token T_YIELD. |
|
42
|
|
|
* The `yield from` keyword was introduced in PHP 7.0 and tokenizes as |
|
43
|
|
|
* "T_YIELD T_WHITESPACE T_STRING". |
|
44
|
|
|
* |
|
45
|
|
|
* Pre-PHPCS 3.1.0, the T_YIELD token was not back-filled for PHP < 5.5. |
|
46
|
|
|
* Also, as of PHPCS 3.1.0, the PHPCS tokenizer adds a new T_YIELD_FROM |
|
47
|
|
|
* token. |
|
48
|
|
|
* |
|
49
|
|
|
* So for PHP 5.3-5.4 icw PHPCS < 3.1.0, we need to look for T_STRING with content "yield". |
|
50
|
|
|
* For PHP 5.5+ we need to look for T_YIELD. |
|
51
|
|
|
* For PHPCS 3.1.0+, we also need to look for T_YIELD_FROM. |
|
52
|
|
|
*/ |
|
53
|
|
|
if (version_compare(PHP_VERSION_ID, '50500', '<') === true |
|
54
|
|
|
&& version_compare(PHPCSHelper::getVersion(), '3.1.0', '<') === true |
|
55
|
|
|
) { |
|
56
|
|
|
$targets[] = T_STRING; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (defined('T_YIELD')) { |
|
60
|
|
|
// phpcs:ignore PHPCompatibility.PHP.NewConstants.t_yieldFound |
|
61
|
|
|
$targets[] = T_YIELD; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (defined('T_YIELD_FROM')) { |
|
65
|
|
|
$targets[] = T_YIELD_FROM; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $targets; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Processes this test, when one of its tokens is encountered. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
75
|
|
|
* @param int $stackPtr The position of the current token in the |
|
76
|
|
|
* stack passed in $tokens. |
|
77
|
|
|
* |
|
78
|
|
|
* @return void|int Void or a stack pointer to skip forward. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->supportsBelow('5.6') !== true) { |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$tokens = $phpcsFile->getTokens(); |
|
87
|
|
|
|
|
88
|
|
|
if ($tokens[$stackPtr]['code'] === T_STRING |
|
89
|
|
|
&& $tokens[$stackPtr]['content'] !== 'yield' |
|
90
|
|
|
) { |
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$function = $phpcsFile->getCondition($stackPtr, T_FUNCTION); |
|
95
|
|
|
if ($function === false) { |
|
96
|
|
|
// Try again, but now for a closure. |
|
97
|
|
|
$function = $phpcsFile->getCondition($stackPtr, T_CLOSURE); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($function === false) { |
|
101
|
|
|
// Yield outside function scope, fatal error, but not our concern. |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
if (isset($tokens[$function]['scope_opener'], $tokens[$function]['scope_closer']) === false) { |
|
106
|
|
|
// Can't reliably determine start/end of function scope. |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$hasReturn = $phpcsFile->findNext(T_RETURN, ($tokens[$function]['scope_opener'] + 1), $tokens[$function]['scope_closer']); |
|
111
|
|
|
if ($hasReturn === false) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$phpcsFile->addError( |
|
116
|
|
|
'Returning a final expression from a generator was not supported in PHP 5.6 or earlier', |
|
117
|
|
|
$hasReturn, |
|
118
|
|
|
'ReturnFound' |
|
119
|
|
|
); |
|
120
|
|
|
|
|
121
|
|
|
// Don't examine this function again. |
|
122
|
|
|
return $tokens[$function]['scope_closer']; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|