AbstractQuery   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 12.03 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 0
cbo 0
dl 16
loc 133
ccs 43
cts 43
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 9 2
A fromString() 0 7 1
A __set() 7 7 2
A __get() 9 9 2
A __isset() 0 9 2
A __unset() 0 4 1
A __toString() 0 4 1
A toString() 0 4 1
A toArray() 0 15 2

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
 * This file is part of the Uri package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Uri;
9
10
/**
11
 * Abstract query component class.
12
 *
13
 * @package GravityMedia\Uri
14
 */
15
abstract class AbstractQuery
16
{
17
    /**
18
     * Create query object from array.
19
     *
20
     * @param array $array
21
     *
22
     * @return static
23
     */
24 2
    public static function fromArray(array $array = [])
25
    {
26 2
        $query = new static();
27 2
        foreach ($array as $argument => $value) {
28 2
            $query->__set($argument, $value);
29 1
        }
30
31 2
        return $query;
32
    }
33
34
    /**
35
     * Create query object from string.
36
     *
37
     * @param string $string
38
     *
39
     * @return static
40
     */
41 2
    public static function fromString($string)
42
    {
43 2
        $array = [];
44 2
        parse_str($string, $array);
45
46 2
        return static::fromArray($array);
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type null; however, GravityMedia\Uri\AbstractQuery::fromArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
47
    }
48
49
    /**
50
     * Set a query argument.
51
     *
52
     * @param string $argument
53
     * @param mixed  $value
54
     */
55 4 View Code Duplication
    public function __set($argument, $value)
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...
56
    {
57 4
        $setter = 'set' . str_replace('_', '', $argument);
58 4
        if (is_callable([$this, $setter])) {
59 4
            call_user_func([$this, $setter], $value);
60 2
        }
61 4
    }
62
63
    /**
64
     * Get a query argument.
65
     *
66
     * @param string $argument
67
     *
68
     * @return mixed
69
     */
70 8 View Code Duplication
    public function __get($argument)
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...
71
    {
72 8
        $getter = 'get' . str_replace('_', '', $argument);
73 8
        if (is_callable([$this, $getter])) {
74 6
            return call_user_func([$this, $getter]);
75
        }
76
77 2
        return null;
78
    }
79
80
    /**
81
     * Test if a query argument is NULL.
82
     *
83
     * @param string $argument
84
     *
85
     * @return bool
86
     */
87 4
    public function __isset($argument)
88
    {
89 4
        $getter = 'get' . str_replace('_', '', $argument);
90 4
        if (!method_exists($this, $getter)) {
91 2
            return false;
92
        }
93
94 2
        return null !== $this->__get($argument);
95
    }
96
97
    /**
98
     * Set a query argument to NULL.
99
     *
100
     * @param string $argument
101
     */
102 2
    public function __unset($argument)
103
    {
104 2
        $this->__set($argument, null);
105 2
    }
106
107
    /**
108
     * Return string representation.
109
     *
110
     * @return string
111
     */
112 4
    public function __toString()
113
    {
114 4
        return $this->toString();
115
    }
116
117
    /**
118
     * Convert query into a string representation.
119
     *
120
     * @return string
121
     */
122 4
    public function toString()
123
    {
124 4
        return http_build_query($this->toArray());
125
    }
126
127
    /**
128
     * Convert query into a array representation.
129
     *
130
     * @return array
131
     */
132 4
    public function toArray()
133
    {
134 4
        $array = [];
135 4
        foreach ($this as $argument => $value) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<GravityMedia\Uri\AbstractQuery> is not traversable.
Loading history...
136 4
            $normalizedArgument = preg_replace_callback('/([A-Z])/', function ($letters) {
137 4
                $letter = array_shift($letters);
138
139 4
                return '_' . strtolower($letter);
140 4
            }, $argument);
141
142 4
            $array[$normalizedArgument] = $value;
143 2
        }
144
145 4
        return $array;
146
    }
147
}
148