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

ar_urlQuery   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 70
Duplicated Lines 17.14 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%
Metric Value
wmc 17
lcom 1
cbo 3
dl 12
loc 70
ccs 0
cts 54
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __setQuery() 0 5 2
A __call() 12 12 3
A __get() 0 3 1
A __set() 0 3 1
A getvar() 0 3 1
A putvar() 0 3 1
A __toString() 0 3 1
A import() 0 3 1
A offsetExists() 0 3 1
A offsetGet() 0 3 1
A offsetSet() 0 3 1
A offsetUnset() 0 3 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 7 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_url');
4
	ar_pinp::allow( 'ar_urlQuery' );
5
	use \arc\url;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, url.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
	class ar_url extends arBase implements arKeyValueStoreInterface {
8
9
		private $url, $query;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
10
11
		public function __construct( $url ) {
12
			$query = new \arc\url\PHPQuery();
13
			$this->url = new \arc\url\Url($url, $query);
14
			$this->query = new ar_urlQuery($query);
15
		}
16
17
		public function __get($var) {
18
			if ($var=='password') {
19
				$var = 'pass';
20
			}
21
			if ($var=='query') {
22
				return $this->query;
23
			} else if ( isset( $this->url->{$var} ) ) {
24
				return $this->url->{$var};
25
			} else {
26
				return null;
27
			}
28
		}
29
30
		public function __set($var, $value) {
31
			switch($var) {
32
				case 'query' :
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...
33
					if (is_string($value)) {
34
						$this->url->query = $value;
35
						$this->query = new ar_urlQuery($this->url->query);
36
					} else if ($value instanceof ar_urlQuery) {
37
						$this->url->query = (string) $value;
38
					} else if (is_object($value) && method_exists($value, '__toString') ) {
39
						$this->url->query = (string)$value;
40
					}
41
				break;
42
				case 'password' :
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...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
43
					$var = 'pass';
44
				case 'path' :
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...
45
				case 'scheme':
46
				case 'host' :
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...
47
				case 'port' :
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...
48
				case 'user' :
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...
49
				case 'pass' :
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...
50
				case 'fragment' :
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...
51
					return $this->url->{$var} = $value;
52
				break;
53
			}
54
		}
55
56
		public function __toString() {
57
			return (string)$this->url;
58
		}
59
60
		public function getvar( $name ) {
61
			return $this->query->$name;
62
		}
63
64
		public function putvar( $name, $value ) {
65
			$this->query->{$name} = $value;
66
		}
67
68
		public function import( $values ) {
69
			$this->query->import( $values );
70
		}
71
72
	}
73
74
	class ar_urlQuery implements arKeyValueStoreInterface, ArrayAccess /*, ArrayAcces, IteratorAggregate, .. */ {
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...
75
		private $query;
76
77
		public function __construct( $query ) {
78
			if ( $query instanceof \arc\url\Query ){
79
				$this->query  = $query;
80
			} else {
81
				$this->query = new \arc\url\PHPQuery($query);
82
			}
83
		}
84
85
		public function __setQuery($url, $query) {
86
			if ( $url->query === $this->query ) {
87
				$url->query = $this->query = $query->query;
88
			}
89
		}
90
91 View Code Duplication
		public function __call( $name, $arguments ) {
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...
92
			if (($name[0]==='_')) {
93
				$realName = substr($name, 1);
94
				if (ar_pinp::isAllowed($this, $realName)) {
95
					return call_user_func_array(array($this, $realName), $arguments);
96
				} else {
97
					trigger_error("Method $realName not found in class ".get_class($this), E_USER_WARNING);
98
				}
99
			} else {
100
				trigger_error("Method $name not found in class ".get_class($this), E_USER_WARNING);
101
			}
102
		}
103
104
		public function __get( $name ) {
105
			return $this->query->{$name};
106
		}
107
108
		public function __set( $name, $value ) {
109
			$this->query->{$name} = $value;
110
		}
111
112
		public function getvar( $name ) {
113
			return $this->query->{$name};
114
		}
115
116
		public function putvar( $name, $value ) {
117
			$this->query->{$name} = $value;
118
		}
119
120
		public function __toString() {
121
			return (string)$this->query;
122
		}
123
124
		public function import( $values ) {
125
			$this->query->import( $values );
126
		}
127
128
		public function offsetExists($name){
129
			return isset($this->query->{$name});
130
		}
131
132
		public function offsetGet($name) {
133
			return $this->query->{$name};
134
		}
135
136
		public function offsetSet($name, $value) {
137
			$this->query->{$name} = $value;
138
		}
139
		public function offsetUnset($name) {
140
			unset($this->query->{$name});
141
		}
142
143
	}
144