XML2ArrayParser::parse_all()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * @author Niels A.D.
4
 * @author Todd Burry <[email protected]>
5
 * @copyright 2010 Niels A.D., 2014 Todd Burry
6
 * @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
7
 * @package pQuery
8
 */
9
10
namespace pQuery;
11
12
/**
13
 * Converts a XML document to an array
14
 */
15
class XML2ArrayParser extends HtmlParserBase {
16
17
	/**
18
	 * Holds the document structure
19
	 * @var array array('name' => 'tag', 'attrs' => array('attr' => 'val'), 'childen' => array())
20
	 */
21
	var $root = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $root.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
22
		'name' => '',
23
		'attrs' => array(),
24
		'children' => array()
25
	);
26
27
	/**
28
	 * Current parsing hierarchy
29
	 * @var array
30
	 * @access private
31
	 */
32
	var $hierarchy = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $hierarchy.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
33
34
	protected function parse_hierarchy($self_close) {
35
		if ($this->status['closing_tag']) {
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
36
			$found = false;
37
			for ($count = count($this->hierarchy), $i = $count - 1; $i >= 0; $i--) {
38
				if (strcasecmp($this->hierarchy[$i]['name'], $this->status['tag_name']) === 0) {
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
39
40
					for($ii = ($count - $i - 1); $ii >= 0; $ii--) {
41
						$e = array_pop($this->hierarchy);
42
						if ($ii > 0) {
43
							$this->addError('Closing tag "'.$this->status['tag_name'].'" while "'.$e['name'].'" is not closed yet');
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
44
						}
45
					}
46
47
					$found = true;
48
					break;
49
				}
50
			}
51
52
			if (!$found) {
53
				$this->addError('Closing tag "'.$this->status['tag_name'].'" which is not open');
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
54
			}
55
		} else {
56
			$tag = array(
57
				'name' => $this->status['tag_name'],
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
58
				'attrs' => $this->status['attributes'],
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
59
				'children' => array()
60
			);
61
			if ($this->hierarchy) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->hierarchy of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
				$current =& $this->hierarchy[count($this->hierarchy) - 1];
63
				$current['children'][] = $tag;
64
				$tag =& $current['children'][count($current['children']) - 1];
65
				unset($current['tagData']);
66
			} else {
67
				$this->root = $tag;
68
				$tag =& $this->root;
69
				$self_close = false;
70
			}
71
			if (!$self_close) {
72
				$this->hierarchy[] =& $tag;
73
			}
74
		}
75
	}
76
77
	function parse_tag_default() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
78
		if (!parent::parse_tag_default()) {return false;}
79
80
		if ($this->status['tag_name'][0] !== '?') {
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
81
			$this->parse_hierarchy(($this->status['self_close']) ? true : null);
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
82
		}
83
		return true;
84
	}
85
86
	function parse_text() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
		parent::parse_text();
88
		if (($this->status['text'] !== '') && $this->hierarchy) {
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
Bug Best Practice introduced by
The expression $this->hierarchy of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89
			$current =& $this->hierarchy[count($this->hierarchy) - 1];
90
			if (!$current['children']) {
91
				$current['tagData'] = $this->status['text'];
0 ignored issues
show
Bug introduced by
The property status cannot be accessed from this context as it is declared private in class pQuery\HtmlParserBase.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
92
			}
93
		}
94
	}
95
96
	function parse_all() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
97
		return ((parent::parse_all()) ? $this->root : false);
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::parse_all() ? $this->root : false; of type array|false adds the type array to the return on line 97 which is incompatible with the return type of the parent method pQuery\HtmlParserBase::parse_all of type boolean.
Loading history...
98
	}
99
}
100
101
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...