1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCompatibility_Sniffs_PHP_NewNowdocSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.3 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package PHPCompatibility |
9
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PHPCompatibility_Sniffs_PHP_NewNowdocSniff. |
14
|
|
|
* |
15
|
|
|
* @category PHP |
16
|
|
|
* @package PHPCompatibility |
17
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PHPCompatibility_Sniffs_PHP_NewNowdocSniff extends PHPCompatibility_Sniff |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns an array of tokens this test wants to listen for. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function register() |
28
|
|
|
{ |
29
|
|
|
$targets = array(); |
30
|
|
|
|
31
|
|
|
if (version_compare(PHP_VERSION, '5.3', '<') === true) { |
32
|
|
|
$targets[] = T_SL; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if (defined('T_START_NOWDOC')) { |
36
|
|
|
$targets[] = constant('T_START_NOWDOC'); |
37
|
|
|
} |
38
|
|
|
if (defined('T_END_NOWDOC')) { |
39
|
|
|
$targets[] = constant('T_END_NOWDOC'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $targets; |
43
|
|
|
|
44
|
|
|
}//end register() |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Processes this test, when one of its tokens is encountered. |
49
|
|
|
* |
50
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
51
|
|
|
* @param int $stackPtr The position of the current token in |
52
|
|
|
* the stack passed in $tokens. |
53
|
|
|
* |
54
|
|
|
* @return int|void On older PHP versions passes a pointer to the nowdoc closer |
55
|
|
|
* to skip passed anything in between in regards to processing |
56
|
|
|
* the file for this sniff. |
57
|
|
|
*/ |
58
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
59
|
|
|
{ |
60
|
|
|
if ($this->supportsBelow('5.2') === false) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$tokens = $phpcsFile->getTokens(); |
65
|
|
|
|
66
|
|
|
/* |
67
|
|
|
* In PHP 5.2 the T_NOWDOC tokens aren't recognized yet and PHPCS does not |
68
|
|
|
* backfill for it, so we have to sniff for a specific combination of tokens. |
69
|
|
|
*/ |
70
|
|
|
if ($tokens[$stackPtr]['code'] === T_SL) { |
71
|
|
View Code Duplication |
if (isset($tokens[($stackPtr+1)]) === false || $tokens[($stackPtr + 1)]['code'] !== T_LESS_THAN) { |
|
|
|
|
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if (isset($tokens[($stackPtr+2)]) === false |
|
|
|
|
76
|
|
|
|| $tokens[($stackPtr + 2)]['code'] !== T_CONSTANT_ENCAPSED_STRING) { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* |
81
|
|
|
* Heredoc and nowdoc naming rules: |
82
|
|
|
* "it must contain only alphanumeric characters and underscores and |
83
|
|
|
* must start with a non-digit character or underscore" |
84
|
|
|
* @link http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc |
85
|
|
|
*/ |
86
|
|
|
if (preg_match('`^\'([a-z][a-z0-9_]*)\'$`iD', $tokens[($stackPtr + 2)]['content'], $matches) < 1) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$closer = null; |
91
|
|
|
|
92
|
|
|
for ($i = ($stackPtr + 3); $i < $phpcsFile->numTokens; $i++) { |
93
|
|
|
$maybeCloser = $phpcsFile->findNext(T_STRING, $i, null, false, $matches[1]); |
94
|
|
|
if ($maybeCloser === false) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// The closing identifier must begin in the first column of the line. |
99
|
|
|
if ($tokens[$maybeCloser]['column'] !== 1) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// The closing identifier must be the only content on that line, except for maybe a semi-colon. |
104
|
|
|
$next = $phpcsFile->findNext(array(T_WHITESPACE, T_SEMICOLON), ($maybeCloser + 1), null, true); |
105
|
|
|
if ($tokens[$maybeCloser]['line'] === $tokens[$next]['line']) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$closer = $maybeCloser; |
110
|
|
|
break; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (isset($closer) === false) { |
114
|
|
|
// No valid closer found. |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$phpcsFile->addError('Nowdocs are not present in PHP version 5.2 or earlier.', $stackPtr, 'Found'); |
120
|
|
|
|
121
|
|
|
if (isset($closer) !== false) { |
122
|
|
|
$phpcsFile->addError('Nowdocs are not present in PHP version 5.2 or earlier.', $closer, 'Found'); |
|
|
|
|
123
|
|
|
return ($closer + 1); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
}//end process() |
127
|
|
|
|
128
|
|
|
}//end class |
129
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.