Skip to content

Implement object-based token_get_all() alternative #5176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
46 changes: 46 additions & 0 deletions ext/tokenizer/tests/PhpToken_constructor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
PhpToken constructor
--SKIPIF--
<?php if (!extension_loaded("tokenizer")) print "skip tokenizer extension not enabled"; ?>
--FILE--
<?php

$token = new PhpToken(300, 'function');
var_dump($token);
$token = new PhpToken(300, 'function', 10);
var_dump($token);
$token = new PhpToken(300, 'function', 10, 100);
var_dump($token);

?>
--EXPECT--
object(PhpToken)#1 (4) {
["id"]=>
int(300)
["text"]=>
string(8) "function"
["line"]=>
int(-1)
["pos"]=>
int(-1)
}
object(PhpToken)#2 (4) {
["id"]=>
int(300)
["text"]=>
string(8) "function"
["line"]=>
int(10)
["pos"]=>
int(-1)
}
object(PhpToken)#1 (4) {
["id"]=>
int(300)
["text"]=>
string(8) "function"
["line"]=>
int(10)
["pos"]=>
int(100)
}
36 changes: 36 additions & 0 deletions ext/tokenizer/tests/PhpToken_extension.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
Extending the PhpToken class
--SKIPIF--
<?php if (!extension_loaded("tokenizer")) print "skip tokenizer extension not enabled"; ?>
--FILE--
<?php

$code = <<<'PHP'
<?PHP
FUNCTION FOO() {
ECHO "bar";
}
PHP;

class MyPhpToken extends PhpToken {
public int $extra = 123;

public function getLoweredText(): string {
return strtolower($this->text);
}
}

foreach (MyPhpToken::getAll($code) as $token) {
echo $token->getLoweredText();

if ($token->extra !== 123) {
echo "Missing property!\n";
}
}

?>
--EXPECT--
<?php
function foo() {
echo "bar";
}
30 changes: 30 additions & 0 deletions ext/tokenizer/tests/PhpToken_extension_errors.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
PhpToken extensions that throw during construction
--SKIPIF--
<?php if (!extension_loaded("tokenizer")) print "skip tokenizer extension not enabled"; ?>
--FILE--
<?php

class MyPhpToken1 extends PhpToken {
public $extra = UNKNOWN;
}

try {
var_dump(MyPhpToken1::getAll("<?php foo"));
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

abstract class MyPhpToken2 extends PhpToken {
}

try {
var_dump(MyPhpToken2::getAll("<?php foo"));
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

?>
--EXPECT--
Undefined constant 'UNKNOWN'
Cannot instantiate abstract class MyPhpToken2
15 changes: 15 additions & 0 deletions ext/tokenizer/tests/PhpToken_final_constructor.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Check that the PhpToken constructor is final
--SKIPIF--
<?php if (!extension_loaded("tokenizer")) print "skip tokenizer extension not enabled"; ?>
--FILE--
<?php

class MyPhpToken extends PhpToken {
public function __construct() {
}
}

?>
--EXPECTF--
Fatal error: Cannot override final method PhpToken::__construct() in %s on line %d
Loading