Skip to content

Commit

Permalink
Discard non-variables within double quoted strings
Browse files Browse the repository at this point in the history
This change fixes a couple of things:

- Stop detecting some literals (escaped $) as variable names.
- Process multiple variables in the same string. Previously only
  the first variable found was being checked.

Fixes moodlehq#10
  • Loading branch information
stronk7 committed Feb 14, 2024
1 parent 42ab278 commit 807358d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
8 changes: 5 additions & 3 deletions moodle/Sniffs/NamingConventions/ValidVariableNameSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ protected function processVariable(File $phpcsfile, $stackptr) {
protected function processVariableInString(File $phpcsfile, $stackptr) {
$tokens = $phpcsfile->getTokens();

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

$this->validate_moodle_variable_name($firstvar, $phpcsfile, $stackptr);
foreach ($captured as $varname) {
$this->validate_moodle_variable_name($varname, $phpcsfile, $stackptr);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion moodle/Tests/MoodleStandardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ public function test_moodle_namingconventions_variablename() {
16 => 0,
17 => 'The \'var\' keyword is not permitted',
20 => 'must be all lower-case',
21 => 'must not contain underscores',
21 => 2,
22 => array('must be all lower-case', 'must not contain underscores'),
));
$this->set_warnings(array());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class foo {
}

$result = "String: $badPlaceholder1 is a badPlaceholder1";
$result = "String: $bad_placeholder2";
$result = "String: $bad_placeholder1 and $bad_placeholder2";
$result = "String: $REALLY_badplaceholder3";
$result = "String: $goodplaceholder1";
$result = "String: $goodplaceholder1 and $goodplaceholder2";
$result = "String: \$THISISNOTAVARIABLE $strangeone";
$result = "String: $strangeone \$THISISNOTAVARIABLE";
$result = "String: \$THISISNOTAVARIABLE $buthisis \$NEITHERTHISIS $butthisistoo";
$result = 'String: $THISISNOTAVARIABLE $NEITHERTHISIS';
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class foo {
}

$result = "String: $badplaceholder1 is a badPlaceholder1";
$result = "String: $badplaceholder2";
$result = "String: $badplaceholder1 and $badplaceholder2";
$result = "String: $reallybadplaceholder3";
$result = "String: $goodplaceholder1";
$result = "String: $goodplaceholder1 and $goodplaceholder2";
$result = "String: \$THISISNOTAVARIABLE $strangeone";
$result = "String: $strangeone \$THISISNOTAVARIABLE";
$result = "String: \$THISISNOTAVARIABLE $buthisis \$NEITHERTHISIS $butthisistoo";
$result = 'String: $THISISNOTAVARIABLE $NEITHERTHISIS';

0 comments on commit 807358d

Please sign in to comment.