PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Introducing PHPParser_Node_Stmt_Block

Open schmittjoh opened this issue 12 years ago • 7 comments

I'd like to get rid of the statement arrays and instead introduce a PHPParser_Node_Stmt_Block.

arrays might be a bit faster, but they make the code operating on the AST unnecessary complex by requiring constant type checking and two different APIs, e.g. traverse(Node $node) and traverseArray(array $nodes).

To reduce the BC break, we could implement ArrayAccess, and Countable on that node class.

What do you think?

schmittjoh avatar Apr 05 '12 23:04 schmittjoh

The need for traversing arrays stays in any case, as arrays are used for a few more things than just statements. Simple example is the FuncCall node, which has an array of ->args. Another example is Echo which accepts several ->exprs. Etc.

But I do agree that adding a dedicated node for statements could be reasonable. It would allow to add a helper methods for handling statements. One such method for example could be ->getByType('Stmt_Method'). Do have ideas what else could be useful?

nikic avatar Apr 07 '12 22:04 nikic

I need it for example to get the next/previous/parent relation correct, see #17.

However, I have discovered some other areas where I might want different nodes (names for example which are sometimes PHP_Node_Name, sometimes strings). So, I'm not sure how much we should change the AST in your library, or if the better approach is to just run a visitor over the AST which normalizes it into a format that I need in my code. Your current AST is better for performance at least.

schmittjoh avatar Apr 07 '12 22:04 schmittjoh

Ah, okay, I have misunderstood your point at first, now I see it. I'll see whether I can add a node for statements without breaking too much stuff.

nikic avatar Apr 07 '12 22:04 nikic

By the way, the reason that some names are Node_Name and others are just strings is that some names can be namespaced and others can't. For example when calling a function the function name can be namespaced, so it's a Node_Name. But when calling a method, the method name cannot be namespaced, so it's just a string.

nikic avatar Apr 07 '12 22:04 nikic

By the way, the reason that some names are Node_Name and others are just strings is that some names can be namespaced and others can't. For example when calling a function the function name can be namespaced, so it's a Node_Name. But when calling a method, the method name cannot be namespaced, so it's just a string.

A string also has no offset information, which can be important in some cases. For example, when a user does go-to-definition for a method, I need to know whether he clicked on the method name or the variable the method was called on.

felixfbecker avatar Nov 20 '16 23:11 felixfbecker

@felixfbecker Can you please open a separate issue for this?

nikic avatar Nov 21 '16 16:11 nikic

+1 for this, node navigation is very clumsy right now, ended up using modified relations from #17.

tostercx avatar Mar 11 '17 17:03 tostercx