Passed
Push — v5.x ( 6eb5e2...afe246 )
by Thierry
02:28
created

Parameter   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 325
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 40
eloc 73
dl 0
loc 325
rs 9.2
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsonValueScript() 0 5 1
A getPageNumberScript() 0 3 1
A getType() 0 3 1
A getQuotedValueScript() 0 3 1
C jsonSerialize() 0 28 12
A __toString() 0 3 1
A getQuotedValue() 0 3 1
A setValue() 0 3 1
A toInt() 0 4 1
A make() 0 25 6
A getElementInnerHTMLScript() 0 3 1
A getFormValuesScript() 0 3 1
A __construct() 0 4 1
A getValue() 0 3 1
A getJaxonCall() 0 3 1
A getScript() 0 9 3
A getUnquotedValueScript() 0 3 1
A getInputValueScript() 0 3 1
A getBoolValueScript() 0 3 2
A getCheckedValueScript() 0 3 1
A getNumericValueScript() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Parameter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Parameter, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Parameter.php - A parameter of a Jaxon request
5
 *
6
 * This class is used to create client side requests to the Jaxon functions and callable objects.
7
 *
8
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
9
 * @copyright 2016 Thierry Feuzeu <[email protected]>
10
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
11
 * @link https://github.com/jaxon-php/jaxon-core
12
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
13
14
namespace Jaxon\Request\Js;
15
16
use function is_bool;
17
use function is_numeric;
18
use function is_string;
19
use function json_encode;
20
use function method_exists;
21
use function str_replace;
22
23
class Parameter implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Parameter
Loading history...
24
{
25
    /*
26
     * Request parameters
27
     */
0 ignored issues
show
Coding Style introduced by
Empty line required after block comment
Loading history...
28
    // Specifies that the parameter will consist of an array of form values.
29
    const FORM_VALUES = 'FormValues';
30
    // Specifies that the parameter will contain the value of an input control.
31
    const INPUT_VALUE = 'InputValue';
32
    // Specifies that the parameter will consist of a bool value of a checkbox.
33
    const CHECKED_VALUE = 'CheckedValue';
34
    // Specifies that the parameter value will be the innerHTML value of the element.
35
    const ELEMENT_INNERHTML = 'ElementInnerHTML';
36
    // Specifies that the parameter will be a quoted value (string).
37
    const QUOTED_VALUE = 'QuotedValue';
38
    // Specifies that the parameter will be a bool value (true or false).
39
    const BOOL_VALUE = 'BoolValue';
40
    // Specifies that the parameter will be a numeric, non-quoted value.
41
    const NUMERIC_VALUE = 'NumericValue';
42
    // Specifies that the parameter will be a non-quoted value
43
    // (evaluated by the browsers javascript engine at run time).
44
    const JS_VALUE = 'UnquotedValue';
45
    // Specifies that the parameter is a call to a javascript function
46
    const JS_CALL = 'JsCall';
47
    // Specifies that the parameter must be json encoded
48
    const JSON_VALUE = 'JsonValue';
49
    // Specifies that the parameter will be an integer used to generate pagination links.
50
    const PAGE_NUMBER = 'PageNumber';
51
52
    /**
53
     * The parameter type
54
     *
55
     * @var string
56
     */
57
    protected $sType;
58
59
    /**
60
     * The parameter value
61
     *
62
     * @var mixed
63
     */
64
    protected $xValue;
65
66
    /**
67
     * Convert the parameter value to integer
68
     *
69
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
70
     */
71
    protected $bToInt = false;
72
73
    /**
74
     * The constructor.
75
     *
76
     * @param string $sType    The parameter type
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
77
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
78
     */
79
    public function __construct(string $sType, $xValue)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
80
    {
81
        $this->sType = $sType;
82
        $this->xValue = $xValue;
83
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
84
85
    /**
86
     * Get the parameter type
87
     *
88
     * @return string
89
     */
90
    public function getType(): string
91
    {
92
        return $this->sType;
93
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
94
95
    /**
96
     * Get the parameter value
97
     *
98
     * @return mixed
99
     */
100
    public function getValue()
101
    {
102
        return $this->xValue;
103
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
104
105
    /**
106
     * Set the parameter value
107
     *
108
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
109
     *
110
     * @return void
111
     */
112
    public function setValue($xValue)
113
    {
114
        $this->xValue = $xValue;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * @return ParameterInterface
119
     */
120
    public function toInt(): ParameterInterface
121
    {
122
        $this->bToInt = true;
123
        return $this;
124
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
125
126
    /**
127
     * Create a Parameter instance using the given value
128
     *
129
     * @param mixed $xValue    The parameter value
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
130
     *
131
     * @return ParameterInterface
132
     */
133
    public static function make($xValue): ParameterInterface
134
    {
135
        if($xValue instanceof ParameterInterface)
136
        {
137
            return $xValue;
138
        }
139
        if(is_numeric($xValue))
140
        {
141
            return new Parameter(self::NUMERIC_VALUE, $xValue);
142
        }
143
        if(is_string($xValue))
144
        {
145
            return new Parameter(self::QUOTED_VALUE, $xValue);
146
        }
147
        if(is_bool($xValue))
148
        {
149
            return new Parameter(self::BOOL_VALUE, $xValue);
150
        }
151
        if($xValue instanceof Call)
152
        {
153
            return new Parameter(self::JS_CALL, $xValue);
154
        }
155
        // if(is_array($xValue) || is_object($xValue))
156
        {
157
            return new Parameter(self::JSON_VALUE, $xValue);
158
        }
159
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
160
161
    /**
162
     * Convert to a value to be inserted in an array for json conversion
163
     *
164
     * @return mixed
165
     */
166
    public function jsonSerialize()
167
    {
168
        switch($this->getType())
169
        {
170
        case self::JS_CALL:
171
            return $this->getValue()->toArray();
172
        case self::JS_VALUE:
173
            return [
174
                '_type' => 'expr',
175
                'calls' => [['_type' => 'attr', '_name' => $this->getValue()]],
176
            ];
177
        case self::FORM_VALUES:
178
            return ['_type' => 'form', '_name' => $this->getValue()];
179
        case self::INPUT_VALUE:
180
            return ['_type' => 'input', '_name' => $this->getValue()];
181
        case self::CHECKED_VALUE:
182
            return ['_type' => 'checked', '_name' => $this->getValue()];
183
        case self::ELEMENT_INNERHTML:
184
            return ['_type' => 'html', '_name' => $this->getValue()];
185
        case self::PAGE_NUMBER:
186
            return ['_type' => 'page', '_name' => ''];
187
        case self::QUOTED_VALUE:
188
        case self::BOOL_VALUE:
189
        case self::NUMERIC_VALUE:
190
        case self::JSON_VALUE:
191
        default:
192
            // Return the value as is.
193
            return $this->getValue();
194
        }
195
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
196
197
    /**
198
     * Add quotes to a given value
199
     *
200
     * @param string $sValue    The value to be quoted
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
201
     *
202
     * @return string
203
     */
204
    private function getQuotedValue(string $sValue): string
205
    {
206
        return "'" . $sValue . "'";
207
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
208
209
    /**
210
     * Get a js call to Jaxon with a single parameter
211
     *
212
     * @param string $sFunction    The function name
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
213
     * @param string $sParameter    The function parameter
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
214
     *
215
     * @return string
216
     */
217
    private function getJaxonCall(string $sFunction, string $sParameter): string
218
    {
219
        return 'jaxon.' . $sFunction . '(' . $this->getQuotedValue($sParameter) . ')';
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
221
222
    /**
223
     * Get the script for an array of form values.
224
     *
225
     * @return string
226
     */
227
    protected function getFormValuesScript(): string
228
    {
229
        return $this->getJaxonCall('getFormValues', $this->xValue);
230
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
231
232
    /**
233
     * Get the script for an input control.
234
     *
235
     * @return string
236
     */
237
    protected function getInputValueScript(): string
238
    {
239
        return $this->getJaxonCall('$', $this->xValue) . '.value';
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * Get the script for a bool value of a checkbox.
244
     *
245
     * @return string
246
     */
247
    protected function getCheckedValueScript(): string
248
    {
249
        return $this->getJaxonCall('$', $this->xValue) . '.checked';
250
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
251
252
    /**
253
     * Get the script for the innerHTML value of the element.
254
     *
255
     * @return string
256
     */
257
    protected function getElementInnerHTMLScript(): string
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
258
    {
259
        return $this->getJaxonCall('$', $this->xValue) . '.innerHTML';
260
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
261
262
    /**
263
     * Get the script for a quoted value (string).
264
     *
265
     * @return string
266
     */
267
    protected function getQuotedValueScript(): string
268
    {
269
        return $this->getQuotedValue(addslashes($this->xValue));
270
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
271
272
    /**
273
     * Get the script for a bool value (true or false).
274
     *
275
     * @return string
276
     */
277
    protected function getBoolValueScript(): string
278
    {
279
        return ($this->xValue) ? 'true' : 'false';
280
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
281
282
    /**
283
     * Get the script for a numeric, non-quoted value.
284
     *
285
     * @return string
286
     */
287
    protected function getNumericValueScript(): string
288
    {
289
        return (string)$this->xValue;
290
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
291
292
    /**
293
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
294
     *
295
     * @return string
296
     */
297
    protected function getUnquotedValueScript(): string
298
    {
299
        return (string)$this->xValue;
300
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
301
302
    /**
303
     * Get the script for a non-quoted value (evaluated by the browsers javascript engine at run time).
304
     *
305
     * @return string
306
     */
307
    protected function getJsonValueScript(): string
308
    {
309
        // Unable to use double quotes here because they cannot be handled on client side.
310
        // So we are using simple quotes even if the Json standard recommends double quotes.
311
        return str_replace('"', "'", json_encode($this->xValue, JSON_HEX_APOS | JSON_HEX_QUOT));
312
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
313
314
    /**
315
     * Get the script for an integer used to generate pagination links.
316
     *
317
     * @return string
318
     */
319
    protected function getPageNumberScript(): string
320
    {
321
        return (string)$this->xValue;
322
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
323
324
    /**
325
     * Generate the javascript code.
326
     *
327
     * @return string
328
     */
329
    public function getScript(): string
330
    {
331
        $sMethodName = 'get' . $this->sType . 'Script';
332
        if(!method_exists($this, $sMethodName))
333
        {
334
            return '';
335
        }
336
        $sScript = $this->$sMethodName();
337
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
338
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
339
340
    /**
341
     * Convert this call to string
342
     *
343
     * @return string
344
     */
345
    public function __toString()
346
    {
347
        return $this->getScript();
348
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
349
}
350