Completed
Push — master ( 790ba1...b96a42 )
by Auke
121:15 queued 112:29
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 10.37%

Importance

Changes 0
Metric Value
dl 97
loc 158
ccs 14
cts 135
cp 0.1037
rs 9.92
c 0
b 0
f 0
wmc 31
lcom 1
cbo 7

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 currentProject() 8 8 2
A parentProject() 8 8 2
A makePath() 7 7 1
A makeRealPath() 7 7 1
A parentSection() 13 13 3

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
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' :
15
					self::$rememberShortcuts = $value;
16
				break;
17
				case 'searchObject' :
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 1
		public static function find( $query = "" ) {
38 1
			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 34 View Code Duplication
		public static function get( $path = "" ) {
53 34
			$path = (string) $path;
54 34
			return new ar_storeGet( ar::context()->getPath( array(
55 34
				'rememberShortcuts' => self::$rememberShortcuts,
56
				'path' => $path
57 34
			) ) );
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 = '' ) {
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 = '' ) {
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 = '' ) {
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 = '' ) {
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 3 View Code Duplication
		public static function parentSection( $path = '' ) {
118
			$path = (string) $path;
119
			$me = ar::context()->getObject();
120
			if ($me) {
121
				if (self::$rememberShortcuts) {
122
					$me->_load('mod_keepurl.php');
123 3
					$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 = '' ) {
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 = '' ) {
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 = '' ) {
150
			$path = (string) $path;
151
			return ar::context()->getPath( array(
152
				'rememberShortcuts' => self::$rememberShortcuts,
153
				'path' => $path
154
			) );
155
		}
156
157 34 View Code Duplication
		public static function makeRealPath( $path = '' ) {
158 34
			$path = (string) $path;
159 34
			return ar::context()->getPath( array(
160 34
				'skipShortcuts' => true,
161
				'path' => $path
162 34
			) );
163
		}
164
165
	}
166
167
	class ar_storeFind extends arBase {
168
169
		var $limit = 0;
170
		var $offset = 0;
171
		var $order = '';
172
		var $query = '';
173
		var $path = '/';
174
175 1
		public function __construct( $path = '/', $query = '' ) {
176 1
			$this->path = (string)$path;
177 1
			$this->query = (string)$query;
178
179 1
			if ( ar_store::$searchObject ) {
180
				$this->query = ar_store::getSearchQuery( $query );
181
				$this->path = ar_store::getSearchPath( $path );
182
			}
183 1
		}
184
185 1
		public function call( $template, $args = null ) {
186 1
			global $store;
187 1
			if ($template instanceof ar_listExpression_Pattern ) {
188
				$template = ar::listExpression( $this->count() )->pattern( $template );
189
			}
190 1
			if (ar_store::$rememberShortcuts) {
191 1
				$path = ar_store::makeRealPath( $this->path );
192 1
			} else {
193
				$path = $this->path;
194
			}
195 1
			$query = $this->query;
196 1
			if ($this->order) {
197
				$query .= ' order by '.$this->order;
198
			}
199 1
			$result = $store->call( $template, $args, $store->find( $path, $query, $this->limit, $this->offset), array( 'usePathAsKey' => true ) );
200 1
			return $result;
201
		}
202
203
		public function info() {
204
			global $store;
205
			if (ar_store::$rememberShortcuts) {
206
				$path = ar_store::makeRealPath( $this->path );
207
			} else {
208
				$path = $this->path;
209
			}
210
			$query = $this->query;
211
			if ($this->order) {
212
				$query .= ' order by '.$this->order;
213
			}
214
			$result = $store->info( $store->find( $path, $query, $this->limit, $this->offset), array( 'usePathAsKey' => true ));
215
			return $result;
216
		}
217
218
		public function count() {
219
			global $store;
220
			if (ar_store::$rememberShortcuts) {
221
				$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...
222
			} else {
223
				$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...
224
			}
225
			return $store->count_find( $this->path, $this->query, $this->limit, $this->offset );
226
		}
227
228
		public function limit( $limit ) {
229
			$clone = clone $this;
230
			$clone->limit = $limit;
231
			return $clone;
232
		}
233
234
		public function offset( $offset ) {
235
			$clone = clone $this;
236
			$clone->offset = $offset;
237
			return $clone;
238
		}
239
240
		public function order( $order ) {
241
			$clone = clone $this;
242
			$clone->order = $order;
243
			return $clone;
244
		}
245
246
	}
247
248
	class ar_storeList extends ar_storeFind {
249
250
		public function __construct( $path ) {
251
			parent::__construct( $path, "object.parent = '" . $path . "'" );
252
		}
253
254
	}
255
256
	class ar_storeGet extends arBase {
257
258
		protected $path = '';
259
260 34
		public function __construct( $path ) {
261 34
			$this->path = (string)$path;
262 34
		}
263
264
		public function find( $query = "" ) {
265
			return new ar_storeFind( $this->path, $query );
266
		}
267
268
		public function ls() {
269
			return new ar_storeList( $this->path );
270
		}
271
272 34
		public function call( $template, $args = null ) {
273 34
			global $store;
274 34
			if ( $template instanceof ar_listExpression_Pattern ) {
275
				$template = ar::listExpression( 1 )->pattern( $template );
276
			}
277 34
			if ( ar_store::$rememberShortcuts ) {
278 34
				$path = ar_store::makeRealPath( $this->path );
279 34
			} else {
280
				$path = $this->path;
281
			}
282 34
			return $store->call( $template, $args, $store->get( $path ), array( 'usePathAsKey' => true ) );
283
		}
284
285
		public function info() {
286
			global $store;
287
			if ( ar_store::$rememberShortcuts ) {
288
				$path = ar_store::makeRealPath( $this->path );
289
			} else {
290
				$path = $this->path;
291
			}
292
			return $store->info( $store->get( $path ), array( 'usePathAsKey' => true ));
293
		}
294
295
		public function parents() {
296
			return new ar_storeParents( $this->path );
297
		}
298
299
	}
300
301
	class ar_storeParents extends arBase {
302
303
		protected $path = '';
304
		protected $top = '/';
305
306
		public function __construct( $path = "" ) {
307
			$this->path	= (string)$path;
308
		}
309
310
		public function call( $template, $args = null ) {
311
			global $store;
312
			if ( $template instanceof ar_listExpression_Pattern ) {
313
				$template = ar::listExpression( $this->count() )->pattern( $template );
314
			}
315 View Code Duplication
			if ( ar_store::$rememberShortcuts) {
316
				$path     = ar_store::makePath( $this->path );
317
				$realpath = ar_store::makeRealPath( $this->path );
318
				if ($realpath != $path ) {
319
					// must do a call for each seperate path.
320
					$list   = array();
321
					$parent = $path;
322
					while ( $realpath != $this->top && $parent != $this->top && end( $list ) != $realpath ) {
323
						$list[$parent] = $realpath;
324
						$parent        = ar_store::makePath( $parent . '../' );
325
						$realpath      = ar_store::makeRealPath( $parent );
326
					}
327
					if ( ( $realpath == $this->top ) || ( $parent == $this->top ) ) {
328
						$list[$parent] = $realpath;
329
					}
330
					$list = array_reverse( $list );
331
					$result = array();
332
					foreach ( $list as $virtualpath => $path ) {
333
						$result[$virtualpath] = current( $store->call( $template, $args,
334
							$store->get( $path ),
335
							array(
336
								'usePathAsKey' => true
337
							)
338
						) );
339
					}
340
					return $result;
341
				}
342
			}
343
			return $store->call( $template, $args,
344
				$store->parents( $this->path, $this->top ),
345
				array( 'usePathAsKey' => true )
346
			);
347
		}
348
349
		public function info(){
350
			global $store;
351
352 View Code Duplication
			if ( ar_store::$rememberShortcuts) {
353
				$path     = ar_store::makePath( $this->path );
354
				$realpath = ar_store::makeRealPath( $this->path );
355
				if ($realpath != $path ) {
356
					// must do a call for each seperate path.
357
					$list   = array();
358
					$parent = $path;
359
					while ( $realpath != $this->top && $parent != $this->top && end( $list ) != $realpath ) {
360
						$list[$parent] = $realpath;
361
						$parent        = ar_store::makePath( $parent . '../' );
362
						$realpath      = ar_store::makeRealPath( $parent );
363
					}
364
					if ( ( $realpath == $this->top ) || ( $parent == $this->top ) ) {
365
						$list[$parent] = $realpath;
366
					}
367
					$list = array_reverse( $list );
368
					$result = array();
369
					foreach ( $list as $virtualpath => $path ) {
370
						$result[$virtualpath] = current( $store->info(
371
							$store->get( $path )
372
						));
373
					}
374
					return $result;
375
				}
376
			}
377
378
			return $store->info(
379
				$store->parents( $this->path, $this->top ), array( 'usePathAsKey' => true )
380
			);
381
		}
382
383
		public function count() {
384
			global $store;
385
			return $store->count( $store->parents( $this->path, $this->top ) );
386
		}
387
388
		public function top( $top = "/" ) {
389
			$clone = clone $this;
390
			$clone->top = $top;
391
			return $clone;
392
		}
393
394
	}
395