Edson Medina
Edson Medina

Reputation: 10269

How to find scope level with token_get_all()?

I'm using token_get_all() to do some static analysis on a php project.

How do I find the scope level for stuff like:

Should I keep a count of '{' and '}' characters? Is that a safe method?

Upvotes: 2

Views: 989

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173562

The token_get_all() is exactly what the package says: it's a tokenizer.

This means that you have to keep track of scope yourself, by keeping track of opening and closing braces; the simplest way I can think of is to build a recursive descent parser.

Btw, parse errors would already be handled by this function.

Upvotes: 2

Lucas Green
Lucas Green

Reputation: 3959

You might do better to use a parser to build a syntax tree, otherwise the edge cases will become very complex (PHP is a very tricky language that can have other languages embedded in it).

A quick google came up with: PHP-Parser

With a syntax tree you could more easily identify the scope of a function or variable, but it would still require some analysis.

Upvotes: 1

Related Questions