Skip to content

Commit 3f0ddc0

Browse files
committed
Now recognises assign-by-heredoc in static variable declarations.
1 parent f483ddc commit 3f0ddc0

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

README.mkdn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ KNOWN ISSUES & BUGS
2929
-------------------
3030

3131
* File scope isn't currently analysed.
32-
* Heredoc/nowdoc in static variable assign-while-declaring may confuse things.

Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -960,24 +960,27 @@ protected function checkForStaticDeclaration(
960960
// can contain assignments. The assignment is compile-time however so can
961961
// only be constant values, which makes life manageable.
962962
// Valid values are:
963-
// number T_MINUS T_LNUMBER T_DNUMBER
964-
// string T_CONSTANT_ENCAPSED_STRING
965-
// define T_STRING
966-
// class constant T_STRING, T_DOUBLE_COLON, T_STRING
967-
// TODO: assignment can be via heredoc, just to confuse matters
963+
// number T_MINUS T_LNUMBER T_DNUMBER
964+
// string T_CONSTANT_ENCAPSED_STRING
965+
// heredoc T_START_HEREDOC T_HEREDOC T_END_HEREDOC
966+
// define T_STRING
967+
// class constant T_STRING T_DOUBLE_COLON T_STRING
968968
// Search backwards for first token that isn't whitespace, comma, variable,
969969
// equals, or on the list of assignable constant values above.
970970
$staticPtr = $phpcsFile->findPrevious(
971-
array(T_WHITESPACE, T_VARIABLE, T_COMMA, T_EQUAL,
972-
T_MINUS, T_LNUMBER, T_DNUMBER,
973-
T_CONSTANT_ENCAPSED_STRING,
974-
T_STRING,
975-
T_DOUBLE_COLON),
971+
array(
972+
T_WHITESPACE, T_VARIABLE, T_COMMA, T_EQUAL,
973+
T_MINUS, T_LNUMBER, T_DNUMBER,
974+
T_CONSTANT_ENCAPSED_STRING,
975+
T_STRING,
976+
T_DOUBLE_COLON,
977+
T_START_HEREDOC, T_HEREDOC, T_END_HEREDOC,
978+
),
976979
$stackPtr - 1, null, true, null, true);
977-
//if ($varName == 'static2') {
978-
//echo "Failing token:\n" . print_r($tokens[$staticPtr], true);
979-
//}
980980
if (($staticPtr === false) || ($tokens[$staticPtr]['code'] !== T_STATIC)) {
981+
//if ($varName == 'static4') {
982+
// echo "Failing token:\n" . print_r($tokens[$staticPtr], true);
983+
//}
981984
return false;
982985
}
983986

Tests/CodeAnalysis/VariableAnalysisUnitTest.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,22 @@ function &function_with_return_by_reference_and_param($param) {
220220

221221
function function_with_static_var() {
222222
static $static1, $static_num = 12, $static_neg_num = -1.5, $static_string = 'abc', $static_string2 = "def", $static_define = MYDEFINE, $static_constant = MyClass::CONSTANT, $static2;
223+
static $static_heredoc = <<<END_OF_HEREDOC
224+
this is an ugly but valid way to continue after a heredoc
225+
END_OF_HEREDOC
226+
, $static3;
227+
static $static_nowdoc = <<<'END_OF_NOWDOC'
228+
this is an ugly but valid way to continue after a nowdoc
229+
END_OF_NOWDOC
230+
, $static4;
223231
echo $static1;
224232
echo $static_num;
225233
echo $static2;
226234
echo $var;
235+
echo $static_heredoc;
236+
echo $static3;
237+
echo $static_nowdoc;
238+
echo $static4;
227239
}
228240

229241
function function_with_pass_by_reference_param(&$param) {

Tests/CodeAnalysis/VariableAnalysisUnitTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ private function _getWarningAndErrorList() {
154154
($base += 5) => 0,
155155
($base + 1) => 1, // 5, // $static_neg_num, $static_string, $static_string2,
156156
// $static_define, $static_constant
157-
($base + 5) => 1, // $var
158-
// function_with_pass_by_reference_param() line (+8)
157+
($base + 13) => 1, // $var
158+
// function_with_pass_by_reference_param() line (+20)
159159
// no warnings.
160-
($base += 8) => 0,
160+
($base += 20) => 0,
161161
// function_with_pass_by_reference_calls() line (+4)
162162
($base += 4) => 0,
163163
($base + 1) => 1, // $matches

0 commit comments

Comments
 (0)