RequestDataHolder   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 282
Duplicated Lines 9.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 27
loc 282
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeParameters() 0 4 1
A isParameterValueEmpty() 0 4 1
A clear() 0 9 2
A clearAll() 0 7 2
A get() 0 9 2
A getAll() 0 9 2
A has() 9 9 2
A isValueEmpty() 9 9 2
A remove() 9 9 2
A set() 0 9 2
A registerSource() 0 5 1
A merge() 0 7 2
A getSourceNames() 0 4 1
A __construct() 0 15 4

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
// +---------------------------------------------------------------------------+
4
// | This file is part of the Agavi package.                                   |
5
// | Copyright (c) 2005-2011 the Agavi Project.                                |
6
// |                                                                           |
7
// | For the full copyright and license information, please view the LICENSE   |
8
// | file that was distributed with this source code. You can also view the    |
9
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10
// |   vi: set noexpandtab:                                                    |
11
// |   Local Variables:                                                        |
12
// |   indent-tabs-mode: t                                                     |
13
// |   End:                                                                    |
14
// +---------------------------------------------------------------------------+
15
16
namespace Agavi\Request;
17
18
use Agavi\Util\Inflector;
19
use Agavi\Util\ParameterHolder;
20
use InvalidArgumentException;
21
22
/**
23
 * RequestDataHolder provides methods for retrieving client request
24
 * information parameters.
25
 *
26
 * @package    agavi
27
 * @subpackage request
28
 *
29
 * @author     Dominik del Bondio <[email protected]>
30
 * @copyright  Authors
31
 * @copyright  The Agavi Project
32
 *
33
 * @since      0.11.0
34
 *
35
 * @version    $Id$
36
 */
37
class RequestDataHolder extends ParameterHolder implements ParametersRequestDataHolderInterface
38
{
39
    /**
40
     * @constant   Constant for source name of parameters.
41
     */
42
    const SOURCE_PARAMETERS = 'parameters';
43
44
    /*
45
	 * @var        array An array of source names and references to their data
46
	 *                   containers. Unset again after construction is complete.
47
	 */
48
    private $sources = array();
49
50
    /*
51
	 * @var        array An array of plural source names and their singular forms.
52
	 */
53
    private $sourceNames = array();
54
55
    /**
56
     * Merge in parameters from another request data holder.
57
     *
58
     * @param      RequestDataHolder $other The other request data holder.
59
     *
60
     * @author     David Zülke <[email protected]>
61
     * @since      0.11.0
62
     */
63
    public function mergeParameters(RequestDataHolder $other)
64
    {
65
        $this->setParameters($other->getParameters());
66
    }
67
68
    /**
69
     * Checks if there is a value of a parameter is empty.
70
     *
71
     * @param      string $field The field name.
72
     *
73
     * @return     bool The result.
74
     *
75
     * @author     Dominik del Bondio <[email protected]>
76
     * @since      0.11.0
77
     */
78
    public function isParameterValueEmpty($field)
79
    {
80
        return !$this->hasParameter($field);
81
    }
82
    
83
    /**
84
     * Deletes all fields in a given source.
85
     *
86
     * @param      string $source The name of the source to operate on.
87
     *
88
     * @author     Dominik del Bondio <[email protected]>
89
     * @since      0.11.0
90
     */
91
    public function clear($source)
92
    {
93
        if (isset($this->$source)) {
94
            $funcname = 'clear' . $source;
95
            $this->$funcname();
96
        } else {
97
            throw new InvalidArgumentException('Unknown source ' . $source . ' specified');
98
        }
99
    }
100
101
    /**
102
     * Deletes all fields in all sources.
103
     *
104
     * @author     Dominik del Bondio <[email protected]>
105
     * @since      0.11.0
106
     */
107
    public function clearAll()
108
    {
109
        foreach ($this->sourceNames as $sourceName => $source) {
110
            $funcname = 'clear' . $sourceName;
111
            $this->$funcname();
112
        }
113
    }
114
115
    /**
116
     * Retrieves a field from one of the stored data types.
117
     *
118
     * @param      string $source The name of the source to operate on.
119
     * @param      string $field A field name.
120
     * @param      mixed  $default A default value.
121
     *
122
     * @return     mixed The field value.
123
     *
124
     * @author     Dominik del Bondio <[email protected]>
125
     * @author     David Zülke <[email protected]>
126
     * @since      0.11.0
127
     */
128
    public function &get($source, $field, $default = null)
129
    {
130
        if (isset($this->$source)) {
131
            $funcname = 'get' . $this->sourceNames[$source];
132
            return $this->$funcname($field, $default);
133
        } else {
134
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
135
        }
136
    }
137
138
    /**
139
     * Retrieves all fields of a stored data types.
140
     *
141
     * @param      string $source The name of the source to operate on.
142
     *
143
     * @return     mixed The values.
144
     *
145
     * @author     Dominik del Bondio <[email protected]>
146
     * @author     David Zülke <[email protected]>
147
     * @since      0.11.0
148
     */
149
    public function &getAll($source)
150
    {
151
        if (isset($this->$source)) {
152
            $funcname = 'get' . $source;
153
            return $this->$funcname();
154
        } else {
155
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
156
        }
157
    }
158
159
    /**
160
     * Checks if a field exists.
161
     *
162
     * @param      string $source The name of the source to operate on.
163
     * @param      string $field A field name.
164
     *
165
     * @return     bool The result.
166
     *
167
     * @author     Dominik del Bondio <[email protected]>
168
     * @author     David Zülke <[email protected]>
169
     * @since      0.11.0
170
     */
171 View Code Duplication
    public function has($source, $field)
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...
172
    {
173
        if (isset($this->$source)) {
174
            $funcname = 'has' . $this->sourceNames[$source];
175
            return $this->$funcname($field);
176
        } else {
177
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
178
        }
179
    }
180
181
    /**
182
     * Checks if a field has no value (In web context this would only return true
183
     * when the strings length is 0 or the field is not set.
184
     *
185
     * @param      string $source The name of the source to operate on.
186
     * @param      string $field A field name.
187
     *
188
     * @return     bool The result.
189
     *
190
     * @author     Dominik del Bondio <[email protected]>
191
     * @author     David Zülke <[email protected]>
192
     * @since      0.11.0
193
     */
194 View Code Duplication
    public function isValueEmpty($source, $field)
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...
195
    {
196
        if (isset($this->$source)) {
197
            $funcname = 'is' . $this->sourceNames[$source] . 'ValueEmpty';
198
            return $this->$funcname($field);
199
        } else {
200
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
201
        }
202
    }
203
204
    /**
205
     * Removes a field.
206
     *
207
     * @param      string $source The name of the source to operate on.
208
     * @param      string $field A field name.
209
     *
210
     * @return     mixed The removed value.
211
     *
212
     * @author     Dominik del Bondio <[email protected]>
213
     * @author     David Zülke <[email protected]>
214
     * @since      0.11.0
215
     */
216 View Code Duplication
    public function &remove($source, $field)
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...
217
    {
218
        if (isset($this->$source)) {
219
            $funcname = 'remove' . $this->sourceNames[$source];
220
            return $this->$funcname($field);
221
        } else {
222
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
223
        }
224
    }
225
226
    /**
227
     * Sets a field.
228
     *
229
     * @param      string $source The name of the source to operate on.
230
     * @param      string $field  A field name.
231
     * @param      mixed  $value  A value.
232
     *
233
     * @author     Dominik del Bondio <[email protected]>
234
     * @author     David Zülke <[email protected]>
235
     * @since      0.11.0
236
     */
237
    public function set($source, $field, $value)
238
    {
239
        if (isset($this->$source)) {
240
            $funcname = 'set' . $this->sourceNames[$source];
241
            $this->$funcname($field, $value);
242
        } else {
243
            throw new InvalidArgumentException('Unknown source ' . $source . 'specified');
244
        }
245
    }
246
    
247
    /**
248
     * Register a source with the holder. Must be called in constructors, and
249
     * prior to calling the parent ctor.
250
     *
251
     * @param      string $name The source name, typically passed using a constant.
252
     * @param      array  $holder The variable that will hold the data for the source.
253
     *
254
     * @author     David Zülke <[email protected]>
255
     * @since      0.11.0
256
     */
257
    final protected function registerSource($name, array &$holder)
258
    {
259
        $this->sources[$name] =& $holder;
260
        $this->sourceNames[$name] = Inflector::singularize($name);
261
    }
262
    
263
    /**
264
     * Merge in another request data holder.
265
     *
266
     * This method calls mergeSourcename for each source.
267
     *
268
     * @param      RequestDataHolder $other The other request data holder.
269
     *
270
     * @author     David Zülke <[email protected]>
271
     * @since      0.11.0
272
     */
273
    public function merge(RequestDataHolder $other)
274
    {
275
        foreach (array_keys($this->sourceNames) as $source) {
276
            $fn = 'merge' . $source; // plural form!
277
            $this->$fn($other);
278
        }
279
    }
280
    
281
    /**
282
     * Returns all the registered source names.
283
     *
284
     * @return     array A list of source names.
285
     *
286
     * @author     Dominik del Bondio <[email protected]>
287
     * @since      0.11.0
288
     */
289
    final public function getSourceNames()
290
    {
291
        return array_keys($this->sourceNames);
292
    }
293
294
    /**
295
     * Constructor
296
     *
297
     * @param      array $data An associative array of request data source names and
298
     *                   data arrays.
299
     *
300
     * @author     David Zülke <[email protected]>
301
     * @since      0.11.0
302
     */
303
    public function __construct(array $data = array())
304
    {
305
        $this->registerSource(self::SOURCE_PARAMETERS, $this->parameters);
306
        
307
        foreach ($this->sources as $name => &$container) {
308
            if (isset($data[$name]) && is_array($data[$name])) {
309
                $container = $data[$name];
310
            } else {
311
                $container = array();
312
            }
313
        }
314
        
315
        // unset it to clean up references that otherwise would mess up cloning
316
        unset($this->sources);
317
    }
318
}
319