Skip to content

Commit 4f7f6d6

Browse files
authored
Merge pull request #105 from stronk7/better_detect_variables_in_strings
Discard non-variables within double quoted strings
2 parents 42ab278 + 807358d commit 4f7f6d6

4 files changed

+18
-6
lines changed

moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ protected function processVariable(File $phpcsfile, $stackptr) {
107107
protected function processVariableInString(File $phpcsfile, $stackptr) {
108108
$tokens = $phpcsfile->getTokens();
109109

110-
if (preg_match('/\$([A-Za-z0-9_]+)(\-\>([A-Za-z0-9_]+))?/i',
110+
if (preg_match_all('/[^\\\\]\$([A-Za-z0-9_]+)(\-\>([A-Za-z0-9_]+))?/i',
111111
$tokens[$stackptr]['content'], $matches)) {
112-
$firstvar = $matches[1];
112+
$captured = $matches[1];
113113

114-
$this->validate_moodle_variable_name($firstvar, $phpcsfile, $stackptr);
114+
foreach ($captured as $varname) {
115+
$this->validate_moodle_variable_name($varname, $phpcsfile, $stackptr);
116+
}
115117
}
116118
}
117119

moodle/Tests/MoodleStandardTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ public function test_moodle_namingconventions_variablename() {
708708
16 => 0,
709709
17 => 'The \'var\' keyword is not permitted',
710710
20 => 'must be all lower-case',
711-
21 => 'must not contain underscores',
711+
21 => 2,
712712
22 => array('must be all lower-case', 'must not contain underscores'),
713713
));
714714
$this->set_warnings(array());

moodle/Tests/fixtures/moodle_namingconventions_variablename.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class foo {
1818
}
1919

2020
$result = "String: $badPlaceholder1 is a badPlaceholder1";
21-
$result = "String: $bad_placeholder2";
21+
$result = "String: $bad_placeholder1 and $bad_placeholder2";
2222
$result = "String: $REALLY_badplaceholder3";
2323
$result = "String: $goodplaceholder1";
24+
$result = "String: $goodplaceholder1 and $goodplaceholder2";
25+
$result = "String: \$THISISNOTAVARIABLE $strangeone";
26+
$result = "String: $strangeone \$THISISNOTAVARIABLE";
27+
$result = "String: \$THISISNOTAVARIABLE $buthisis \$NEITHERTHISIS $butthisistoo";
28+
$result = 'String: $THISISNOTAVARIABLE $NEITHERTHISIS';

moodle/Tests/fixtures/moodle_namingconventions_variablename.php.fixed

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ class foo {
1818
}
1919

2020
$result = "String: $badplaceholder1 is a badPlaceholder1";
21-
$result = "String: $badplaceholder2";
21+
$result = "String: $badplaceholder1 and $badplaceholder2";
2222
$result = "String: $reallybadplaceholder3";
2323
$result = "String: $goodplaceholder1";
24+
$result = "String: $goodplaceholder1 and $goodplaceholder2";
25+
$result = "String: \$THISISNOTAVARIABLE $strangeone";
26+
$result = "String: $strangeone \$THISISNOTAVARIABLE";
27+
$result = "String: \$THISISNOTAVARIABLE $buthisis \$NEITHERTHISIS $butthisistoo";
28+
$result = 'String: $THISISNOTAVARIABLE $NEITHERTHISIS';

0 commit comments

Comments
 (0)