From 0a7e384a3376a4d44135454249a1945de73ff3e3 Mon Sep 17 00:00:00 2001 From: Chuck Burgess Date: Fri, 10 May 2013 17:39:44 -0500 Subject: [PATCH 1/2] handle the codeCoverageIgnore checks before handling the comment token; don't ignore a comment line if there appears to be other code in front of it on the same line; --- PHP/CodeCoverage/Util.php | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/PHP/CodeCoverage/Util.php b/PHP/CodeCoverage/Util.php index 26a284135..5a4673ccd 100644 --- a/PHP/CodeCoverage/Util.php +++ b/PHP/CodeCoverage/Util.php @@ -114,25 +114,9 @@ public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE) switch (get_class($token)) { case 'PHP_Token_COMMENT': case 'PHP_Token_DOC_COMMENT': { - $count = substr_count($token, "\n"); - $line = $token->getLine(); - - for ($i = $line; $i < $line + $count; $i++) { - self::$ignoredLines[$filename][$i] = TRUE; - } - - if ($token instanceof PHP_Token_DOC_COMMENT) { - // Workaround for the fact the DOC_COMMENT token - // does not include the final \n character in its - // text. - if (substr(trim($lines[$i-1]), -2) == '*/') { - self::$ignoredLines[$filename][$i] = TRUE; - } - - break; - } $_token = trim($token); + $_line = trim($lines[$token->getLine() - 1]); if ($_token == '// @codeCoverageIgnore' || $_token == '//@codeCoverageIgnore') { @@ -149,6 +133,26 @@ public static function getLinesToBeIgnored($filename, $cacheTokens = TRUE) $_token == '//@codeCoverageIgnoreEnd') { $stop = TRUE; } + + // be sure the comment doesn't have some token BEFORE it on the same line... + // it would not be safe to ignore the whole line in those cases. + if (0 === strpos($_token, $_line)) { + $count = substr_count($token, "\n"); + $line = $token->getLine(); + + for ($i = $line; $i < $line + $count; $i++) { + self::$ignoredLines[$filename][$i] = TRUE; + } + + if ($token instanceof PHP_Token_DOC_COMMENT) { + // Workaround for the fact the DOC_COMMENT token + // does not include the final \n character in its + // text. + if (substr(trim($lines[$i-1]), -2) == '*/') { + self::$ignoredLines[$filename][$i] = TRUE; + } + } + } } break; From 7025c3e5ff5f3cc3d832b501055bccf7d59b0d33 Mon Sep 17 00:00:00 2001 From: Chuck Burgess Date: Fri, 10 May 2013 17:40:46 -0500 Subject: [PATCH 2/2] tests for #158; --- Tests/PHP/CodeCoverage/UtilTest.php | 16 ++++++++++++-- .../source_with_oneline_annotations.php | 22 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Tests/PHP/CodeCoverage/UtilTest.php b/Tests/PHP/CodeCoverage/UtilTest.php index ed6b0f7cb..50641f7c5 100644 --- a/Tests/PHP/CodeCoverage/UtilTest.php +++ b/Tests/PHP/CodeCoverage/UtilTest.php @@ -164,17 +164,29 @@ public function testGetLinesToBeIgnoredOneLineAnnotations() array( 1 => TRUE, 2 => TRUE, - 7 => TRUE, 3 => TRUE, 4 => TRUE, 5 => TRUE, 6 => TRUE, + 7 => TRUE, 8 => TRUE, 9 => TRUE, 10 => TRUE, 11 => TRUE, 12 => TRUE, - 13 => TRUE + 13 => TRUE, + 14 => TRUE, + 17 => TRUE, + 19 => TRUE, + 22 => TRUE, + 23 => TRUE, + 27 => TRUE, + 28 => TRUE, + 29 => TRUE, + 30 => TRUE, + 31 => TRUE, + 32 => TRUE, + 33 => TRUE, ), PHP_CodeCoverage_Util::getLinesToBeIgnored( TEST_FILES_PATH . 'source_with_oneline_annotations.php' diff --git a/Tests/_files/source_with_oneline_annotations.php b/Tests/_files/source_with_oneline_annotations.php index 358df9697..327122e5a 100644 --- a/Tests/_files/source_with_oneline_annotations.php +++ b/Tests/_files/source_with_oneline_annotations.php @@ -11,3 +11,25 @@ public function bar() { } } + +function baz() +{ + // a one-line comment + print '*'; // a one-line comment + + /* a one-line comment */ + print '*'; /* a one-line comment */ + + /* a one-line comment + */ + print '*'; /* a one-line comment + */ + + print '*'; // @codeCoverageIgnore + + print '*'; // @codeCoverageIgnoreStart + print '*'; + print '*'; // @codeCoverageIgnoreEnd + + print '*'; +}