Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

Call::elseError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Call.php - The Jaxon Call
5
 *
6
 * This class is used to create js ajax requests to callable classes and functions.
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
 * @author Jared White
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
10
 * @author J. Max Wilson
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
11
 * @author Joseph Woolley
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
12
 * @author Steffen Konerow
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 * @author Thierry Feuzeu <[email protected]>
14
 * @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
15
 * @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White  & J. Max Wilson
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-core
19
 */
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...
20
21
namespace Jaxon\Request\Call;
22
23
use function array_map;
24
use function func_get_args;
25
26
class Call extends JsCall
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Call
Loading history...
27
{
28
    /**
29
     * The type of the message to show
30
     *
31
     * @var string
32
     */
33
    private $sMessageType = 'warning';
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
34
35
    /**
36
     * The arguments of the elseShow() call
37
     *
38
     * @var array
39
     */
40
    protected $aMessageArgs = [];
41
42
    /**
43
     * A condition to check before making the call
44
     *
45
     * @var array
46
     */
47
    protected $aCondition = [];
48
49
    /**
50
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
51
     */
52
    protected $bConfirm = false;
53
54
    /**
55
     * The arguments of the confirm() call
56
     *
57
     * @var array
58
     */
59
    protected $aConfirmArgs = [];
60
61
    /**
62
     * Set the message if the condition to the call is not met
63
     *
64
     * The first parameter is the message to show. The second allows inserting data from
65
     * the webpage in the message using positional placeholders.
66
     *
67
     * @param string $sMessageType  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
68
     * @param array $aMessageArgs
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
69
     *
70
     * @return Call
71
     */
72
    private function setMessage(string $sMessageType, array $aMessageArgs): Call
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
73
    {
74
        $this->sMessageType = $sMessageType;
75
        $this->aMessageArgs = array_map(function($xParameter) {
76
            return Parameter::make($xParameter);
77
        }, $aMessageArgs);
78
        return $this;
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * Get the message
83
     *
84
     * @return array
85
     */
86
    protected function getMessage(): array
87
    {
88
        return [
89
            'type' => $this->sMessageType,
90
            'message' => $this->aMessageArgs,
91
        ];
92
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
93
94
    /**
95
     * Show a message if the condition to the call is not met
96
     *
97
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
98
     *
99
     * @return Call
100
     */
101
    public function elseShow(string $sMessage): Call
102
    {
103
        return $this->setMessage('warning', func_get_args());
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Show an information message if the condition to the call is not met
108
     *
109
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
110
     *
111
     * @return Call
112
     */
113
    public function elseInfo(string $sMessage): Call
114
    {
115
        return $this->setMessage('info', func_get_args());
116
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
117
118
    /**
119
     * Show a success message if the condition to the call is not met
120
     *
121
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
122
     *
123
     * @return Call
124
     */
125
    public function elseSuccess(string $sMessage): Call
126
    {
127
        return $this->setMessage('success', func_get_args());
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Show a warning message if the condition to the call is not met
132
     *
133
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
134
     *
135
     * @return Call
136
     */
137
    public function elseWarning(string $sMessage): Call
138
    {
139
        return $this->setMessage('warning', func_get_args());
140
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
141
142
    /**
143
     * Show an error message if the condition to the call is not met
144
     *
145
     * @param string $sMessage  The message to show
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
146
     *
147
     * @return Call
148
     */
149
    public function elseError(string $sMessage): Call
150
    {
151
        return $this->setMessage('error', func_get_args());
152
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
153
154
    /**
155
     * Add a condition to the request
156
     *
157
     * The request is sent only if the condition is true.
158
     *
159
     * @param mixed $xCondition    The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
160
     *
161
     * @return Call
162
     */
163
    public function when($xCondition): Call
164
    {
165
        $this->aCondition = [true, Parameter::make($xCondition)];
166
        return $this;
167
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
168
169
    /**
170
     * Add a condition to the request
171
     *
172
     * The request is sent only if the condition is false.
173
     *
174
     * @param mixed $xCondition    The condition to check
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
175
     *
176
     * @return Call
177
     */
178
    public function unless($xCondition): Call
179
    {
180
        $this->aCondition = [false, Parameter::make($xCondition)];
181
        return $this;
182
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
183
184
    /**
185
     * Check if a value is equal to another before sending the request
186
     *
187
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
188
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
189
     *
190
     * @return Call
191
     */
192
    public function ifeq($xValue1, $xValue2): Call
193
    {
194
        $this->aCondition = ['==', Parameter::make($xValue1), Parameter::make($xValue2)];
195
        return $this;
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * Check if a value is equal to another before sending the request
200
     *
201
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
202
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
203
     *
204
     * @return Call
205
     */
206
    public function ifteq($xValue1, $xValue2): Call
207
    {
208
        $this->aCondition = ['===', Parameter::make($xValue1), Parameter::make($xValue2)];
209
        return $this;
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
211
212
    /**
213
     * Check if a value is not equal to another before sending the request
214
     *
215
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
216
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
217
     *
218
     * @return Call
219
     */
220
    public function ifne($xValue1, $xValue2): Call
221
    {
222
        $this->aCondition = ['!=', Parameter::make($xValue1), Parameter::make($xValue2)];
223
        return $this;
224
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
225
226
    /**
227
     * Check if a value is not equal to another before sending the request
228
     *
229
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
230
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
231
     *
232
     * @return Call
233
     */
234
    public function ifnte($xValue1, $xValue2): Call
235
    {
236
        $this->aCondition = ['!==', Parameter::make($xValue1), Parameter::make($xValue2)];
237
        return $this;
238
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
239
240
    /**
241
     * Check if a value is greater than another before sending the request
242
     *
243
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
244
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
245
     *
246
     * @return Call
247
     */
248
    public function ifgt($xValue1, $xValue2): Call
249
    {
250
        $this->aCondition = ['>', Parameter::make($xValue1), Parameter::make($xValue2)];
251
        return $this;
252
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
253
254
    /**
255
     * Check if a value is greater or equal to another before sending the request
256
     *
257
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
258
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
259
     *
260
     * @return Call
261
     */
262
    public function ifge($xValue1, $xValue2): Call
263
    {
264
        $this->aCondition = ['>=', Parameter::make($xValue1), Parameter::make($xValue2)];
265
        return $this;
266
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
267
268
    /**
269
     * Check if a value is lower than another before sending the request
270
     *
271
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
272
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
273
     *
274
     * @return Call
275
     */
276
    public function iflt($xValue1, $xValue2): Call
277
    {
278
        $this->aCondition = ['<', Parameter::make($xValue1), Parameter::make($xValue2)];
279
        return $this;
280
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
281
282
    /**
283
     * Check if a value is lower or equal to another before sending the request
284
     *
285
     * @param mixed $xValue1    The first value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
286
     * @param mixed $xValue2    The second value to compare
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
287
     *
288
     * @return Call
289
     */
290
    public function ifle($xValue1, $xValue2): Call
291
    {
292
        $this->aCondition = ['<=', Parameter::make($xValue1), Parameter::make($xValue2)];
293
        return $this;
294
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
295
296
    /**
297
     * Add a confirmation question to the request
298
     *
299
     * @param string $sQuestion    The question to ask
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
300
     *
301
     * @return Call
302
     */
303
    public function confirm(string $sQuestion): Call
304
    {
305
        $this->bConfirm = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
306
        $this->aConfirmArgs = array_map(function($xParameter) {
307
            return Parameter::make($xParameter);
308
        }, func_get_args());
309
        return $this;
310
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
311
312
    /**
313
     * Convert this call to array
314
     *
315
     * @return array
316
     */
317
    public function toArray(): array
318
    {
319
        $aCall = parent::toArray();
320
        if($this->bConfirm)
321
        {
322
            $aCall['confirm'] = $this->aConfirmArgs;
323
        }
324
        if(($this->aCondition))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aCondition of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
325
        {
326
            $aCall['condition'] = $this->aCondition;
327
            if(($this->aMessageArgs))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->aMessageArgs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
328
            {
329
                $aCall['else'] = $this->getMessage();
330
            }
331
        }
332
        return $aCall;
333
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
334
335
    /**
336
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
337
     *
338
     * @return ParameterInterface|null
339
     */
340
    private function findPageNumber(): ?ParameterInterface
341
    {
342
        foreach($this->aParameters as $xParameter)
343
        {
344
            if($xParameter->getType() === Parameter::PAGE_NUMBER)
345
            {
346
                return $xParameter;
347
            }
348
        }
349
        return null;
350
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
351
352
    /**
353
     * Check if the request has a parameter of type Parameter::PAGE_NUMBER
354
     *
355
     * @return bool
356
     */
357
    public function hasPageNumber(): bool
358
    {
359
        return $this->findPageNumber() !== null;
360
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
361
362
    /**
363
     * Set a value to the Parameter::PAGE_NUMBER parameter
364
     *
365
     * @param integer $nPageNumber    The current page number
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
366
     *
367
     * @return Call
368
     */
369
    public function setPageNumber(int $nPageNumber): Call
370
    {
371
        /** @var Parameter */
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
372
        $xParameter = $this->findPageNumber();
373
        if($xParameter !== null)
374
        {
375
            $xParameter->setValue($nPageNumber);
0 ignored issues
show
Bug introduced by
The method setValue() does not exist on Jaxon\Request\Call\ParameterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Jaxon\Request\Call\ParameterInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

375
            $xParameter->/** @scrutinizer ignore-call */ 
376
                         setValue($nPageNumber);
Loading history...
376
        }
377
        return $this;
378
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
379
}
380