Comments (8)
@r3verser I don't know either it will be enough for you or not, but you can see one example of handling code with PHPParser here. PhpTemplateEngine is template engine that handles .phtml files that contain code in PHPTemplate language and returns processed PHP as output. The PHPTemplate language is just PHP with changed semantic of statements/expressions, a capability to call Plugins and process HTML tags.
Here is a description how it works:
- The PHPTemplateEngine::process() method initializes Filters. The PhpTemplateEngine is FilterChain, each Filter in the chain gets input and returns changed output that will be processed as input by other Filter and so on. It works similar to unix pipeline:
echo 'foo' | grep 'o'.
So after init() we have the following Filters in the chain:
PhpTemplateEngine\Parser(),
PhpTemplateEngine\CodeGenerator()
- The PhpTemplateEngine\Parser as you can see only parses code with \PHPParser_Parser and \PHPParser_Lexer, the result is AST and it will be processed by the next Filter - PhpTemplateEngine\CodeGenerator.
- The PhpTemplateEngine\CodeGenerator gets AST as input (that was output from PhpTemplateEngine\Parser) and uses \PHPParser_NodeTraverser() to traverse it. To do that it adds the PhpTemplateEngine\Preprocessor that makes transformations of AST nodes. After all transformations the CodeGenerator generates changed PHP with:
$prettyPrinter = new \PHPParser_PrettyPrinter_Default();
return $prettyPrinter->prettyPrintFile($ast);
So result of the CodeGenerator::filter() is simple string of PHP code.
- The PhpTemplateEngine\Preprocessor can use any method defined in the \PHPParser_NodeVisitor interface to change the AST nodes, but for now it uses only one: leaveNode(). What it does - it just changes semantic of the echo statement by wrapping its arguments with htmlspecialchars, for example:
echo $s;
// will become
echo htmlspecialchars($s, ENT_QUOTES);
There is other thing what it does - it handles HTML with other HTML Parser - instance of the PhpTemplateEngine\SemiParser that provides a capability for each HTML tag write respective method that will be able to change its content. For example - the PhpTemplateEngine\HtmlParser class uses the tagA() method to change the 'a' tags, specifically the 'href' attribute, so that HTML links will be automatically prepended with base URI.
from php-parser.
Hi, thanks for reply, but unfortunately this is also a very simple example. It does not use a data-flow analysis :( I need to do a code analyzer, using memory / stack / etc, which analyzes the flow of execution and, based on result, producing some action. I'm not quite sure that PHP_Parser has ability to do all those things, so I wanted to look at the complex examples! But thanks for help once more!:)
from php-parser.
@r3verser Look at this repo scrutinizer-ci/scrutinizer. It performs a deep code analysis.
from php-parser.
@Baachi thanks, that's one seems exactly what i want!
from php-parser.
You can look at my static code analyser and refactoring tool https://github.com/Trismegiste/Mondrian using about 7 visitors.
It transforms a set of php files to a digraph and runs analysis based on the Graph Theory.
from php-parser.
Maybe I should create a wiki page with a list of projects using PHP-Parser? So one can look at various sample code.
from php-parser.
@Trismegiste Thanks, realy interesting tool!
@nikic That would be great!
from php-parser.
I added a Wiki page here: https://github.com/nikic/PHP-Parser/wiki/Projects-using-the-PHP-Parser Just a few stubs right now. If you find some project using PHP-Parser, feel free to add it in there :)
from php-parser.
Related Issues (20)
- Comment attributes not being repeated for nested nodes is not in UPGRADING guide
- [5.0] Parser crashes on an empty file HOT 1
- [Format-preserving printer] How to get rid of trailing commas in function calls/parameter definitions/closure uses HOT 2
- [5.0] PropertyItem extends Stmt by mistake? HOT 1
- 5.0.0 - Call to undefined method PhpParser\ParserFactory::createForHostVersion() HOT 2
- Declaration of PhpParser\Parser\Multiple::parse HOT 1
- Upgrade Guide to v5.0 -- typo error on Changes to the lexer chapter
- TokenPolyfill tries to construct itself with an id which is a string HOT 5
- getPhpVersion() method has different return type in V5 HOT 2
- Should PhpParser\Node\Name extend PhpParser\Node\Expr ? HOT 2
- Parse arbitrary expressions HOT 4
- Appending new statements HOT 2
- Parser instance is not automatically garbage collected due to self references HOT 4
- TokenPolyfill issue on PHP 7.4 platform HOT 25
- Pretty printing removes leading whitespace from class declaration on first line HOT 6
- php-parser v4.19.0 got deprecated Optional parameter before required parameter on PrintableNewAnonClassNode.php HOT 1
- Is there a way to modify or remove comments? HOT 1
- Error "Syntax error, unexpected EOF on line" when parse valid php file HOT 1
- Is there a way to find places from where specific class/object methods are called HOT 6
- Confusing docs about the usage of NodeTraverser
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-parser.