Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Find all classes/functions in code
Beating my head against a wall with this one. Given a block of text that represents a hunk of PHP I’d like to retrieve all the class names and function names in that string such that I know which functions belong to which class(es) and which functions are globally defined.
Ideal output would probably be a multi-dimensional array such as:
$fns = array(
'class1' => array('luke', 'obi-wan', 'r2d2'),
'class2' => array('c3po', 'chewie', 'han'),
...
'' => array('darth', 'death_star', 'emperor'),
)
where that last entry signifies the ‘classless’ (a.k.a. global) functions. But as long as the information is iteratable I don’t mind what format it is in.
Using things like get_declared_classes()
and get_defined_functions()
would be great if the code had been executed but it’s not, it’s just a dumb string. Though if it was possible to eval()
it and somehow get the function names that way it’d be better than nothing (I’d probably have to grab before and after snapshots of the declared classes/functions and array_diff() them to find which ones were defined in the code block).
I’ve tried various regexes such as:
$classFinder = '/class[\s\n]+(\w+)[\s\n]*\{?(function[\s\n]+(\w+)[\s\n]*\(.*\)[\s\n]*\{?)*/';
$functionFinder = '/function[\s\n]+(\w+)[\s\n]*\(.*\)[\s\n]*\{?/';
but nothing has yet retrieved reliable information.
The functionFinder regex works 99% of the time for finding all functions in a file but that also includes all functions inside classes and I can’t find a way to differentiate them, hence I was trying a pair of regexes. It occasionally trips up on function names in comments like // call function strtr() here
which is why it’s only 99% accurate, but I can live with that.
The only other avenue I considered was a parser, perhaps using token_get_all()
, but that’s probably a last resort as I expect it’d be time/compute expensive.
Any ideas or code fragments that might get me on the right road greatly appreciated.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
Re: Find all classes/functions in code
You might look into Reflection.
Offline
Re: Find all classes/functions in code
wet wrote:
You might look into Reflection.
Thanks, but I’m actually trying to build something that builds on reflection, if you get what I mean.
If the code in question was already in TXP’s scope (i.e. loaded / eval()d / whatever) then I could probably query its API with reflection, but as it’s just a hunk of code in a textarea (think ied_plugin_composer) I’m having a hard time figuring out how to get all the function names.
If I can pull this off, it’ll be quite a cool feature of the composer but it’s in danger of becoming “never-see-the-light-of-day-ware” right now so I might just drop that part of it unless someone can dangle the carrot of hope.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online
#4 2011-03-29 18:19:22
- Smartys
- New Member
- Registered: 2011-03-25
- Posts: 2
Re: Find all classes/functions in code
So you have a block of text which is valid PHP code and you want to extract the class/function names from it? Using the tokenizer is the way to go. You can certainly call eval and compare the defined functions/classes before and after, but PHP is tokenizing the content internally anyway.
Offline
Re: Find all classes/functions in code
Smartys wrote:
So you have a block of text which is valid PHP code and you want to extract the class/function names from it? Using the tokenizer is the way to go.
Yup. I feared as much. Just thought there might be a cool regex shortcut, or something obvious I’d overlooked.
In which case I’d best dredge my memory banks for all those times I played with Lex/Yacc so I can apply them to this situation. And then figure out how to make it as efficient as possible.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Online