grom358 / pharborist

A PHP library to query and transform source code via tree operations.
GNU General Public License v3.0
44 stars 10 forks source link

Common interface for FunctionDeclarationNode and ClassMethodNode #180

Open pako-pl opened 10 years ago

pako-pl commented 10 years ago

FunctionDeclarationNode and ClassMethodNode both use FunctionTrait. It would be nice to add a common interface to these classes, so it would be possible to use it in type hints.

grom358 commented 10 years ago

Do you have a use case in mind? Also ideas for name of this interface?

Functions and methods are similar in many ways, so in many ways it does make sense. With a method like a function except it belongs to a class and has visibility and static keyword.

At the moment I have mostly just made interfaces to group nodes together as I have needed them and that fit the grammar. And as it is there a lot of classes/interfaces/traits.

phenaproxima commented 10 years ago

How about CallableNodeInterface? I think being able to type hint callable things would be quite handy. Plus, we could explicitly define common parameter and argument handling methods in it, for organization's sake.

pako-pl commented 10 years ago

Would CallableNodeInterface also include anonymous functions? They don't have a name, so they are a bit different from functions and methods.

What about MethodOrFunctionNodeInterface? A bit longer, but would avoid confusion. And then CallableNodeInterface could also include anonymous functions.

Having these interfaces would allow to group together code that needs to work for of functions and methods. For example, we have an analyzer that checks the number of arguments in a function/method call.

pako-pl commented 10 years ago

It would also be useful to have a common interface for ClassMethodNodes and InterfaceMethodNodes. Otherwise it's impossible to add type hints to parameters of methods that accept either type of a node.

Also, ClassNode, InterfaceNode and TraitNode are used interchangeably in some cases and share similar properties.

grom358 commented 10 years ago

Agreed.. Just trying think of good names for these. Suggestions welcomed.

Well ClassNode and TraitNode already share a common base class, SingleInheritanceNode

pako-pl commented 10 years ago

What about ClassOrInterfaceMethodNode and ClassInterfaceOrTraitNode?