Skip to content

Commit 78d841f

Browse files
committed
Fixup, fix bug in converting trait adaptations
1 parent 16f726c commit 78d841f

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ PHP-Parser to php-ast
33

44
[![Build Status](https://travis-ci.org/TysonAndre/php-parser-to-php-ast.svg?branch=master)](https://travis-ci.org/TysonAndre/php-parser-to-php-ast)
55

6-
8 out of 41 tests are failing, and only due to namespace support and line numbers being slightly different in php-ast.
6+
No tests are failing
77

8+
- This is 90% done
89
- The test suite is not yet comprehensive.
10+
- Need to normalize namespaces.
911

1012
[Current Issues](https://github.com/TysonAndre/php-parser-to-php-ast/issues/)
1113

12-
- The test suite this is based off of is not comprehensive, and edge cases still remain.
14+
- The test suite this is based off of covers common cases for Phan, but edge cases still remain.
1315
See https://github.com/TysonAndre/php-parser-to-php-ast/issues/4
1416

1517
Usage

src/ASTConverter/ASTConverter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,9 +846,10 @@ private static function _init_handle_map() : array {
846846
);
847847
},
848848
'PhpParser\Node\Stmt\TraitUseAdaptation\Alias' => function(PhpParser\Node\Stmt\TraitUseAdaptation\Alias $n, int $startLine) : \ast\Node {
849-
$old_class = $n->trait !== null ? self::_phpparser_name_to_string($n->trait) : null;
849+
$old_class = $n->trait !== null ? self::_phpparser_node_to_ast_node($n->trait) : null;
850+
$flags = ($n->trait instanceof PhpParser\Node\Name\FullyQualified) ? \ast\flags\NAME_FQ : \ast\flags\NAME_NOT_FQ;
850851
// TODO: flags for visibility
851-
return astnode(\ast\AST_TRAIT_ALIAS, self::_phpparser_visibility_to_ast_visibility($n->newModifier ?? 0), [
852+
return astnode(\ast\AST_TRAIT_ALIAS, self::_phpparser_visibility_to_ast_visibility($n->newModifier ?? 0, false), [
852853
'method' => astnode(\ast\AST_METHOD_REFERENCE, 0, [
853854
'class' => $old_class,
854855
'method' => $n->method,
@@ -1523,7 +1524,7 @@ private static function _phpparser_constelem_to_ast_constelem(PhpParser\Node\Con
15231524
return astnode(\ast\AST_CONST_ELEM, 0, $children, $startLine, self::_extract_phpdoc_comment($n->getAttribute('comments') ?? $docComment));
15241525
}
15251526

1526-
private static function _phpparser_visibility_to_ast_visibility(int $visibility) : int {
1527+
private static function _phpparser_visibility_to_ast_visibility(int $visibility, bool $automatically_add_public = true) : int {
15271528
$ast_visibility = 0;
15281529
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_PUBLIC) {
15291530
$ast_visibility |= \ast\flags\MODIFIER_PUBLIC;
@@ -1532,7 +1533,9 @@ private static function _phpparser_visibility_to_ast_visibility(int $visibility)
15321533
} else if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_PRIVATE) {
15331534
$ast_visibility |= \ast\flags\MODIFIER_PRIVATE;
15341535
} else {
1535-
$ast_visibility |= \ast\flags\MODIFIER_PUBLIC;
1536+
if ($automatically_add_public) {
1537+
$ast_visibility |= \ast\flags\MODIFIER_PUBLIC;
1538+
}
15361539
}
15371540
if ($visibility & \PHPParser\Node\Stmt\Class_::MODIFIER_STATIC) {
15381541
$ast_visibility |= \ast\flags\MODIFIER_STATIC;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// Phan should detect and catch some types of misuse of trait adaptations(`insteadof`/`as`)
3+
4+
trait Trait295 {
5+
public function baz() { }
6+
public function xyz() { }
7+
}
8+
trait Trait295B {
9+
}
10+
11+
class A295 {
12+
use Trait295 {
13+
Trait295::foo as bar;
14+
Trait295B::xyz insteadof Trait295;
15+
Trait295C::zz as zzAlias;
16+
}
17+
}
18+
19+
function test295() {
20+
$x = new A295();
21+
$x->baz();
22+
$x->bar();
23+
$x->foo();
24+
}

0 commit comments

Comments
 (0)