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
|
|
|
T_SL, |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
if (defined('T_START_NOWDOC')) { |
34
|
|
|
$targets[] = constant('T_START_NOWDOC'); |
35
|
|
|
} |
36
|
|
|
if (defined('T_END_NOWDOC')) { |
37
|
|
|
$targets[] = constant('T_END_NOWDOC'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $targets; |
41
|
|
|
|
42
|
|
|
}//end register() |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Processes this test, when one of its tokens is encountered. |
47
|
|
|
* |
48
|
|
|
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
49
|
|
|
* @param int $stackPtr The position of the current token in |
50
|
|
|
* the stack passed in $tokens. |
51
|
|
|
* |
52
|
|
|
* @return int|void On older PHP versions passes a pointer to the nowdoc closer |
53
|
|
|
* to skip passed anything in between in regards to processing |
54
|
|
|
* the file for this sniff. |
55
|
|
|
*/ |
56
|
|
|
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
57
|
|
|
{ |
58
|
|
|
if ($this->supportsBelow('5.2') === false) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$tokens = $phpcsFile->getTokens(); |
63
|
|
|
$tokenType = $tokens[$stackPtr]['type']; |
64
|
|
|
$isOldPhp = (PHP_VERSION_ID < 50300); |
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 ($isOldPhp === true && $tokenType === 'T_SL' ) { |
71
|
|
View Code Duplication |
if (isset($tokens[($stackPtr+1)]) === false || $tokens[($stackPtr+1)]['type'] !== 'T_LESS_THAN') { |
|
|
|
|
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if (isset($tokens[($stackPtr+2)]) === false || $tokens[($stackPtr+2)]['type'] !== 'T_CONSTANT_ENCAPSED_STRING') { |
|
|
|
|
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (preg_match('`^\'([a-z][a-z0-9_]*)\'$`iD', $tokens[($stackPtr+2)]['content'], $matches ) < 1) { |
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$closer = $phpcsFile->findNext(T_STRING, ($stackPtr + 3), null, false, $matches[1], true); |
84
|
|
|
if ($closer === false) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$phpcsFile->addError( 'Nowdocs are not present in PHP version 5.2 or earlier.', $stackPtr, 'Found' ); |
90
|
|
|
|
91
|
|
|
if (isset($closer) !== false) { |
92
|
|
|
$phpcsFile->addError( 'Nowdocs are not present in PHP version 5.2 or earlier.', $closer, 'Found' ); |
|
|
|
|
93
|
|
|
return ($closer + 1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
}//end process() |
97
|
|
|
|
98
|
|
|
}//end class |
99
|
|
|
|
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.