PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/scanner/PHP-Parser/lib/PHPParser/Unserializer/XML.php

https://bitbucket.org/theguly/thaps
PHP | 118 lines | 99 code | 18 blank | 1 comment | 17 complexity | be159e0d0ac0af9ee9ef506d2e8ef265 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. class PHPParser_Unserializer_XML implements PHPParser_Unserializer
  3. {
  4. protected $reader;
  5. public function __construct() {
  6. $this->reader = new XMLReader;
  7. }
  8. public function unserialize($string) {
  9. $this->reader->XML($string);
  10. $this->reader->read();
  11. if ('AST' !== $this->reader->name) {
  12. throw new DomainException('AST root element not found');
  13. }
  14. return $this->read($this->reader->depth);
  15. }
  16. protected function read($depthLimit, $throw = true, &$nodeFound = null) {
  17. $nodeFound = true;
  18. while ($this->reader->read() && $depthLimit < $this->reader->depth) {
  19. if (XMLReader::ELEMENT !== $this->reader->nodeType) {
  20. continue;
  21. }
  22. if ('node' === $this->reader->prefix) {
  23. return $this->readNode();
  24. } elseif ('scalar' === $this->reader->prefix) {
  25. return $this->readScalar();
  26. } else {
  27. throw new DomainException(sprintf('Unexpected node of type "%s"', $this->reader->name));
  28. }
  29. }
  30. $nodeFound = false;
  31. if ($throw) {
  32. throw new DomainException('Expected node or scalar');
  33. }
  34. }
  35. protected function readNode()
  36. {
  37. $className = 'PHPParser_Node_' . $this->reader->localName;
  38. // create the node without calling it's constructor
  39. $node = unserialize(
  40. sprintf('O:%d:"%s":0:{}', strlen($className), $className)
  41. );
  42. $line = $this->reader->getAttribute('line');
  43. $node->setLine(null !== $line ? $line : -1);
  44. $docComment = $this->reader->getAttribute('docComment');
  45. $node->setDocComment($docComment);
  46. $depthLimit = $this->reader->depth;
  47. while ($this->reader->read() && $depthLimit < $this->reader->depth) {
  48. if (XMLReader::ELEMENT !== $this->reader->nodeType) {
  49. continue;
  50. }
  51. if ('subNode' !== $this->reader->prefix) {
  52. throw new DomainException(
  53. sprintf('Expected sub node, got node of type "%s"', $this->reader->name)
  54. );
  55. }
  56. $subNodeName = $this->reader->localName;
  57. $subNodeContent = $this->read($this->reader->depth);
  58. $node->$subNodeName = $subNodeContent;
  59. }
  60. return $node;
  61. }
  62. protected function readScalar() {
  63. switch ($name = $this->reader->localName) {
  64. case 'array':
  65. $depth = $this->reader->depth;
  66. $array = array();
  67. while (true) {
  68. $node = $this->read($depth, false, $nodeFound);
  69. if (!$nodeFound) {
  70. break;
  71. }
  72. $array[] = $node;
  73. }
  74. return $array;
  75. case 'string':
  76. return $this->reader->readString();
  77. case 'int':
  78. $text = $this->reader->readString();
  79. if (false === $int = filter_var($text, FILTER_VALIDATE_INT)) {
  80. throw new DomainException(sprintf('"%s" is not a valid integer', $text));
  81. }
  82. return $int;
  83. case 'float':
  84. $text = $this->reader->readString();
  85. if (false === $float = filter_var($text, FILTER_VALIDATE_FLOAT)) {
  86. throw new DomainException(sprintf('"%s" is not a valid float', $text));
  87. }
  88. return $float;
  89. case 'true':
  90. case 'false':
  91. case 'null':
  92. if (!$this->reader->isEmptyElement) {
  93. throw new DomainException(sprintf('"%s" scalar must be empty', $name));
  94. }
  95. return constant($name);
  96. default:
  97. throw new DomainException(sprintf('Unknown scalar type "%s"', $name));
  98. }
  99. }
  100. }