Skip to content
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

[question] How to list all classes that used in a file? #232

Open
erlangparasu opened this issue Sep 9, 2022 · 2 comments
Open

[question] How to list all classes that used in a file? #232

erlangparasu opened this issue Sep 9, 2022 · 2 comments

Comments

@erlangparasu
Copy link

erlangparasu commented Sep 9, 2022

For example:

class MyKlass {
    public function createBook()
    {
        $book = new Book();

        return $book;
    }

    public function formatter()
    {
        $formatter = Utils::getFormatter();

        return $formatter;
    }
}

How to print list of class usage of that file?
In this example expected output will be: "Book" and "Utils" class.
Thanks

@erlangparasu
Copy link
Author

erlangparasu commented Sep 9, 2022

Currently i found:
Stmt_Class, Expr_StaticCall, Name_FullyQualified and Expr_New
to able to find class usage.

Any other possible keyword to check? Thanks

@TysonAndre
Copy link
Collaborator

There's a lot of them - there's also AST_USE/AST_GROUP_USE which would tell you what use Xyz\Foo; meant Foo was.

You may want to use a static analyzer instead, e.g. https://github.com/phan/phan

For example, https://github.com/phan/phan/blob/5.2.1/tool/pdep#L20-L54 would print the full dependency graph of classes


Currently i found:
Stmt_Class, Expr_StaticCall, Name_FullyQualified and Expr_New
to able to find class usage.

Are you thinking of https://github.com/nikic/php-parser and https://github.com/nikic/PHP-Parser/tree/master/lib/PhpParser/Node/Stmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants