Giter Club home page Giter Club logo

Comments (8)

hinikato avatar hinikato commented on May 9, 2024

@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:

  1. 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()

  1. 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.
  2. 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.

  1. 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.

r3verser avatar r3verser commented on May 9, 2024

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.

Baachi avatar Baachi commented on May 9, 2024

@r3verser Look at this repo scrutinizer-ci/scrutinizer. It performs a deep code analysis.

from php-parser.

r3verser avatar r3verser commented on May 9, 2024

@Baachi thanks, that's one seems exactly what i want!

from php-parser.

Trismegiste avatar Trismegiste commented on May 9, 2024

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.

nikic avatar nikic commented on May 9, 2024

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.

r3verser avatar r3verser commented on May 9, 2024

@Trismegiste Thanks, realy interesting tool!
@nikic That would be great!

from php-parser.

nikic avatar nikic commented on May 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.