protected function doTestPrettyPrintMethod($method, $name, $code, $expected, $modeLine) { $lexer = new Lexer\Emulative(); $parser5 = new Parser\Php5($lexer); $parser7 = new Parser\Php7($lexer); list($version, $options) = $this->parseModeLine($modeLine); $prettyPrinter = new Standard($options); try { $output5 = canonicalize($prettyPrinter->{$method}($parser5->parse($code))); } catch (Error $e) { $output5 = null; $this->assertEquals('php7', $version); } try { $output7 = canonicalize($prettyPrinter->{$method}($parser7->parse($code))); } catch (Error $e) { $output7 = null; $this->assertEquals('php5', $version); } if ('php5' === $version) { $this->assertSame($expected, $output5, $name); $this->assertNotSame($expected, $output7, $name); } else { if ('php7' === $version) { $this->assertSame($expected, $output7, $name); $this->assertNotSame($expected, $output5, $name); } else { $this->assertSame($expected, $output5, $name); $this->assertSame($expected, $output7, $name); } } }
public function testSpecialClassNamesAreCaseInsensitive() { $source = <<<'EOC' <?php namespace Foo; class Bar { public static function method() { SELF::method(); PARENT::method(); STATIC::method(); } } EOC; $parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative()); $stmts = $parser->parse($source); $traverser = new PhpParser\NodeTraverser(); $traverser->addVisitor(new NameResolver()); $stmts = $traverser->traverse($stmts); $classStmt = $stmts[0]; $methodStmt = $classStmt->stmts[0]->stmts[0]; $this->assertSame('SELF', (string) $methodStmt->stmts[0]->class); $this->assertSame('PARENT', (string) $methodStmt->stmts[1]->class); $this->assertSame('STATIC', (string) $methodStmt->stmts[2]->class); }
/** * @covers PhpParser\Serializer\XML<extended> */ public function testSerialize() { $code = <<<CODE <?php // comment /** doc comment */ function functionName(&\$a = 0, \$b = 1.0) { echo 'Foo'; } CODE; $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar"> <scalar:array> <node:Stmt_Function> <attribute:comments> <scalar:array> <comment isDocComment="false" line="2">// comment </comment> <comment isDocComment="true" line="3">/** doc comment */</comment> </scalar:array> </attribute:comments> <attribute:startLine> <scalar:int>4</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>6</scalar:int> </attribute:endLine> <subNode:byRef> <scalar:false/> </subNode:byRef> <subNode:name> <scalar:string>functionName</scalar:string> </subNode:name> <subNode:params> <scalar:array> <node:Param> <attribute:startLine> <scalar:int>4</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>4</scalar:int> </attribute:endLine> <subNode:type> <scalar:null/> </subNode:type> <subNode:byRef> <scalar:true/> </subNode:byRef> <subNode:variadic> <scalar:false/> </subNode:variadic> <subNode:name> <scalar:string>a</scalar:string> </subNode:name> <subNode:default> <node:Scalar_LNumber> <attribute:startLine> <scalar:int>4</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>4</scalar:int> </attribute:endLine> <subNode:value> <scalar:int>0</scalar:int> </subNode:value> </node:Scalar_LNumber> </subNode:default> </node:Param> <node:Param> <attribute:startLine> <scalar:int>4</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>4</scalar:int> </attribute:endLine> <subNode:type> <scalar:null/> </subNode:type> <subNode:byRef> <scalar:false/> </subNode:byRef> <subNode:variadic> <scalar:false/> </subNode:variadic> <subNode:name> <scalar:string>b</scalar:string> </subNode:name> <subNode:default> <node:Scalar_DNumber> <attribute:startLine> <scalar:int>4</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>4</scalar:int> </attribute:endLine> <subNode:value> <scalar:float>1</scalar:float> </subNode:value> </node:Scalar_DNumber> </subNode:default> </node:Param> </scalar:array> </subNode:params> <subNode:returnType> <scalar:null/> </subNode:returnType> <subNode:stmts> <scalar:array> <node:Stmt_Echo> <attribute:startLine> <scalar:int>5</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>5</scalar:int> </attribute:endLine> <subNode:exprs> <scalar:array> <node:Scalar_String> <attribute:startLine> <scalar:int>5</scalar:int> </attribute:startLine> <attribute:endLine> <scalar:int>5</scalar:int> </attribute:endLine> <subNode:value> <scalar:string>Foo</scalar:string> </subNode:value> </node:Scalar_String> </scalar:array> </subNode:exprs> </node:Stmt_Echo> </scalar:array> </subNode:stmts> </node:Stmt_Function> </scalar:array> </AST> XML; $parser = new PhpParser\Parser\Php7(new PhpParser\Lexer); $serializer = new XML; $code = str_replace("\r\n", "\n", $code); $stmts = $parser->parse($code); $this->assertXmlStringEqualsXmlString($xml, $serializer->serialize($stmts)); }