Completed
Push — dev ( 3bce75...1a2bfc )
by Auke
09:55
created

ar_store   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 158
Duplicated Lines 61.39 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 12.83%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 31
c 1
b 0
f 1
lcom 1
cbo 7
dl 97
loc 158
ccs 14
cts 109
cp 0.1283
rs 9.8

18 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 3
A __set() 0 3 1
A __get() 0 5 2
A ls() 0 3 1
A find() 0 3 1
A getSearchPath() 0 6 1
A getSearchQuery() 0 3 1
A get() 7 7 1
A parents() 0 5 1
A exists() 8 8 1
A currentSite() 13 13 3
A parentSite() 13 13 3
A currentSection() 13 13 3
A parentSection() 13 13 3
A currentProject() 8 8 2
A parentProject() 8 8 2
A makePath() 7 7 1
A makeRealPath() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

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

Loading history...
2
3
	ar_pinp::allow('ar_store');
4
	ar_pinp::allow('ar_storeFind');
5
	ar_pinp::allow('ar_storeGet');
6
	ar_pinp::allow('ar_storeParents');
7
8
	class ar_store extends arBase {
9
		static public $rememberShortcuts = true;
10
		static public $searchObject = false;
11
12
		public static function configure( $option, $value ) {
13
			switch ($option) {
14
				case 'rememberShortcuts' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
15
					self::$rememberShortcuts = $value;
16
				break;
17
				case 'searchObject' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
18
					self::$searchObject = $value;
19
				break;
20
			}
21
		}
22
23
		public function __set( $name, $value ) {
24
			ar_store::configure( $name, $value );
25
		}
26
27
		public function __get( $name ) {
28
			if ( isset( ar_store::${$name} ) ) {
29
				return ar_store::${$name};
30
			}
31
		}
32
33
		public static function ls() {
34
			return new ar_storeList( ar::context()->getPath() );
35
		}
36
37 4
		public static function find( $query = "" ) {
38 4
			return new ar_storeFind( ar::context()->getPath(), $query);
39
		}
40
41
		public static function getSearchPath( $path ) {
42
			return ar::context()->getPath( array(
43
				'searchObject' => self::$searchObject,
44
				'path' => $path
45
			) );
46
		}
47
48
		public static function getSearchQuery( $query ) {
49
			return ar::context()->getQuery( $query );
50
		}
51
52 136 View Code Duplication
		public static function get( $path = "" ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53 136
			$path = (string) $path;
54 136
			return new ar_storeGet( ar::context()->getPath( array(
55 136
				'rememberShortcuts' => self::$rememberShortcuts,
56 34
				'path' => $path
57 102
			) ) );
58
		}
59
60
		public static function parents() {
61
			return new ar_storeParents( ar::context()->getPath( array(
62
				'rememberShortcuts' => self::$rememberShortcuts
63
			) ) );
64
		}
65
66 View Code Duplication
		public static function exists( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
			global $store;
68
			$path = (string) $path;
69
			return $store->exists( ar::context()->getPath( array(
70
				'skipShortcuts' => true,
71
				'path' => $path
72
			) ) );
73
		}
74
75 View Code Duplication
		public static function currentSite( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
			$path = (string) $path;
77
			$me = ar::context()->getObject();
78
			if ($me) {
79
				if (self::$rememberShortcuts) {
80
					$me->_load('mod_keepurl.php');
81
					$path = pinp_keepurl::_currentsite( $path );
82
				} else {
83
					$path = $me->currentsite( $path );
84
				}
85
			}
86
			return $path;
87
		}
88
89 View Code Duplication
		public static function parentSite( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
			$path = (string) $path;
91
			$me = ar::context()->getObject();
92
			if ($me) {
93
				if (self::$rememberShortcuts) {
94
					$me->_load('mod_keepurl.php');
95
					$path = pinp_keepurl::_currentsite( $path.'../' );
96
				} else {
97
					$path = $me->parentsite( $path );
98
				}
99
			}
100
			return $path;
101
		}
102
103 View Code Duplication
		public static function currentSection( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
			$path = (string) $path;
105
			$me = ar::context()->getObject();
106
			if ($me) {
107
				if (self::$rememberShortcuts) {
108
					$me->_load('mod_keepurl.php');
109
					$path = pinp_keepurl::_currentsection( $path );
110
				} else {
111
					$path = $me->currentsection( $path );
112
				}
113
			}
114
			return $path;
115
		}
116
117 View Code Duplication
		public static function parentSection( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
			$path = (string) $path;
119
			$me = ar::context()->getObject();
120
			if ($me) {
121
				if (self::$rememberShortcuts) {
122
					$me->_load('mod_keepurl.php');
123
					$path = pinp_keepurl::_currentsection( $path.'../' );
124
				} else {
125
					$path = $me->parentsection( $path );
126
				}
127
			}
128
			return $path;
129
		}
130
131 View Code Duplication
		public static function currentProject( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
			$path = (string) $path;
133
			$me = ar::context()->getObject();
134
			if ($me) {
135
				$path = $me->currentproject( $path );
136
			}
137
			return $path;
138
		}
139
140 View Code Duplication
		public static function parentProject( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
			$path = (string) $path;
142
			$me = ar::context()->getObject();
143
			if ($me) {
144
				$path = $me->parentproject( $path );
145
			}
146
			return $path;
147
		}
148
149 View Code Duplication
		public static function makePath( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
			$path = (string) $path;
151
			return ar::context()->getPath( array(
152
				'rememberShortcuts' => self::$rememberShortcuts,
153
				'path' => $path
154
			) );
155
		}
156
157 136 View Code Duplication
		public static function makeRealPath( $path = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158 136
			$path = (string) $path;
159 136
			return ar::context()->getPath( array(
160 136
				'skipShortcuts' => true,
161 34
				'path' => $path
162 102
			) );
163
		}
164
165
	}
166
167
	class ar_storeFind extends arBase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
168
169
		var $limit = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $limit.

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...
170
		var $offset = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $offset.

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...
171
		var $order = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $order.

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...
172
		var $query = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $query.

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...
173
		var $path = '/';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $path.

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...
174
175 4
		public function __construct( $path = '/', $query = '' ) {
176 4
			$this->path = (string)$path;
177 4
			$this->query = (string)$query;
178
179 4
			if ( ar_store::$searchObject ) {
180
				$this->query = ar_store::getSearchQuery( $query );
181
				$this->path = ar_store::getSearchPath( $path );
182
			}
183 4
		}
184
185 4
		public function call( $template, $args = null ) {
186 4
			global $store;
187 4
			if ($template instanceof ar_listExpression_Pattern ) {
188
				$template = ar::listExpression( $this->count() )->pattern( $template );
189
			}
190 4
			if (ar_store::$rememberShortcuts) {
191 4
				$path = ar_store::makeRealPath( $this->path );
192 3
			} else {
193
				$path = $this->path;
194
			}
195 4
			$query = $this->query;
196 4
			if ($this->order) {
197
				$query .= ' order by '.$this->order;
198
			}
199 4
			$result = $store->call( $template, $args, $store->find( $path, $query, $this->limit, $this->offset), array( 'usePathAsKey' => true ) );
200 4
			return $result;
201
		}
202
203
		public function count() {
204
			global $store;
205
			if (ar_store::$rememberShortcuts) {
206
				$path = ar_store::makeRealPath( $this->path );
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
207
			} else {
208
				$path = $this->path;
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
209
			}
210
			return $store->count_find( $this->path, $this->query, $this->limit, $this->offset );
211
		}
212
213
		public function limit( $limit ) {
214
			$clone = clone $this;
215
			$clone->limit = $limit;
216
			return $clone;
217
		}
218
219
		public function offset( $offset ) {
220
			$clone = clone $this;
221
			$clone->offset = $offset;
222
			return $clone;
223
		}
224
225
		public function order( $order ) {
226
			$clone = clone $this;
227
			$clone->order = $order;
228
			return $clone;
229
		}
230
231
	}
232
233
	class ar_storeList extends ar_storeFind {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
234
235
		public function __construct( $path ) {
236
			parent::__construct( $path, "object.parent = '" . $path . "'" );
237
		}
238
239
	}
240
241
	class ar_storeGet extends arBase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
242
243
		protected $path = '';
244
245 136
		public function __construct( $path ) {
246 136
			$this->path = (string)$path;
247 136
		}
248
249
		public function find( $query = "" ) {
250
			return new ar_storeFind( $this->path, $query );
251
		}
252
253
		public function ls() {
254
			return new ar_storeList( $this->path );
255
		}
256
257 136
		public function call( $template, $args = null ) {
258 136
			global $store;
259 136
			if ( $template instanceof ar_listExpression_Pattern ) {
260
				$template = ar::listExpression( 1 )->pattern( $template );
261
			}
262 136
			if ( ar_store::$rememberShortcuts ) {
263 136
				$path = ar_store::makeRealPath( $this->path );
264 102
			} else {
265
				$path = $this->path;
266
			}
267 136
			return $store->call( $template, $args, $store->get( $path ), array( 'usePathAsKey' => true ) );
268
		}
269
270
		public function parents() {
271
			return new ar_storeParents( $this->path );
272
		}
273
274
	}
275
276
	class ar_storeParents extends arBase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
277
278
		protected $path = '';
279
		protected $top = '/';
280
281
		public function __construct( $path = "" ) {
282
			$this->path	= (string)$path;
283
		}
284
285
		public function call( $template, $args = null ) {
286
			global $store;
287
			if ( $template instanceof ar_listExpression_Pattern ) {
288
				$template = ar::listExpression( $this->count() )->pattern( $template );
289
			}
290
			if ( ar_store::$rememberShortcuts) {
291
				$path     = ar_store::makePath( $this->path );
292
				$realpath = ar_store::makeRealPath( $this->path );
293
				if ($realpath != $path ) {
294
					// must do a call for each seperate path.
295
					$list   = array();
296
					$parent = $path;
297
					while ( $realpath != $this->top && $parent != $this->top && end( $list ) != $realpath ) {
298
						$list[$parent] = $realpath;
299
						$parent        = ar_store::makePath( $parent . '../' );
300
						$realpath      = ar_store::makeRealPath( $parent );
301
					}
302
					if ( ( $realpath == $this->top ) || ( $parent == $this->top ) ) {
303
						$list[$parent] = $realpath;
304
					}
305
					$list = array_reverse( $list );
306
					$result = array();
307
					foreach ( $list as $virtualpath => $path ) {
308
						$result[$virtualpath] = current( $store->call( $template, $args,
309
							$store->get( $path ),
310
							array(
311
								'usePathAsKey' => true
312
							)
313
						) );
314
					}
315
					return $result;
316
				}
317
			}
318
			return $store->call( $template, $args,
319
				$store->parents( $this->path, $this->top ),
320
				array( 'usePathAsKey' => true )
321
			);
322
		}
323
324
		public function count() {
325
			global $store;
326
			return $store->count( $store->parents( $this->path, $this->top ) );
327
		}
328
329
		public function top( $top = "/" ) {
330
			$clone = clone $this;
331
			$clone->top = $top;
332
			return $clone;
333
		}
334
335
	}
336