Completed
Push — dev ( 43499a...2ff4e1 )
by Robbert
122:27 queued 99:46
created

ar_storeFind::call()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.5924
Metric Value
dl 0
loc 17
ccs 10
cts 15
cp 0.6667
rs 9.2
cc 4
eloc 13
nc 8
nop 2
crap 4.5924
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
11
		public static function configure( $option, $value ) {
12
			switch ($option) {
13
				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...
14
					self::$rememberShortcuts = $value;
15
				break;
16
			}
17
		}
18
19
		public function __set( $name, $value ) {
20
			ar_store::configure( $name, $value );
21
		}
22
23
		public function __get( $name ) {
24
			if ( isset( ar_store::${$name} ) ) {
25
				return ar_store::${$name};
26
			}
27
		}
28
29
		public static function ls() {
30
			return new ar_storeList( ar::context()->getPath() );
31
		}
32
33 4
		public static function find( $query = "" ) {
34 4
			return new ar_storeFind( ar::context()->getPath(), $query);
35
		}
36
37 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...
38 136
			$path = (string) $path;
39 136
			return new ar_storeGet( ar::context()->getPath( array(
40 136
				'rememberShortcuts' => self::$rememberShortcuts,
41 68
				'path' => $path
42 68
			) ) );
43
		}
44
45
		public static function parents() {
46
			return new ar_storeParents( ar::context()->getPath( array(
47
				'rememberShortcuts' => self::$rememberShortcuts
48
			) ) );
49
		}
50
51 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...
52
			global $store;
53
			$path = (string) $path;
54
			return $store->exists( ar::context()->getPath( array(
55
				'skipShortcuts' => true,
56
				'path' => $path
57
			) ) );
58
		}
59
60 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...
61
			$path = (string) $path;
62
			$me = ar::context()->getObject();
63
			if ($me) {
64
				if (self::$rememberShortcuts) {
65
					$me->_load('mod_keepurl.php');
66
					$path = pinp_keepurl::_currentsite( $path );
67
				} else {
68
					$path = $me->currentsite( $path );
69
				}
70
			}
71
			return $path;
72
		}
73
74 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...
75
			$path = (string) $path;
76
			$me = ar::context()->getObject();
77
			if ($me) {
78
				if (self::$rememberShortcuts) {
79
					$me->_load('mod_keepurl.php');
80
					$path = pinp_keepurl::_currentsite( $path.'../' );
81
				} else {
82
					$path = $me->parentsite( $path );
83
				}
84
			}
85
			return $path;
86
		}
87
88 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...
89
			$path = (string) $path;
90
			$me = ar::context()->getObject();
91
			if ($me) {
92
				if (self::$rememberShortcuts) {
93
					$me->_load('mod_keepurl.php');
94
					$path = pinp_keepurl::_currentsection( $path );
95
				} else {
96
					$path = $me->currentsection( $path );
97
				}
98
			}
99
			return $path;
100
		}
101
102 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...
103
			$path = (string) $path;
104
			$me = ar::context()->getObject();
105
			if ($me) {
106
				if (self::$rememberShortcuts) {
107
					$me->_load('mod_keepurl.php');
108
					$path = pinp_keepurl::_currentsection( $path.'../' );
109
				} else {
110
					$path = $me->parentsection( $path );
111
				}
112
			}
113
			return $path;
114
		}
115
116 6 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...
117
			$path = (string) $path;
118
			$me = ar::context()->getObject();
119
			if ($me) {
120
				$path = $me->currentproject( $path );
121
			}
122
			return $path;
123 6
		}
124
125 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...
126
			$path = (string) $path;
127
			$me = ar::context()->getObject();
128
			if ($me) {
129
				$path = $me->parentproject( $path );
130
			}
131
			return $path;
132
		}
133
134 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...
135
			$path = (string) $path;
136
			return ar::context()->getPath( array(
137
				'rememberShortcuts' => self::$rememberShortcuts,
138
				'path' => $path
139
			) );
140
		}
141
142 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...
143 136
			$path = (string) $path;
144 136
			return ar::context()->getPath( array(
145 136
				'skipShortcuts' => true,
146 68
				'path' => $path
147 68
			) );
148
		}
149
150
	}
151
152
	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...
153
154
		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...
155
		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...
156
		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...
157
		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...
158
		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...
159
160 4
		public function __construct( $path = '/', $query = '' ) {
161 4
			$this->path = (string)$path;
162 4
			$this->query = (string)$query;
163 4
		}
164
165 4
		public function call( $template, $args = null ) {
166 4
			global $store;
167 4
			if ($template instanceof ar_listExpression_Pattern ) {
168
				$template = ar::listExpression( $this->count() )->pattern( $template );
169
			}
170 4
			if (ar_store::$rememberShortcuts) {
171 4
				$path = ar_store::makeRealPath( $this->path );
172 2
			} else {
173
				$path = $this->path;
174
			}
175 4
			$query = $this->query;
176 4
			if ($this->order) {
177
				$query .= ' order by '.$this->order;
178
			}
179 4
			$result = $store->call( $template, $args, $store->find( $path, $query, $this->limit, $this->offset), array( 'usePathAsKey' => true ) );
180 4
			return $result;
181
		}
182
183
		public function count() {
184
			global $store;
185
			if (ar_store::$rememberShortcuts) {
186
				$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...
187
			} else {
188
				$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...
189
			}
190
			return $store->count_find( $this->path, $this->query, $this->limit, $this->offset );
191
		}
192
193
		public function limit( $limit ) {
194
			$clone = clone $this;
195
			$clone->limit = $limit;
196
			return $clone;
197
		}
198
199
		public function offset( $offset ) {
200
			$clone = clone $this;
201
			$clone->offset = $offset;
202
			return $clone;
203
		}
204
205
		public function order( $order ) {
206
			$clone = clone $this;
207
			$clone->order = $order;
208
			return $clone;
209
		}
210
211
	}
212
213
	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...
214
215
		public function __construct( $path ) {
216
			parent::__construct( $path, "object.parent = '" . $path . "'" );
217
		}
218
219
	}
220
221
	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...
222
223
		protected $path = '';
224
225 136
		public function __construct( $path ) {
226 136
			$this->path = (string)$path;
227 136
		}
228
229
		public function find( $query = "" ) {
230
			return new ar_storeFind( $this->path, $query );
231
		}
232
233
		public function ls() {
234
			return new ar_storeList( $this->path );
235
		}
236
237 136
		public function call( $template, $args = null ) {
238 136
			global $store;
239 136
			if ( $template instanceof ar_listExpression_Pattern ) {
240
				$template = ar::listExpression( 1 )->pattern( $template );
241
			}
242 136
			if ( ar_store::$rememberShortcuts ) {
243 136
				$path = ar_store::makeRealPath( $this->path );
244 68
			} else {
245
				$path = $this->path;
246
			}
247 136
			return $store->call( $template, $args, $store->get( $path ), array( 'usePathAsKey' => true ) );
248
		}
249
250
		public function parents() {
251
			return new ar_storeParents( $this->path );
252
		}
253
254
	}
255
256
	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...
257
258
		protected $path = '';
259
		protected $top = '/';
260
261
		public function __construct( $path = "" ) {
262
			$this->path	= (string)$path;
263
		}
264
265
		public function call( $template, $args = null ) {
266
			global $store;
267
			if ( $template instanceof ar_listExpression_Pattern ) {
268
				$template = ar::listExpression( $this->count() )->pattern( $template );
269
			}
270
			if ( ar_store::$rememberShortcuts) {
271
				$path     = ar_store::makePath( $this->path );
272
				$realpath = ar_store::makeRealPath( $this->path );
273
				if ($realpath != $path ) {
274
					// must do a call for each seperate path.
275
					$list   = array();
276
					$parent = $path;
277
					while ( $realpath != $this->top && $parent != $this->top && end( $list ) != $realpath ) {
278
						$list[$parent] = $realpath;
279
						$parent        = ar_store::makePath( $parent . '../' );
280
						$realpath      = ar_store::makeRealPath( $parent );
281
					}
282
					if ( ( $realpath == $this->top ) || ( $parent == $this->top ) ) {
283
						$list[$parent] = $realpath;
284
					}
285
					$list = array_reverse( $list );
286
					$result = array();
287
					foreach ( $list as $virtualpath => $path ) {
288
						$result[$virtualpath] = current( $store->call( $template, $args,
289
							$store->get( $path ),
290
							array(
291
								'usePathAsKey' => true
292
							)
293
						) );
294
					}
295
					return $result;
296
				}
297
			}
298
			return $store->call( $template, $args,
299
				$store->parents( $this->path, $this->top ),
300
				array( 'usePathAsKey' => true )
301
			);
302
		}
303
304
		public function count() {
305
			global $store;
306
			return $store->count( $store->parents( $this->path, $this->top ) );
307
		}
308
309
		public function top( $top = "/" ) {
310
			$clone = clone $this;
311
			$clone->top = $top;
312
			return $clone;
313
		}
314
315
	}
316