Skip to content

Commit e5a1bd4

Browse files
committed
Support php-ast version 45 properly.
1 parent 1f45a8c commit e5a1bd4

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PHP-Parser to php-ast
66
This converts ASTs(Abstract Syntax Trees) from [PHP-Parser](https://github.com/nikic/PHP-Parser) to [php-ast](https://github.com/nikic/php-ast/).
77
It can be used as a PHP-only implementation of php-ast.
88

9-
Supported [php-ast AST versions](https://github.com/nikic/php-ast#version-changelog): 40, 50
9+
Supported [php-ast AST versions](https://github.com/nikic/php-ast#version-changelog): 40, 45, 50
1010

1111
Current Status
1212
--------------
@@ -32,12 +32,13 @@ Using it as a slow substitute for php-ast
3232

3333
- [tests/ASTConverter/ConversionTest.php](https://github.com/TysonAndre/php-parser-to-php-ast/blob/master/tests/ASTConverter/ConversionTest.php)
3434

35-
Using it as an error-tolerant substitute for php-ast (e.g. for use in IDEs)
35+
Using it as an error-tolerant substitute for php-ast: (e.g. for use in IDEs)
3636

3737
- There are currently two modes: omitting errors and adding placeholders (e.g. `__INCOMPLETE_VARIABLE__`).
3838
- Omitting errors only handles some common cases that come up while editing a file.
3939
- Placeholders may change in the future.
4040
- [tests/ASTConverter/ErrorTolerantConversionTest.php](https://github.com/TysonAndre/php-parser-to-php-ast/blob/master/tests/ASTConverter/ErrorTolerantConversionTest.php)
41+
- If performance is an issue, cache previous results for a file's contents (in combination with the version used to generate the file `ASTConversion::ASTCONVERTER_VERSION`)
4142

4243
Running unit tests
4344
------------------

src/ASTConverter/ASTConverter.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@
4949
* SOFTWARE.
5050
*/
5151
final class ASTConverter {
52+
// The release version of this class.
53+
const ASTCONVERTER_VERSION = '0.1.0';
54+
5255
// The latest stable version of php-ast.
5356
// For something > 50, update the library's release.
5457
// For something < 40, there are no releases.
5558
const AST_VERSION = 50;
5659
// The versions that this supports
57-
const SUPPORTED_AST_VERSIONS = [40, 50];
60+
const SUPPORTED_AST_VERSIONS = [40, 45, 50];
5861

5962
/**
6063
* @var int - A version in SUPPORTED_AST_VERSIONS
@@ -1893,13 +1896,13 @@ private static function phpParserDeclareListToAstDeclares(array $declares, int $
18931896
];
18941897
$doc_comment = self::extractPhpdocComment($declare->getAttribute('comments')) ?? $first_doc_comment;
18951898
$first_doc_comment = null;
1896-
if (self::$ast_version >= 50) {
1899+
if (self::$ast_version >= 45) {
18971900
if (PHP_VERSION_ID >= 70100) {
18981901
$children['docComment'] = $doc_comment;
18991902
}
19001903
}
19011904
$node = new ast\Node(ast\AST_CONST_ELEM, 0, $children, $declare->getAttribute('startLine'));
1902-
if (self::$ast_version < 50 && is_string($doc_comment)) {
1905+
if (self::$ast_version < 45 && is_string($doc_comment)) {
19031906
if (PHP_VERSION_ID >= 70100) {
19041907
$node->docComment = $doc_comment;
19051908
}
@@ -2070,7 +2073,7 @@ private static function phpParserNameToString(PhpParser\Node\Name $name) : strin
20702073
* @return ast\Node
20712074
*/
20722075
private static function newAstNode(int $kind, int $flags, array $children, int $lineno, string $doc_comment = null) : ast\Node {
2073-
if (self::$ast_version >= 45) {
2076+
if (self::$ast_version >= 50) {
20742077
if (is_string($doc_comment) || array_key_exists($kind, self::_NODES_WITH_NULL_DOC_COMMENT)) {
20752078
if ($kind !== \ast\AST_CONST_ELEM || PHP_VERSION_ID >= 70100) {
20762079
$children['docComment'] = $doc_comment;
@@ -2096,15 +2099,15 @@ private static function newAstNode(int $kind, int $flags, array $children, int $
20962099
* @suppress PhanUndeclaredProperty
20972100
*/
20982101
private static function newAstDecl(int $kind, int $flags, array $children, int $lineno, string $doc_comment = null, string $name = null, int $end_lineno = 0, int $decl_id = -1) : ast\Node {
2099-
if (self::$ast_version >= 45) {
2100-
$children45 = [];
2101-
$children45['name'] = $name;
2102-
$children45['docComment'] = $doc_comment;
2103-
$children45 += $children;
2102+
if (self::$ast_version >= 50) {
2103+
$children50 = [];
2104+
$children50['name'] = $name;
2105+
$children50['docComment'] = $doc_comment;
2106+
$children50 += $children;
21042107
if ($decl_id >= 0 && self::$ast_version >= 50) {
2105-
$children45['__declId'] = $decl_id;
2108+
$children50['__declId'] = $decl_id;
21062109
}
2107-
$node = new ast\Node($kind, $flags, $children45, $lineno);
2110+
$node = new ast\Node($kind, $flags, $children50, $lineno);
21082111
if (is_int($end_lineno)) {
21092112
$node->endLineno = $end_lineno;
21102113
}

tests/ASTConverter/ConversionTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ public function astValidFileExampleProvider() {
4747
$paths = $this->_scanSourceDirForPHP($source_dir);
4848
sort($paths);
4949
$supports40 = self::hasNativeASTSupport(40);
50+
$supports45 = self::hasNativeASTSupport(45);
5051
$supports50 = self::hasNativeASTSupport(50);
5152
if (!($supports40 || $supports50)) {
52-
throw new RuntimeException("Neither AST version 40 nor 50 are natively supported");
53+
throw new RuntimeException("None of version 40, 45 or 50 are natively supported");
5354
}
5455
foreach ($paths as $path) {
5556
if ($supports40) {
5657
$tests[] = [$path, 40];
5758
}
59+
if ($supports45) {
60+
$tests[] = [$path, 45];
61+
}
5862
if ($supports50) {
5963
$tests[] = [$path, 50];
6064
}

0 commit comments

Comments
 (0)