Inheritance: extends PHPParser_PrettyPrinterAbstract
Example #1
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);
        }
    }
Example #2
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);
 }
Example #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)));
 }
Example #4
0
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;
    }
}
Example #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;
    }
}
Example #6
0
 /**
  * Function: pExpr_Include
  *
  * Handles the inclusion of script-files.
  */
 public function pExpr_Include(PHPParser_Node_Expr_Include $node)
 {
     $file_to_include = $node->expr->value;
     if ($file_to_include) {
         LogMore::debug('File to include: %s', $file_to_include);
         # If the file should be only included/required once
         if ($node->type == PHPParser_Node_Expr_Include::TYPE_INCLUDE_ONCE || $node->type == PHPParser_Node_Expr_Include::TYPE_REQUIRE_ONCE) {
             # If the file has already been included
             if (isset($this->includedFiles[$file_to_include])) {
                 LogMore::debug('File has already been included once');
                 # Leave function
                 return null;
             }
         }
         $code = $this->parseFile($file_to_include);
         # Add file to array of included files and raise counter:
         if (isset($this->includedFiles[$file_to_include])) {
             $this->includedFiles[$file_to_include] += 1;
         } else {
             $this->includedFiles[$file_to_include] = 1;
         }
         return $code;
     } else {
         return parent::pExpr_Include($node);
     }
 }
Example #7
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 function enterNode(\PHPParser_Node $node)
 {
     $prettyPrinter = new \PHPParser_PrettyPrinter_Zend();
     switch (get_class($node)) {
         case 'PHPParser_Node_Stmt_Use':
             /** @var \PHPParser_Node_Stmt_UseUse $use */
             foreach ($node->uses as $use) {
                 $this->namespace_aliases[$use->alias] = implode('\\', $use->name->parts);
             }
             break;
         case 'PHPParser_Node_Stmt_Namespace':
             $this->current_namespace = implode('\\', $node->name->parts);
             break;
         case 'PHPParser_Node_Stmt_Class':
             $class = new ClassReflector($node);
             $class->setNamespaceAliases($this->namespace_aliases);
             $class->parseSubElements();
             $this->classes[] = $class;
             break;
         case 'PHPParser_Node_Stmt_Trait':
             $trait = new TraitReflector($node);
             $trait->setNamespaceAliases($this->namespace_aliases);
             $this->traits[] = $trait;
             break;
         case 'PHPParser_Node_Stmt_Interface':
             $interface = new InterfaceReflector($node);
             $interface->setNamespaceAliases($this->namespace_aliases);
             $interface->parseSubElements();
             $this->interfaces[] = $interface;
             break;
         case 'PHPParser_Node_Stmt_Function':
             $function = new FunctionReflector($node);
             $function->setNamespaceAliases($this->namespace_aliases);
             $this->functions[] = $function;
             break;
         case 'PHPParser_Node_Stmt_Const':
             foreach ($node->consts as $constant) {
                 $reflector = new ConstantReflector($node, $constant);
                 $reflector->setNamespaceAliases($this->namespace_aliases);
                 $this->constants[$reflector->getName()] = $reflector;
             }
             break;
         case 'PHPParser_Node_Expr_FuncCall':
             if ($node->name instanceof \PHPParser_Node_Name && $node->name == 'define') {
                 $name = trim($prettyPrinter->prettyPrintExpr($node->args[0]->value), '\'');
                 $constant = new \PHPParser_Node_Const($name, $node->args[1]->value, $node->getAttributes());
                 $constant->namespacedName = new \PHPParser_Node_Name(($this->current_namespace ? $this->current_namespace . '\\' : '') . $name);
                 // we use $constant here both times since this is a
                 // FuncCall, which combines properties that are otherwise
                 // split over 2 objects
                 $reflector = new ConstantReflector($constant, $constant);
                 $reflector->setNamespaceAliases($this->namespace_aliases);
                 $this->constants[$reflector->getName()] = $reflector;
             }
             break;
         case 'PHPParser_Node_Expr_Include':
             $include = new IncludeReflector($node);
             $include->setNamespaceAliases($this->namespace_aliases);
             $this->includes[] = $include;
             break;
     }
 }
 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);
 }
Example #10
0
 public function pStmt_InlineHTML(PHPParser_Node_Stmt_InlineHTML $node)
 {
     ++$this->inlineHTMLBlocksCount;
     return parent::pStmt_InlineHTML($node);
 }
Example #11
0
    function filter_func($path)
    {
        return preg_match('~\\.php(?:\\.cache)?$~', $path) && false === strpos($path, 'skeleton');
    }
} elseif ('PHP' === $TEST_TYPE) {
    function filter_func($path)
    {
        return preg_match('~\\.phpt$~', $path);
    }
} else {
    die('The test type must be either "Symfony" or "PHP".');
}
require_once dirname(__FILE__) . '/../lib/PHPParser/Autoloader.php';
PHPParser_Autoloader::register();
$parser = new PHPParser_Parser();
$prettyPrinter = new PHPParser_PrettyPrinter_Zend();
$nodeDumper = new PHPParser_NodeDumper();
$parseFail = $ppFail = $compareFail = $count = 0;
$readTime = $parseTime = $ppTime = $reparseTime = $compareTime = 0;
$totalStartTime = microtime(true);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($DIR), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
    if (!filter_func($file)) {
        continue;
    }
    $startTime = microtime(true);
    $code = file_get_contents($file);
    $readTime += microtime(true) - $startTime;
    if ('PHP' === $TEST_TYPE) {
        if (preg_match('~(?:
# skeleton files
  ext.gmp.tests.001