PHP Reflecting code from a file
Moderator: General Moderators
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
PHP Reflecting code from a file
Hi,
I've been trying to find information on how to use reflection on a file that doesn't necessarily contain class definitions or OO code. I'd like to be able to read any file (not with include obviously because you don't want the code to execute) but be able to read the different functions, variables, classes that are defined in it and use that reflection class to analyse the content of the file.
There is an extension proposition for the zend framework but i can't see if there is a way to download it and i'd prefer a native php extension or class set cause i hate using Zend Framework, don't ask me why, i just don't like it... Is there an extension that can do that? The reflection core API for PHP is good but only works on information in memory, not on information that is not loaded...
The reasons for this are large, i'd like to create some kind of code analysis tool but i also see a good way of securing pluggins for applications with this. Load the reflection from the file, analyse the code, are there free instructions floating in the code that will execute when i include this file, is the code in this file defining a class? Is this class extending or implementing a specific other class or interface?
Thanks
I've been trying to find information on how to use reflection on a file that doesn't necessarily contain class definitions or OO code. I'd like to be able to read any file (not with include obviously because you don't want the code to execute) but be able to read the different functions, variables, classes that are defined in it and use that reflection class to analyse the content of the file.
There is an extension proposition for the zend framework but i can't see if there is a way to download it and i'd prefer a native php extension or class set cause i hate using Zend Framework, don't ask me why, i just don't like it... Is there an extension that can do that? The reflection core API for PHP is good but only works on information in memory, not on information that is not loaded...
The reasons for this are large, i'd like to create some kind of code analysis tool but i also see a good way of securing pluggins for applications with this. Load the reflection from the file, analyse the code, are there free instructions floating in the code that will execute when i include this file, is the code in this file defining a class? Is this class extending or implementing a specific other class or interface?
Thanks
Re: PHP Reflecting code from a file
http://us2.php.net/runkit, http://us2.php.net/parsekit, http://us2.php.net/tokenizer
tokenizer provides lower level api to the php source - on top of which some analysis tools like PHP_CodeSniffer have been built
tokenizer provides lower level api to the php source - on top of which some analysis tools like PHP_CodeSniffer have been built
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: PHP Reflecting code from a file
Nice, thanks Dan, hope that'll solve my problem 
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: PHP Reflecting code from a file
Hey dan, do you know of any class files that already exist that read the tokenizer output to generate a real DOM of the inspected file? If not, i think i'll try my hand at this. It could be a very nice addition to the reflection api. Use the tokenizer to read PHP file and get all the information from it. The tokenizer itself outputs a <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> of data that is not coordinated which is what it's meant to do anyway, tokenize the script, but analysing that becomes quite complex i think.
So, #1 do you know of a library of classes that can analyse the tokenizer output giving out a DOM
#2 if not, do you think it would be interresting that someone created that
I think i can achieve it but obviously, i don't want to reinvent the wheel.
CrazyOne
So, #1 do you know of a library of classes that can analyse the tokenizer output giving out a DOM
#2 if not, do you think it would be interresting that someone created that
I think i can achieve it but obviously, i don't want to reinvent the wheel.
CrazyOne
Re: PHP Reflecting code from a file
It seems you're looking for AST (Abstract Syntax Tree) generator for php. While there's none I'm familiar with personally there seems to be quite a few projects having some AST implementations. One of those is PHP_Depend's: https://github.com/pdepend/pdepend/tree ... epend/Code - seems to be a comprehensive attempt at least.
Re: PHP Reflecting code from a file
I've just found another project from pdepend's author: https://github.com/manuelpichler/static ... design.txt
Sounds suspiciously like something you would be interested in
Sounds suspiciously like something you would be interested in
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: PHP Reflecting code from a file
Thanks again, i'll look into those, man, seriously, how did you find those on google? I just don't use the right search term i guess, but please instruct me!
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: PHP Reflecting code from a file
I've been looking over the examples you sent but they are using their own AST but don't provide a framework or library i can use and even less documentation. I have two options, try to understand how they use their own AST and hijack it or make my own and package it as an open-source library dedicated to that purpose.
Re: PHP Reflecting code from a file
Well, I know about tokenizer functions because that's what PHP_CodeSniffer is based on, and we use phpcs in the project I'm working on. You mentioned DOM, and dom is one of representations of xml, and that reminded me of AST (the term I knew from my previous research on this topic) because xml is often used to store parsed syntax trees (and to use various xml tools on them).crazycoders wrote:Thanks again, i'll look into those, man, seriously, how did you find those on google? I just don't use the right search term i guess, but please instruct me!
php_depend's source was seventh link on my search using keywords 'php ast', and most results above that were for some C++ framework.
Then I just looked at other projects by the same author on github.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: PHP Reflecting code from a file
Give a man a fish, he can eat for a day. Teach a man how to google, he can look up how to fish. 