Passed
Push — main ( 18c043...2ebc4f )
by Thierry
05:47 queued 40s
created

CallableObjectOptions::setSeparator()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * CallableObjectOptions.php
5
 *
6
 * Options of a callable object.
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 Thierry Feuzeu <[email protected]>
10
 * @copyright 2024 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
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...
14
15
namespace Jaxon\Plugin\Request\CallableClass;
16
17
use function array_merge;
18
use function array_unique;
19
use function is_array;
20
use function is_string;
21
use function substr;
22
use function trim;
23
24
class CallableObjectOptions
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableObjectOptions
Loading history...
25
{
26
    /**
27
     * Check if the js code for this object must be generated
28
     *
29
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
30
     */
31
    private $bExcluded = false;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
32
33
    /**
34
     * The character to use as separator in javascript class names
35
     *
36
     * @var string
37
     */
38
    private $sSeparator = '.';
39
40
    /**
41
     * A list of methods of the user registered callable object the library must not export to javascript
42
     *
43
     * @var array
44
     */
45
    private $aProtectedMethods = [];
46
47
    /**
48
     * A list of methods to call before processing the request
49
     *
50
     * @var array
51
     */
52
    private $aBeforeMethods = [];
53
54
    /**
55
     * A list of methods to call after processing the request
56
     *
57
     * @var array
58
     */
59
    private $aAfterMethods = [];
60
61
    /**
62
     * The javascript class options
63
     *
64
     * @var array
65
     */
66
    private $aJsOptions = [];
67
68
    /**
69
     * The DI options
70
     *
71
     * @var array
72
     */
73
    private $aDiOptions = [];
74
75
    /**
76
     * The constructor
77
     *
78
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     * @param array $aAnnotations
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     */
81
    public function __construct(array $aOptions, array $aAnnotations)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
82
    {
83
        [$bExcluded, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations;
84
        $this->bExcluded = $bExcluded || (bool)($aOptions['excluded'] ?? false);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 41 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...
85
        if($this->bExcluded)
86
        {
87
            return;
88
        }
89
90
        $this->setSeparator($aOptions['separator']);
91
        $this->addProtectedMethods($aOptions['protected']);
92
        $this->addProtectedMethods($aAnnotationProtected);
93
94
        foreach($aOptions['functions'] as $sNames => $aFunctionOptions)
95
        {
96
            $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list.
97
            foreach($aFunctionNames as $sFunctionName)
98
            {
99
                $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
100
            }
101
        }
102
        foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions)
103
        {
104
            $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
105
        }
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * @param string $sSeparator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     *
111
     * @return void
112
     */
113
    private function setSeparator(string $sSeparator)
114
    {
115
        if($sSeparator === '_' || $sSeparator === '.')
116
        {
117
            $this->sSeparator = $sSeparator;
118
        }
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * @param mixed $xMethods
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
123
     *
124
     * @return void
125
     */
126
    private function addProtectedMethods($xMethods)
127
    {
128
        if(!is_array($xMethods))
129
        {
130
            $this->aProtectedMethods[trim((string)$xMethods)] = true;
131
            return;
132
        }
133
        foreach($xMethods as $sMethod)
134
        {
135
            $this->aProtectedMethods[trim((string)$sMethod)] = true;
136
        }
137
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
138
139
    /**
140
     * Check if the js code for this object must be generated
141
     *
142
     * @return bool
143
     */
144
    public function excluded(): bool
145
    {
146
        return $this->bExcluded;
147
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
148
149
    /**
150
     * @return string
151
     */
152
    public function separator(): string
153
    {
154
        return $this->sSeparator;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * @param string $sMethodName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
159
     *
160
     * @return bool
161
     */
162
    public function isProtectedMethod(string $sMethodName): bool
163
    {
164
        return isset($this->aProtectedMethods[$sMethodName]) || isset($this->aProtectedMethods['*']);
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * @return array
169
     */
170
    public function beforeMethods(): array
171
    {
172
        return $this->aBeforeMethods;
173
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
174
175
    /**
176
     * @return array
177
     */
178
    public function afterMethods(): array
179
    {
180
        return $this->aAfterMethods;
181
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
182
183
    /**
184
     * @return array
185
     */
186
    public function diOptions(): array
187
    {
188
        return $this->aDiOptions;
189
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
190
191
    /**
192
     * @return array
193
     */
194
    public function jsOptions(): array
195
    {
196
        return $this->aJsOptions;
197
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
198
199
    /**
200
     * Set hook methods
201
     *
202
     * @param array $aHookMethods    The array of hook methods
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
203
     * @param string|array $xValue    The value of the configuration option
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 4 found
Loading history...
204
     *
205
     * @return void
206
     */
207
    private function setHookMethods(array &$aHookMethods, $xValue)
208
    {
209
        foreach($xValue as $sCalledMethod => $xMethodToCall)
210
        {
211
            if(is_array($xMethodToCall))
212
            {
213
                $aHookMethods[$sCalledMethod] = $xMethodToCall;
214
            }
215
            elseif(is_string($xMethodToCall))
216
            {
217
                $aHookMethods[$sCalledMethod] = [$xMethodToCall];
218
            }
219
        }
220
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
221
222
    /**
223
     * @param array $aDiOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
224
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
225
    private function addDiOption(array $aDiOptions)
226
    {
227
        $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions);
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
229
230
    /**
231
     * Set configuration options / call options for each method
232
     *
233
     * @param string $sName    The name of the configuration option
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
234
     * @param string|array $xValue    The value of the configuration option
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
235
     *
236
     * @return void
237
     */
238
    public function addOption(string $sName, $xValue)
239
    {
240
        switch($sName)
241
        {
242
        // Set the separator
243
        case 'separator':
244
            $this->setSeparator((string)$xValue);
245
            break;
246
        case 'excluded':
247
            $this->bExcluded = (bool)$xValue;
248
            break;
249
        // Set the protected methods
250
        case 'protected':
251
            $this->addProtectedMethods($xValue);
252
            break;
253
        // Set the methods to call before processing the request
254
        case '__before':
255
            $this->setHookMethods($this->aBeforeMethods, $xValue);
256
            break;
257
        // Set the methods to call after processing the request
258
        case '__after':
259
            $this->setHookMethods($this->aAfterMethods, $xValue);
260
            break;
261
        // Set the attributes to inject in the callable object
262
        case '__di':
263
            $this->addDiOption($xValue);
0 ignored issues
show
Bug introduced by
It seems like $xValue can also be of type string; however, parameter $aDiOptions of Jaxon\Plugin\Request\Cal...tOptions::addDiOption() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

263
            $this->addDiOption(/** @scrutinizer ignore-type */ $xValue);
Loading history...
264
            break;
265
        default:
266
            break;
267
        }
268
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
269
270
    /**
271
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
272
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
273
     * @param mixed $xOptionValue
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...
274
     *
275
     * @return void
276
     */
277
    private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue)
278
    {
279
        if(is_string($xOptionValue))
280
        {
281
            $xOptionValue = [$xOptionValue];
282
        }
283
        if(!is_array($xOptionValue))
284
        {
285
            return; // Do not save.
286
        }
287
        $aOptions = $this->aJsOptions[$sFunctionName][$sOptionName] ?? [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 39 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...
288
        $this->aJsOptions[$sFunctionName][$sOptionName] = array_merge($aOptions, $xOptionValue);
289
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
290
291
    /**
292
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
293
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
294
     * @param mixed $xOptionValue
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...
295
     *
296
     * @return void
297
     */
298
    private function _setJsOption(string $sFunctionName, string $sOptionName, $xOptionValue)
299
    {
300
        $this->aJsOptions[$sFunctionName][$sOptionName] = $xOptionValue;
301
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
302
303
    /**
304
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
305
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
306
     * @param mixed $xOptionValue
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...
307
     *
308
     * @return void
309
     */
310
    private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue)
311
    {
312
        switch($sOptionName)
313
        {
314
        case 'excluded':
315
            if((bool)$xOptionValue)
316
            {
317
                $this->addProtectedMethods($sFunctionName);
318
            }
319
            break;
320
        // For databags, all the value are merged in a single array.
321
        case 'bags':
322
            $this->_addJsArrayOption($sFunctionName, $sOptionName, $xOptionValue);
323
            return;
324
        // For all the other options, including callback, only the last value is kept.
325
        case 'callback':
326
        default:
327
            $this->_setJsOption($sFunctionName, $sOptionName, $xOptionValue);
328
        }
329
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
330
331
    /**
332
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
333
     * @param array $aFunctionOptions
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
334
     *
335
     * @return void
336
     */
337
    private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions)
338
    {
339
        foreach($aFunctionOptions as $sOptionName => $xOptionValue)
340
        {
341
            substr($sOptionName, 0, 2) === '__' ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
342
                // Options for PHP classes. They start with "__".
343
                $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
344
                // Options for javascript code.
345
                $this->addJsOption($sFunctionName, $sOptionName, $xOptionValue);
346
        }
347
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
348
349
    /**
350
     * @param string $sMethodName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
351
     *
352
     * @return array
353
     */
354
    public function getMethodOptions(string $sMethodName): array
355
    {
356
        // First take the common options.
357
        $aOptions = array_merge($this->aJsOptions['*'] ?? []); // Clone the array
358
        // Then add the method options.
359
        $aMethodOptions = $this->aJsOptions[$sMethodName] ?? [];
360
        foreach($aMethodOptions as $sOptionName => $xOptionValue)
361
        {
362
            // For databags, merge the values in a single array.
363
            // For all the other options, including callback, keep the last value.
364
            $aOptions[$sOptionName] = $sOptionName !== 'bags' ? $xOptionValue :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
365
                array_unique(array_merge($aOptions[$sOptionName] ?? [], $xOptionValue));
366
        }
367
        return $aOptions;
368
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
369
}
370