Permalink
Browse files

Fixed bug #19414 : php_cs seems to not track variables correctly in h…

…eredocs. It was important to include the start and end tags of a heredoc so that variables inside it would be tokenized correctly.
  • Loading branch information...
1 parent ecc6693 commit d8f22be5a02aff870471b024f685e23eaaf84297 @gsherwood gsherwood committed May 11, 2012
View
17 CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php
@@ -116,16 +116,23 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
unset($params[$token['content']]);
} else if ($code === T_DOUBLE_QUOTED_STRING
- || $code === T_HEREDOC
- || $code === T_NOWDOC
+ || $code === T_START_HEREDOC
+ || $code === T_START_NOWDOC
) {
// Tokenize strings that can contain variables.
// Make sure the string is re-joined if it occurs over multiple lines.
+ $validTokens = array(
+ T_HEREDOC,
+ T_NOWDOC,
+ T_END_HEREDOC,
+ T_END_NOWDOC,
+ T_DOUBLE_QUOTED_STRING,
+ );
+ $validTokens = array_merge($validTokens, PHP_CodeSniffer_Tokens::$emptyTokens);
+
$content = $token['content'];
for ($i = ($next + 1); $i <= $end; $i++) {
- if ($tokens[$i]['code'] === $code
- || in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === true
- ) {
+ if (in_array($tokens[$i]['code'], $validTokens) === true) {
$content .= $tokens[$i]['content'];
$next++;
} else {
View
12 CodeSniffer/Standards/Generic/Tests/CodeAnalysis/UnusedFunctionParameterUnitTest.inc
@@ -53,4 +53,14 @@ HERE;
8
9 at least 9 lines long
HERE;
-}
+}
+
+function foo( $parameter ) {
+ return <<<HTML
+<?xml version="1.0"?>
+<value>$parameter</value>
+HTML;
+}
+
+print foo( 'PARAMETER' );
+print "\n";
View
1 package.xml
@@ -57,6 +57,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #19394 : Notice in NonExecutableCodeSniff
- Fixed bug #19412 : Incorrect error about assigning objects to variables when inside inline IF
- Fixed bug #19413 : php_cs thinks I haven't used a parameter when I have
+ - Fixed bug #19414 : php_cs seems to not track variables correctly in heredocs
</notes>
<contents>
<dir name="/">

0 comments on commit d8f22be

Please sign in to comment.