Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update parser to recognize newlines when PHP does not provide a line

For instance the content '}' has no PHP token, so if it is the only thing on a line it'll inherit the line of it's parent line which would be incorrect.
  • Loading branch information...
commit d7d8da304ade3d0b7ce8e469edb7ec3c22e26075 1 parent 8a8fe88
@blainesch blainesch authored
Showing with 25 additions and 4 deletions.
  1. +4 −0 analysis/Parser.php
  2. +21 −4 tests/cases/analysis/ParserTest.php
View
4 analysis/Parser.php
@@ -74,6 +74,10 @@ public static function tokenize($code, array $options = array()) {
}
}
$tokens[] = array('id' => $id, 'name' => $name, 'content' => $content, 'line' => $line);
+
+ if ($id === T_WHITESPACE) {
+ $line += count(preg_split('/\r\n|\r|\n/', $content)) - 1;
+ }
}
if ($options['wrap'] && empty($options['include'])) {
View
25 tests/cases/analysis/ParserTest.php
@@ -56,7 +56,12 @@ public function testFullTokenization() {
$result = Parser::tokenize('$foo = function() {};');
$this->assertEqual(11, count($result));
- $expected = array('id' => 309, 'name' => 'T_VARIABLE', 'content' => '$foo', 'line' => 1);
+ $expected = array(
+ 'id' => T_VARIABLE,
+ 'name' => 'T_VARIABLE',
+ 'content' => '$foo',
+ 'line' => 1
+ );
$this->assertEqual($expected, $result[0]);
$expected = array('id' => null, 'name' => ';', 'content' => ';', 'line' => 1);
@@ -99,9 +104,9 @@ public function testFilteredTokenization() {
$result = Parser::tokenize($code, array('include' => array('T_IF', 'T_WHILE', 'T_CATCH')));
$expected = array(
- array('id' => 318, 'name' => 'T_WHILE', 'content' => 'while', 'line' => 1),
- array('id' => 301, 'name' => 'T_IF', 'content' => 'if', 'line' => 1),
- array('id' => 338, 'name' => 'T_CATCH', 'content' => 'catch', 'line' => 3)
+ array('id' => T_WHILE, 'name' => 'T_WHILE', 'content' => 'while', 'line' => 1),
+ array('id' => T_IF, 'name' => 'T_IF', 'content' => 'if', 'line' => 1),
+ array('id' => T_CATCH, 'name' => 'T_CATCH', 'content' => 'catch', 'line' => 3)
);
$this->assertEqual($expected, $result);
}
@@ -136,6 +141,18 @@ function ($i) { return join('', $i); },
$expected = array('ClassName', 'method');
$this->assertEqual($expected, $results);
}
+
+ public function testParserGuessesLineBleed() {
+ $code = <<<EOD
+if (false) {
+ return true;
+}
+EOD;
+ $tokens = Parser::tokenize($code);
+ $this->assertIdentical('}', $tokens[13]['content']);
+ $this->assertIdentical(3, $tokens[13]['line']);
+ }
+
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.