1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewHeredocInitializeSniff. |
4
|
|
|
* |
5
|
|
|
* PHP version 5.3 |
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\Sniffs\PHP\NewConstantScalarExpressionsSniff; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* \PHPCompatibility\Sniffs\PHP\NewHeredocInitializeSniff. |
18
|
|
|
* |
19
|
|
|
* As of PHP 5.3.0, it's possible to initialize static variables, class properties |
20
|
|
|
* and constants declared using the `const` keyword, using the Heredoc syntax. |
21
|
|
|
* And while not documented, heredoc initialization can now also be used for function param defaults. |
22
|
|
|
* See: https://3v4l.org/JVH8W |
23
|
|
|
* |
24
|
|
|
* These heredocs (still) cannot contain variables. That's, however, outside the scope of the |
25
|
|
|
* PHPCompatibility library until such time as there is a PHP version in which this would be accepted. |
26
|
|
|
* |
27
|
|
|
* PHP version 5.3 |
28
|
|
|
* |
29
|
|
|
* @category PHP |
30
|
|
|
* @package PHPCompatibility |
31
|
|
|
* @author Juliette Reinders Folmer <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class NewHeredocInitializeSniff extends NewConstantScalarExpressionsSniff |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Error message. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
const ERROR_PHRASE = 'Initializing %s using the Heredoc syntax was not supported in PHP 5.2 or earlier'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Partial error phrases to be used in combination with the error message constant. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $errorPhrases = array( |
49
|
|
|
'const' => 'constants', |
50
|
|
|
'property' => 'class properties', |
51
|
|
|
'staticvar' => 'static variables', |
52
|
|
|
'default' => 'default parameter values', |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Do a version check to determine if this sniff needs to run at all. |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
protected function bowOutEarly() |
62
|
|
|
{ |
63
|
|
|
return ($this->supportsBelow('5.2') !== true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Is a value declared and does the declared value not contain an heredoc ? |
69
|
|
|
* |
70
|
|
|
* @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. |
71
|
|
|
* @param int $stackPtr The position of the current token in the |
72
|
|
|
* stack passed in $tokens. |
73
|
|
|
* @param int $end The end of the value definition. |
74
|
|
|
* |
75
|
|
|
* @return bool True if no heredoc (or assignment) is found, false otherwise. |
76
|
|
|
*/ |
77
|
|
|
protected function isValidAssignment(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $end) |
78
|
|
|
{ |
79
|
|
|
$tokens = $phpcsFile->getTokens(); |
80
|
|
|
$next = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), $end, true); |
81
|
|
|
if ($next === false || $tokens[$next]['code'] !== T_EQUAL) { |
82
|
|
|
// No value assigned. |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return ($phpcsFile->findNext(T_START_HEREDOC, ($next + 1), $end, false, null, true) === false); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|