ElggPluginManifestParser17::parse()   B
last analyzed

Complexity

Conditions 8
Paths 17

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 17
nop 0
dl 0
loc 36
ccs 0
cts 27
cp 0
crap 72
rs 8.0995
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plugin manifest.xml parser for Elgg 1.7 and lower.
4
 *
5
 * @package    Elgg.Core
6
 * @subpackage Plugins
7
 * @since      1.8
8
 */
9
class ElggPluginManifestParser17 extends \ElggPluginManifestParser {
10
	/**
11
	 * The valid top level attributes and defaults for a 1.7 manifest
12
	 */
13
	protected $validAttributes = array(
14
		'author', 'version', 'description', 'website',
15
		'copyright', 'license', 'licence', 'elgg_version',
16
17
		// were never really used and not enforced in code.
18
		'requires', 'recommends', 'conflicts',
19
20
		// not a 1.7 field, but we need it
21
		'name',
22
	);
23
24
	/**
25
	 * Parse a manifest object from 1.7 or earlier.
26
	 *
27
	 * @return void
28
	 */
29
	public function parse() {
30
		if (!isset($this->manifestObject->children)) {
31
			return false;
32
		}
33
34
		$elements = array();
35
36
		foreach ($this->manifestObject->children as $element) {
37
			$key = $element->attributes['key'];
38
			$value = $element->attributes['value'];
39
40
			// create arrays if multiple fields are set
41
			if (array_key_exists($key, $elements)) {
42
				if (!is_array($elements[$key])) {
43
					$orig = $elements[$key];
44
					$elements[$key] = array($orig);
45
				}
46
47
				$elements[$key][] = $value;
48
			} else {
49
				$elements[$key] = $value;
50
			}
51
		}
52
53
		if ($elements && !array_key_exists('name', $elements)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $elements 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...
54
			$elements['name'] = $this->caller->getName();
55
		}
56
57
		$this->manifest = $elements;
58
59
		if (!$this->manifest) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->manifest 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...
60
			return false;
61
		}
62
63
		return true;
64
	}
65
66
	/**
67
	 * Return an attribute in the manifest.
68
	 *
69
	 * Overrides \ElggPluginManifestParser::getAttribute() because before 1.8
70
	 * there were no rules...weeeeeeeee!
71
	 *
72
	 * @param string $name Attribute name
73
	 * @return mixed
74
	 */
75
	public function getAttribute($name) {
76
		if (isset($this->manifest[$name])) {
77
			return $this->manifest[$name];
78
		}
79
80
		return false;
81
	}
82
}
83