diff --git a/src/Row.php b/src/Row.php index 1c2fe77..d276a17 100644 --- a/src/Row.php +++ b/src/Row.php @@ -447,11 +447,11 @@ class Row { $rowNum = $this->idx + 1; $hasTokens = isset($this->parent->syntax->tokens); - $hasRow = array_key_exists($rowNum, $this->parent->syntax->tokens); + $hasRow = $hasTokens && array_key_exists($rowNum, $this->parent->syntax->tokens); if ( ! ( - isset($this->parent->syntax->tokens) && - array_key_exists($rowNum, $this->parent->syntax->tokens) && + $hasTokens && + $hasRow && $this->idx < $this->parent->numRows )) { diff --git a/src/Tokens/PHP.php b/src/Tokens/PHP.php index ecbddec..a1a682e 100644 --- a/src/Tokens/PHP.php +++ b/src/Tokens/PHP.php @@ -11,7 +11,7 @@ class PHP { private array $rawLines; - private array $tokens = []; + private array $tokens; private int $lineNum = 1; @@ -23,6 +23,7 @@ class PHP { $this->code = $code; $this->rawLines = $lines; + $this->tokens = array_fill(1, count($lines) - 1, []); } /** @@ -71,16 +72,6 @@ class PHP { } } - // Add "missing" row indexes - $lineCount = count($this->rawLines); - for ($i = 1; $i <= $lineCount; $i++) - { - if ( ! array_key_exists($i, $this->tokens)) - { - $this->tokens[$i] = []; - } - } - ksort($this->tokens); return $this->tokens; @@ -91,6 +82,11 @@ class PHP { [$type, $rawChar, $currentLine] = $token; $char = tabs_to_spaces($rawChar); + if ($currentLine !== $this->lineNum) + { + $this->lineNum = $currentLine; + } + $current = [ 'type' => $type, 'typeName' => token_name($type), @@ -98,9 +94,10 @@ class PHP { 'line' => $currentLine, ]; - if ($char === "\n") + // Single new line, or starts with a new line with other whitespace + if (strpos($char, "\n") === 0 && trim($char) === '') { - $this->tokens[$this->lineNum] = $current; + $this->tokens[$this->lineNum][] = $current; $this->lineNum++; if ( ! array_key_exists($this->lineNum, $this->tokens)) @@ -139,11 +136,6 @@ class PHP { } } - if ($currentLine !== $this->lineNum) - { - $this->lineNum = $currentLine; - } - $this->tokens[$this->lineNum][] = $current; } diff --git a/tests/Tokens/PHPTest.php b/tests/Tokens/PHPTest.php index 4ea3703..4fa4443 100644 --- a/tests/Tokens/PHPTest.php +++ b/tests/Tokens/PHPTest.php @@ -22,7 +22,7 @@ class PHPTest extends TestCase { { if (empty($lineTokens)) { - $this->assertNotEmpty(trim($lines[$index]), 'Token is empty for non-empty line'); + $this->assertEmpty(trim($lines[$index]), 'Token is empty for non-empty line'); } foreach ($lineTokens as $token)