Completed
Push — refact-v2 ( d4acae...9bddae )
by mw
03:23
created

PropertyRegistry::addPropertyDefinition()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 8
nop 2
dl 0
loc 35
ccs 22
cts 22
cp 1
crap 4
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace SESP;
4
5
use SMW\PropertyRegistry as BasePropertyRegistry;
6
use SMW\DataTypeRegistry;
7
use SMW\DIProperty;
8
use SMW\Message;
9
use SMWDataItem as DataItem;
10
11
/**
12
 * @ingroup SESP
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class PropertyRegistry {
20
21
	/**
22
	 * @var AppFactory
23
	 */
24
	private $appFactory;
25
26
	/**
27
	 * @since 2.0
28
	 *
29
	 * @param AppFactory $appFactory
30
	 */
31 5
	public function __construct( AppFactory $appFactory ) {
32 5
		$this->appFactory = $appFactory;
33 5
	}
34
35
	/**
36
	 * @since 1.0
37
	 *
38
	 * @param PropertyRegistry $propertyRegistry
39
	 *
40
	 * @return boolean
41
	 */
42 2
	public function registerOn( BasePropertyRegistry $propertyRegistry ) {
43
44 2
		$definitions = $this->appFactory->getPropertyDefinitions();
45
46 2
		foreach ( $definitions as $key => $definition ) {
47
48 1
			if ( !isset( $definition['id'] ) ) {
49
				continue;
50
			}
51
52 1
			$this->addPropertyDefinition( $propertyRegistry, $definition );
53 2
		}
54
55 2
		foreach ( $definitions->safeGet( '_EXIF', array() ) as $key => $definition ) {
56
57
			if ( !isset( $definition['id'] ) ) {
58
				continue;
59
			}
60
61
			$this->addPropertyDefinition( $propertyRegistry, $definition );
62 2
		}
63
64 2
		return true;
65
	}
66
67
	/**
68
	 * @since 2.0
69
	 *
70
	 * @param array $customFixedProperties
71
	 * @param array $fixedPropertyTablePrefix
72
	 */
73 2
	public function registerAsFixedProperties( &$customFixedProperties, &$fixedPropertyTablePrefix ) {
74
75 2
		if ( $this->appFactory->getOption( 'sespUseAsFixedTables' ) === false ) {
76 1
			return;
77
		}
78
79 1
		$definitions = $this->appFactory->getPropertyDefinitions();
80
81 1
		$properties = array_flip(
82 1
			$this->appFactory->getOption( 'sespSpecialProperties', array() )
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83 1
		);
84
85 1
		foreach ( $definitions as $key => $definition ) {
86
87
			if ( !isset( $definition['id'] ) ) {
88
				continue;
89
			}
90
91
			$id = $definition['id'];
92
93
			if ( isset( $properties[$key] ) ) {
94
				$customFixedProperties[$id] = str_replace( array( '___', '__' ), '_', strtolower( $id ) );
95
96
				// Legacy setting `smw_ftp` vs. `smw_fpt`
97
				$fixedPropertyTablePrefix[$id] = 'smw_ftp_sesp';
98
			}
99 1
		}
100 1
	}
101
102 1
	private function addPropertyDefinition( $propertyRegistry, $definition ) {
103
104 1
		$visible = isset( $definition['show'] ) ? $definition['show'] : false;
105 1
		$annotable = false;
106
107 1
		$alias = isset( $definition['alias'] ) ? $definition['alias'] : 'smw-unknown-alias';
108
109
		// If someone screws up the definition format we just fail epically here
110
		// on purpose
111
112 1
		$propertyRegistry->registerProperty(
113 1
			$definition['id'],
114 1
			$definition['type'],
115 1
			$definition['label'],
116 1
			$visible,
117
			$annotable
118 1
		);
119
120 1
		$propertyRegistry->registerPropertyAlias(
121 1
			$definition['id'],
122 1
			Message::get( $alias )
123 1
		);
124
125 1
		$propertyRegistry->registerPropertyAliasByMsgKey(
126 1
			$definition['id'],
127
			$alias
128 1
		);
129
130 1
		$desc = isset( $definition['desc'] ) ? $definition['desc'] : '';
131
132 1
		$propertyRegistry->registerPropertyDescriptionMsgKeyById(
133 1
			$definition['id'],
134
			$desc
135 1
		);
136 1
	}
137
138
}
139