Completed
Push — master ( 1fe645...db26d9 )
by Auke
50:55 queued 41:34
created

ar_store::getSearchQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
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 6 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 6
					$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 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 {
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...
249
250
		public function __construct( $path ) {
251
			parent::__construct( $path, "object.parent = '" . $path . "'" );
252
		}
253
254
	}
255
256
	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...
257
258
		protected $path = '';
259
260 136
		public function __construct( $path ) {
261 136
			$this->path = (string)$path;
262 136
		}
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 136
		public function call( $template, $args = null ) {
273 136
			global $store;
274 136
			if ( $template instanceof ar_listExpression_Pattern ) {
275
				$template = ar::listExpression( 1 )->pattern( $template );
276
			}
277 136
			if ( ar_store::$rememberShortcuts ) {
278 136
				$path = ar_store::makeRealPath( $this->path );
279 102
			} else {
280
				$path = $this->path;
281
			}
282 136
			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 {
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...
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