Exemple #1
0
 /**
  * @dataProvider provideTestPrettyPrint
  * @covers PHPParser_PrettyPrinter_Zend<extended>
  */
 public function testPrettyPrint($name, $code, $dump)
 {
     $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative());
     $prettyPrinter = new PHPParser_PrettyPrinter_Zend();
     $stmts = $parser->parse($code);
     $this->assertEquals($this->canonicalize($dump), $this->canonicalize($prettyPrinter->prettyPrint($stmts)), $name);
 }
Exemple #2
0
    /**
     * @dataProvider provideTestConversion
     */
    public function testConversion($originalCode, $expectedCode, $expectedOutput) {
        $factory = new PHPBackporter_Factory;

        $parser        = new PHPParser_Parser;
        $traverser     = $factory->getTraverser();
        $prettyPrinter = new PHPParser_PrettyPrinter_Zend;

        $stmts = $parser->parse(new PHPParser_Lexer('<?php ' . $originalCode));

        $traverser->traverse($stmts);

        $code = $prettyPrinter->prettyPrint($stmts);

        if (false === strpos($expectedCode, '%')) {
            $this->assertEquals($expectedCode, $code);
        } else {
            $this->assertStringMatchesFormat($expectedCode, $code);
        }

        ob_start();
        eval($code);
        $output = trim(ob_get_clean());

        if (false === strpos($expectedOutput, '%')) {
            $this->assertEquals($expectedOutput, $output);
        } else {
            $this->assertStringMatchesFormat($expectedOutput, $output);
        }
    }
Exemple #3
0
 /**
  * @dataProvider provideTestPlaceholderReplacement
  * @covers PHPParser_Template
  */
 public function testPlaceholderReplacement($templateCode, $placeholders, $expectedPrettyPrint)
 {
     $parser = new PHPParser_Parser();
     $prettyPrinter = new PHPParser_PrettyPrinter_Zend();
     $template = new PHPParser_Template($parser, $templateCode);
     $this->assertEquals($expectedPrettyPrint, $prettyPrinter->prettyPrint($template->getStmts($placeholders)));
 }
function snowscript_to_php($code, $debug = false, $return = false, $namespace = "Anonymous")
{
    $lexer = new Snowscript_Lexer($code . "\n");
    if ($debug) {
        debug_lexer($lexer);
    }
    $parser = new PHPParser_Parser();
    $prettyPrinter = new PHPParser_PrettyPrinter_Zend();
    $stmts = $parser->parse($lexer);
    $traverser = new PHPParser_NodeTraverser();
    $scope_traverser = new Snowscript_Visitors_Scope($namespace);
    $traverser->addVisitor($scope_traverser);
    $stmts = $traverser->traverse($stmts);
    $nodeDumper = new PHPParser_NodeDumper();
    if ($debug) {
        $nodeDumper = new PHPParser_NodeDumper();
        echo $nodeDumper->dump($stmts) . "\n";
    }
    $php = $prettyPrinter->prettyPrint($stmts) . "\n";
    if ($return) {
        return $php;
    } else {
        print "<?php\n" . $php;
    }
}
Exemple #5
0
function snowscript_to_php($code, $debug = false, $return = false)
{
    $lexer = new Snowscript_Lexer($code . "\n");
    if ($debug) {
        debug_lexer($lexer);
    }
    $parser = new PHPParser_Parser();
    $prettyPrinter = new PHPParser_PrettyPrinter_Zend();
    $stmts = $parser->parse($lexer);
    if ($debug) {
        $nodeDumper = new PHPParser_NodeDumper();
        echo $nodeDumper->dump($stmts) . "\n";
    }
    $php = $prettyPrinter->prettyPrint($stmts) . "\n";
    if ($return) {
        return $php;
    } else {
        print "<?php\n" . $php;
    }
}
Exemple #6
0
    /**
     * @covers PHPParser_NodeVisitor_NameResolver
     */
    public function testResolveLocations()
    {
        $code = <<<EOC
<?php
namespace NS {
    class A extends B implements C {
        use A;
    }

    interface A extends C {
        public function a(A \$a);
    }

    A::b();
    A::\$b;
    A::B;
    new A;
    \$a instanceof A;

    namespace\\a();
    namespace\\A;

    try {
        \$someThing;
    } catch (A \$a) {
        \$someThingElse;
    }
}
EOC;
        $expectedCode = <<<EOC
namespace NS {
    class A extends \\NS\\B implements \\NS\\C
    {
        use \\NS\\A;
    }
    interface A extends \\NS\\C
    {
        public function a(\\NS\\A \$a);
    }
    \\NS\\A::b();
    \\NS\\A::\$b;
    \\NS\\A::B;
    new \\NS\\A();
    \$a instanceof \\NS\\A;
    \\NS\\a();
    \\NS\\A;
    try {
        \$someThing;
    } catch (\\NS\\A \$a) {
        \$someThingElse;
    }
}
EOC;
        $parser = new PHPParser_Parser(new PHPParser_Lexer_Emulative());
        $prettyPrinter = new PHPParser_PrettyPrinter_Zend();
        $traverser = new PHPParser_NodeTraverser();
        $traverser->addVisitor(new PHPParser_NodeVisitor_NameResolver());
        $stmts = $parser->parse($code);
        $stmts = $traverser->traverse($stmts);
        $this->assertEquals($expectedCode, $prettyPrinter->prettyPrint($stmts));
    }
 public static function getStringRepr(\PHPParser_Node $node)
 {
     static $prettyPrinter;
     if (null === $prettyPrinter) {
         $prettyPrinter = new \PHPParser_PrettyPrinter_Zend();
     }
     if ($node instanceof \PHPParser_Node_Stmt_If || $node instanceof \PHPParser_Node_Stmt_ElseIf) {
         $label = 'if (';
         $label .= $prettyPrinter->prettyPrintExpr($node->cond);
         $label .= ')';
         return $label;
     }
     if ($node instanceof \PHPParser_Node_Stmt_Label) {
         return 'Label ' . $node->name;
     }
     if ($node instanceof \PHPParser_Node_Stmt_Echo) {
         return 'echo ' . $prettyPrinter->prettyPrint($node->exprs);
     }
     if ($node instanceof \PHPParser_Node_Scalar_String) {
         return 'string(' . strlen($node->value) . ') ' . var_export($node->value, true);
     }
     if ($node instanceof \PHPParser_Node_Expr_Variable) {
         if (is_string($node->name)) {
             return '$' . $node->name;
         }
         return 'Variable';
     }
     if ($node instanceof BlockNode) {
         $str = 'Block';
         if ($parent = $node->getAttribute('parent')) {
             $str .= ' of ' . self::getStringRepr($parent);
         } else {
             $str .= ' (global)';
         }
         return $str;
     }
     if ($node instanceof \PHPParser_Node_Expr_Assign) {
         return 'Assign (L' . $node->getLine() . ')';
     }
     if ($node instanceof \PHPParser_Node_Stmt_Catch) {
         return 'catch ' . implode("\\", $node->type->parts);
     }
     return get_class($node);
 }
Exemple #8
0
     if (!preg_match('~--FILE--\\s*(.*?)--[A-Z]+--~s', $code, $matches)) {
         continue;
     }
     if (preg_match('~--EXPECT(?:F|REGEX)?--\\s*(?:Parse|Fatal) error~', $code)) {
         continue;
     }
     $code = $matches[1];
 }
 set_time_limit(10);
 ++$count;
 try {
     $startTime = microtime(true);
     $stmts = $parser->parse(new PHPParser_Lexer_Emulative($code));
     $parseTime += microtime(true) - $startTime;
     $startTime = microtime(true);
     $code = '<?php' . "\n" . $prettyPrinter->prettyPrint($stmts);
     $ppTime += microtime(true) - $startTime;
     try {
         $startTime = microtime(true);
         $ppStmts = $parser->parse(new PHPParser_Lexer_Emulative($code));
         $reparseTime += microtime(true) - $startTime;
         $startTime = microtime(true);
         $same = $nodeDumper->dump($stmts) == $nodeDumper->dump($ppStmts);
         $compareTime += microtime(true) - $startTime;
         if (!$same) {
             echo $file, ":\n    Result of initial parse and parse after pretty print differ\n";
             ++$compareFail;
         }
     } catch (PHPParser_Error $e) {
         echo $file, ":\n    Parse of pretty print failed with message: {$e->getMessage()}\n";
         ++$ppFail;