-<?php
+<?php declare(strict_types=1);
namespace PhpParser;
use PhpParser\Parser\Tokens;
+use PHPUnit\Framework\TestCase;
-class LexerTest extends \PHPUnit_Framework_TestCase
+class LexerTest extends TestCase
{
/* To allow overwriting in parent class */
- protected function getLexer(array $options = array()) {
+ protected function getLexer(array $options = []) {
return new Lexer($options);
}
$lexer->startLexing($code, $errorHandler);
$errors = $errorHandler->getErrors();
- $this->assertSame(count($messages), count($errors));
+ $this->assertCount(count($messages), $errors);
for ($i = 0; $i < count($messages); $i++) {
$this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
}
}
public function provideTestError() {
- return array(
- array("<?php /*", array("Unterminated comment from 1:7 to 1:9")),
- array("<?php \1", array("Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7")),
- array("<?php \0", array("Unexpected null byte from 1:7 to 1:7")),
+ return [
+ ["<?php /*", ["Unterminated comment from 1:7 to 1:9"]],
+ ["<?php \1", ["Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7"]],
+ ["<?php \0", ["Unexpected null byte from 1:7 to 1:7"]],
// Error with potentially emulated token
- array("<?php ?? \0", array("Unexpected null byte from 1:10 to 1:10")),
- array("<?php\n\0\1 foo /* bar", array(
+ ["<?php ?? \0", ["Unexpected null byte from 1:10 to 1:10"]],
+ ["<?php\n\0\1 foo /* bar", [
"Unexpected null byte from 2:1 to 2:1",
"Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2",
"Unterminated comment from 2:8 to 2:14"
- )),
- );
+ ]],
+ ];
}
/**
}
public function provideTestLex() {
- return array(
+ return [
// tests conversion of closing PHP tag and drop of whitespace and opening tags
- array(
+ [
'<?php tokens ?>plaintext',
- array(),
- array(
- array(
+ [],
+ [
+ [
Tokens::T_STRING, 'tokens',
- array('startLine' => 1), array('endLine' => 1)
- ),
- array(
+ ['startLine' => 1], ['endLine' => 1]
+ ],
+ [
ord(';'), '?>',
- array('startLine' => 1), array('endLine' => 1)
- ),
- array(
+ ['startLine' => 1], ['endLine' => 1]
+ ],
+ [
Tokens::T_INLINE_HTML, 'plaintext',
- array('startLine' => 1, 'hasLeadingNewline' => false),
- array('endLine' => 1)
- ),
- )
- ),
+ ['startLine' => 1, 'hasLeadingNewline' => false],
+ ['endLine' => 1]
+ ],
+ ]
+ ],
// tests line numbers
- array(
+ [
'<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
- array(),
- array(
- array(
+ [],
+ [
+ [
ord('$'), '$',
- array('startLine' => 2), array('endLine' => 2)
- ),
- array(
+ ['startLine' => 2], ['endLine' => 2]
+ ],
+ [
Tokens::T_STRING, 'token',
- array('startLine' => 2), array('endLine' => 2)
- ),
- array(
+ ['startLine' => 2], ['endLine' => 2]
+ ],
+ [
ord('$'), '$',
- array(
+ [
'startLine' => 3,
- 'comments' => array(
- new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
- )
- ),
- array('endLine' => 3)
- ),
- )
- ),
+ 'comments' => [
+ new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14, 5),
+ ]
+ ],
+ ['endLine' => 3]
+ ],
+ ]
+ ],
// tests comment extraction
- array(
+ [
'<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
- array(),
- array(
- array(
+ [],
+ [
+ [
Tokens::T_STRING, 'token',
- array(
+ [
'startLine' => 2,
- 'comments' => array(
- new Comment('/* comment */', 1, 6),
- new Comment('// comment' . "\n", 1, 20),
- new Comment\Doc('/** docComment 1 */', 2, 31),
- new Comment\Doc('/** docComment 2 */', 2, 50),
- ),
- ),
- array('endLine' => 2)
- ),
- )
- ),
+ 'comments' => [
+ new Comment('/* comment */', 1, 6, 1),
+ new Comment('// comment' . "\n", 1, 20, 3),
+ new Comment\Doc('/** docComment 1 */', 2, 31, 4),
+ new Comment\Doc('/** docComment 2 */', 2, 50, 5),
+ ],
+ ],
+ ['endLine' => 2]
+ ],
+ ]
+ ],
// tests differing start and end line
- array(
+ [
'<?php "foo' . "\n" . 'bar"',
- array(),
- array(
- array(
+ [],
+ [
+ [
Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
- array('startLine' => 1), array('endLine' => 2)
- ),
- )
- ),
+ ['startLine' => 1], ['endLine' => 2]
+ ],
+ ]
+ ],
// tests exact file offsets
- array(
+ [
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
- array('usedAttributes' => array('startFilePos', 'endFilePos')),
- array(
- array(
+ ['usedAttributes' => ['startFilePos', 'endFilePos']],
+ [
+ [
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
- array('startFilePos' => 6), array('endFilePos' => 8)
- ),
- array(
+ ['startFilePos' => 6], ['endFilePos' => 8]
+ ],
+ [
ord(';'), ';',
- array('startFilePos' => 9), array('endFilePos' => 9)
- ),
- array(
+ ['startFilePos' => 9], ['endFilePos' => 9]
+ ],
+ [
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
- array('startFilePos' => 18), array('endFilePos' => 20)
- ),
- array(
+ ['startFilePos' => 18], ['endFilePos' => 20]
+ ],
+ [
ord(';'), ';',
- array('startFilePos' => 21), array('endFilePos' => 21)
- ),
- )
- ),
+ ['startFilePos' => 21], ['endFilePos' => 21]
+ ],
+ ]
+ ],
// tests token offsets
- array(
+ [
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
- array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
- array(
- array(
+ ['usedAttributes' => ['startTokenPos', 'endTokenPos']],
+ [
+ [
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
- array('startTokenPos' => 1), array('endTokenPos' => 1)
- ),
- array(
+ ['startTokenPos' => 1], ['endTokenPos' => 1]
+ ],
+ [
ord(';'), ';',
- array('startTokenPos' => 2), array('endTokenPos' => 2)
- ),
- array(
+ ['startTokenPos' => 2], ['endTokenPos' => 2]
+ ],
+ [
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
- array('startTokenPos' => 5), array('endTokenPos' => 5)
- ),
- array(
+ ['startTokenPos' => 5], ['endTokenPos' => 5]
+ ],
+ [
ord(';'), ';',
- array('startTokenPos' => 6), array('endTokenPos' => 6)
- ),
- )
- ),
+ ['startTokenPos' => 6], ['endTokenPos' => 6]
+ ],
+ ]
+ ],
// tests all attributes being disabled
- array(
+ [
'<?php /* foo */ $bar;',
- array('usedAttributes' => array()),
- array(
- array(
+ ['usedAttributes' => []],
+ [
+ [
Tokens::T_VARIABLE, '$bar',
- array(), array()
- ),
- array(
+ [], []
+ ],
+ [
ord(';'), ';',
- array(), array()
- )
- )
- ),
+ [], []
+ ]
+ ]
+ ],
// tests no tokens
- array(
+ [
'',
- array(),
- array()
- ),
- );
+ [],
+ []
+ ],
+ ];
}
/**
}
public function provideTestHaltCompiler() {
- return array(
- array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
- array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
- array('<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'),
+ return [
+ ['<?php ... __halt_compiler();Remaining Text', 'Remaining Text'],
+ ['<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'],
+ ['<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'],
//array('<?php ... __halt_compiler();' . "\0", "\0"),
//array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
- );
+ ];
}
/**
public function testGetTokens() {
$code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";';
- $expectedTokens = array(
- array(T_OPEN_TAG, '<?php ', 1),
- array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1),
+ $expectedTokens = [
+ [T_OPEN_TAG, '<?php ', 1],
+ [T_CONSTANT_ENCAPSED_STRING, '"a"', 1],
';',
- array(T_WHITESPACE, "\n", 1),
- array(T_COMMENT, '// foo' . "\n", 2),
- array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
+ [T_WHITESPACE, "\n", 1],
+ [T_COMMENT, '// foo' . "\n", 2],
+ [T_CONSTANT_ENCAPSED_STRING, '"b"', 3],
';',
- );
+ ];
$lexer = $this->getLexer();
$lexer->startLexing($code);