Remove XML serializer

This commit is contained in:
Nikita Popov 2017-02-04 01:10:09 +01:00
parent 7fa5495d64
commit 90b6d7cb5b
7 changed files with 3 additions and 630 deletions

View File

@ -22,4 +22,6 @@ source code, while running on a newer version.
* Removed `type` subnode on `Class`, `ClassMethod` and `Property` nodes. Use `flags` instead.
* The `ClassConst::isStatic()` method has been removed. Constants cannot have a static modifier.
* The `NodeTraverser` no longer accepts `false` as a return value from a `leaveNode()` method.
`NodeTraverser::REMOVE_NODE` should be returned instead.
`NodeTraverser::REMOVE_NODE` should be returned instead.
* The XML serializer has been removed. As such, the classes `Serializer\XML`, and
`Unserializer\XML`, as well as the interfaces `Serializer` and `Unserializer` no longer exist.

View File

@ -1,18 +0,0 @@
<?php
namespace PhpParser;
/**
* @deprecated
*/
interface Serializer
{
/**
* Serializes statements into some string format.
*
* @param array $nodes Statements
*
* @return string Serialized string
*/
public function serialize(array $nodes);
}

View File

@ -1,89 +0,0 @@
<?php
namespace PhpParser\Serializer;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Serializer;
use XMLWriter;
/**
* @deprecated
*/
class XML implements Serializer
{
protected $writer;
/**
* Constructs a XML serializer.
*/
public function __construct() {
$this->writer = new XMLWriter;
$this->writer->openMemory();
$this->writer->setIndent(true);
}
public function serialize(array $nodes) {
$this->writer->flush();
$this->writer->startDocument('1.0', 'UTF-8');
$this->writer->startElement('AST');
$this->writer->writeAttribute('xmlns:node', 'http://nikic.github.com/PHPParser/XML/node');
$this->writer->writeAttribute('xmlns:subNode', 'http://nikic.github.com/PHPParser/XML/subNode');
$this->writer->writeAttribute('xmlns:attribute', 'http://nikic.github.com/PHPParser/XML/attribute');
$this->writer->writeAttribute('xmlns:scalar', 'http://nikic.github.com/PHPParser/XML/scalar');
$this->_serialize($nodes);
$this->writer->endElement();
return $this->writer->outputMemory();
}
protected function _serialize($node) {
if ($node instanceof Node) {
$this->writer->startElement('node:' . $node->getType());
foreach ($node->getAttributes() as $name => $value) {
$this->writer->startElement('attribute:' . $name);
$this->_serialize($value);
$this->writer->endElement();
}
foreach ($node as $name => $subNode) {
$this->writer->startElement('subNode:' . $name);
$this->_serialize($subNode);
$this->writer->endElement();
}
$this->writer->endElement();
} elseif ($node instanceof Comment) {
$this->writer->startElement('comment');
$this->writer->writeAttribute('isDocComment', $node instanceof Comment\Doc ? 'true' : 'false');
$this->writer->writeAttribute('line', (string) $node->getLine());
$this->writer->text($node->getText());
$this->writer->endElement();
} elseif (is_array($node)) {
$this->writer->startElement('scalar:array');
foreach ($node as $subNode) {
$this->_serialize($subNode);
}
$this->writer->endElement();
} elseif (is_string($node)) {
$this->writer->writeElement('scalar:string', $node);
} elseif (is_int($node)) {
$this->writer->writeElement('scalar:int', (string) $node);
} elseif (is_float($node)) {
// TODO Higher precision conversion?
$this->writer->writeElement('scalar:float', (string) $node);
} elseif (true === $node) {
$this->writer->writeElement('scalar:true');
} elseif (false === $node) {
$this->writer->writeElement('scalar:false');
} elseif (null === $node) {
$this->writer->writeElement('scalar:null');
} else {
throw new \InvalidArgumentException('Unexpected node type');
}
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace PhpParser;
/**
* @deprecated
*/
interface Unserializer
{
/**
* Unserializes a string in some format into a node tree.
*
* @param string $string Serialized string
*
* @return mixed Node tree
*/
public function unserialize($string);
}

View File

@ -1,162 +0,0 @@
<?php
namespace PhpParser\Unserializer;
use DomainException;
use PhpParser\Unserializer;
use XMLReader;
/**
* @deprecated
*/
class XML implements Unserializer
{
protected $reader;
public function __construct() {
$this->reader = new XMLReader;
}
public function unserialize($string) {
$this->reader->XML($string);
$this->reader->read();
if ('AST' !== $this->reader->name) {
throw new DomainException('AST root element not found');
}
return $this->read($this->reader->depth);
}
protected function read($depthLimit, $throw = true, &$nodeFound = null) {
$nodeFound = true;
while ($this->reader->read() && $depthLimit < $this->reader->depth) {
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
continue;
}
if ('node' === $this->reader->prefix) {
return $this->readNode();
} elseif ('scalar' === $this->reader->prefix) {
return $this->readScalar();
} elseif ('comment' === $this->reader->name) {
return $this->readComment();
} else {
throw new DomainException(sprintf('Unexpected node of type "%s"', $this->reader->name));
}
}
$nodeFound = false;
if ($throw) {
throw new DomainException('Expected node or scalar');
}
}
protected function readNode() {
$className = $this->getClassNameFromType($this->reader->localName);
// create the node without calling it's constructor
$node = unserialize(
sprintf(
"O:%d:\"%s\":1:{s:13:\"\0*\0attributes\";a:0:{}}",
strlen($className), $className
)
);
$depthLimit = $this->reader->depth;
while ($this->reader->read() && $depthLimit < $this->reader->depth) {
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
continue;
}
$type = $this->reader->prefix;
if ('subNode' !== $type && 'attribute' !== $type) {
throw new DomainException(
sprintf('Expected sub node or attribute, got node of type "%s"', $this->reader->name)
);
}
$name = $this->reader->localName;
$value = $this->read($this->reader->depth);
if ('subNode' === $type) {
$node->$name = $value;
} else {
$node->setAttribute($name, $value);
}
}
return $node;
}
protected function readScalar() {
switch ($name = $this->reader->localName) {
case 'array':
$depth = $this->reader->depth;
$array = array();
while (true) {
$node = $this->read($depth, false, $nodeFound);
if (!$nodeFound) {
break;
}
$array[] = $node;
}
return $array;
case 'string':
return $this->reader->readString();
case 'int':
return $this->parseInt($this->reader->readString());
case 'float':
$text = $this->reader->readString();
if (false === $float = filter_var($text, FILTER_VALIDATE_FLOAT)) {
throw new DomainException(sprintf('"%s" is not a valid float', $text));
}
return $float;
case 'true':
case 'false':
case 'null':
if (!$this->reader->isEmptyElement) {
throw new DomainException(sprintf('"%s" scalar must be empty', $name));
}
return constant($name);
default:
throw new DomainException(sprintf('Unknown scalar type "%s"', $name));
}
}
private function parseInt($text) {
if (false === $int = filter_var($text, FILTER_VALIDATE_INT)) {
throw new DomainException(sprintf('"%s" is not a valid integer', $text));
}
return $int;
}
protected function readComment() {
$className = $this->reader->getAttribute('isDocComment') === 'true'
? 'PhpParser\Comment\Doc'
: 'PhpParser\Comment'
;
return new $className(
$this->reader->readString(),
$this->parseInt($this->reader->getAttribute('line'))
);
}
/**
* Reconstruct the class name from the node type.
*
* @param string $type Node type
*
* @return string Node class name
*/
protected function getClassNameFromType($type) {
$className = 'PhpParser\\Node\\' . strtr($type, '_', '\\');
if (!class_exists($className)) {
$className .= '_';
}
if (!class_exists($className)) {
throw new DomainException(sprintf('Unknown node type "%s"', $type));
}
return $className;
}
}

View File

@ -1,192 +0,0 @@
<?php
namespace PhpParser\Serializer;
use PhpParser;
class XMLTest extends \PHPUnit_Framework_TestCase
{
/**
* @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"?>
<AST xmlns:attribute="http://nikic.github.com/PHPParser/XML/attribute" xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode">
<scalar:array>
<node:Stmt_Function>
<attribute:startLine>
<scalar:int>4</scalar:int>
</attribute:startLine>
<attribute:comments>
<scalar:array>
<comment isDocComment="false" line="2">// comment
</comment>
<comment isDocComment="true" line="3">/** doc comment */</comment>
</scalar:array>
</attribute:comments>
<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:var>
<node:Expr_Variable>
<attribute:startLine>
<scalar:int>4</scalar:int>
</attribute:startLine>
<attribute:endLine>
<scalar:int>4</scalar:int>
</attribute:endLine>
<subNode:name>
<scalar:string>a</scalar:string>
</subNode:name>
</node:Expr_Variable>
</subNode:var>
<subNode:default>
<node:Scalar_LNumber>
<attribute:startLine>
<scalar:int>4</scalar:int>
</attribute:startLine>
<attribute:endLine>
<scalar:int>4</scalar:int>
</attribute:endLine>
<attribute:kind>
<scalar:int>10</scalar:int>
</attribute:kind>
<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:var>
<node:Expr_Variable>
<attribute:startLine>
<scalar:int>4</scalar:int>
</attribute:startLine>
<attribute:endLine>
<scalar:int>4</scalar:int>
</attribute:endLine>
<subNode:name>
<scalar:string>b</scalar:string>
</subNode:name>
</node:Expr_Variable>
</subNode:var>
<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>
<attribute:kind>
<scalar:int>1</scalar:int>
</attribute:kind>
<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));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unexpected node type
*/
public function testError() {
$serializer = new XML;
$serializer->serialize(array(new \stdClass));
}
}

View File

@ -1,150 +0,0 @@
<?php
namespace PhpParser\Unserializer;
use PhpParser\Comment;
use PhpParser\Node\Scalar;
class XMLTest extends \PHPUnit_Framework_TestCase
{
public function testNode() {
$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">
<node:Scalar_String line="1" docComment="/** doc comment */">
<attribute:startLine>
<scalar:int>1</scalar:int>
</attribute:startLine>
<attribute:comments>
<scalar:array>
<comment isDocComment="false" line="2">// comment
</comment>
<comment isDocComment="true" line="3">/** doc comment */</comment>
</scalar:array>
</attribute:comments>
<subNode:value>
<scalar:string>Test</scalar:string>
</subNode:value>
</node:Scalar_String>
</AST>
XML;
$unserializer = new XML;
$this->assertEquals(
new Scalar\String_('Test', array(
'startLine' => 1,
'comments' => array(
new Comment('// comment' . "\n", 2),
new Comment\Doc('/** doc comment */', 3),
),
)),
$unserializer->unserialize($xml)
);
}
public function testEmptyNode() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node">
<node:Scalar_MagicConst_Class />
</AST>
XML;
$unserializer = new XML;
$this->assertEquals(
new Scalar\MagicConst\Class_,
$unserializer->unserialize($xml)
);
}
public function testScalars() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
<scalar:array>
<scalar:array></scalar:array>
<scalar:array/>
<scalar:string>test</scalar:string>
<scalar:string></scalar:string>
<scalar:string/>
<scalar:int>1</scalar:int>
<scalar:float>1</scalar:float>
<scalar:float>1.5</scalar:float>
<scalar:true/>
<scalar:false/>
<scalar:null/>
</scalar:array>
</AST>
XML;
$result = array(
array(), array(),
'test', '', '',
1,
1, 1.5,
true, false, null
);
$unserializer = new XML;
$this->assertEquals($result, $unserializer->unserialize($xml));
}
/**
* @expectedException \DomainException
* @expectedExceptionMessage AST root element not found
*/
public function testWrongRootElementError() {
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<notAST/>
XML;
$unserializer = new XML;
$unserializer->unserialize($xml);
}
/**
* @dataProvider provideTestErrors
*/
public function testErrors($xml, $errorMsg) {
$this->setExpectedException('DomainException', $errorMsg);
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<AST xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar"
xmlns:node="http://nikic.github.com/PHPParser/XML/node"
xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode"
xmlns:foo="http://nikic.github.com/PHPParser/XML/foo">
$xml
</AST>
XML;
$unserializer = new XML;
$unserializer->unserialize($xml);
}
public function provideTestErrors() {
return array(
array('<scalar:true>test</scalar:true>', '"true" scalar must be empty'),
array('<scalar:false>test</scalar:false>', '"false" scalar must be empty'),
array('<scalar:null>test</scalar:null>', '"null" scalar must be empty'),
array('<scalar:foo>bar</scalar:foo>', 'Unknown scalar type "foo"'),
array('<scalar:int>x</scalar:int>', '"x" is not a valid int'),
array('<scalar:float>x</scalar:float>', '"x" is not a valid float'),
array('', 'Expected node or scalar'),
array('<foo:bar>test</foo:bar>', 'Unexpected node of type "foo:bar"'),
array(
'<node:Scalar_String><foo:bar>test</foo:bar></node:Scalar_String>',
'Expected sub node or attribute, got node of type "foo:bar"'
),
array(
'<node:Scalar_String><subNode:value/></node:Scalar_String>',
'Expected node or scalar'
),
array(
'<node:Foo><subNode:value/></node:Foo>',
'Unknown node type "Foo"'
),
);
}
}