Passed
Push — main ( fef313...79e33c )
by Thierry
11:43
created

CallableObjectOptions::diOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
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
23
class CallableObjectOptions
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableObjectOptions
Loading history...
24
{
25
    /**
26
     * Check if the js code for this object must be generated
27
     *
28
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
29
     */
30
    private $bExcluded = false;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
31
32
    /**
33
     * The character to use as separator in javascript class names
34
     *
35
     * @var string
36
     */
37
    private $sSeparator = '.';
38
39
    /**
40
     * A list of methods of the user registered callable object the library must not export to javascript
41
     *
42
     * @var array
43
     */
44
    private $aProtectedMethods = [];
45
46
    /**
47
     * A list of methods to call before processing the request
48
     *
49
     * @var array
50
     */
51
    private $aBeforeMethods = [];
52
53
    /**
54
     * A list of methods to call after processing the request
55
     *
56
     * @var array
57
     */
58
    private $aAfterMethods = [];
59
60
    /**
61
     * The javascript class options
62
     *
63
     * @var array
64
     */
65
    private $aJsOptions = [];
66
67
    /**
68
     * The DI options
69
     *
70
     * @var array
71
     */
72
    private $aDiOptions = [];
73
74
    /**
75
     * Check if the js code for this object must be generated
76
     *
77
     * @return bool
78
     */
79
    public function excluded(): bool
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
80
    {
81
        return $this->bExcluded;
82
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
83
84
    /**
85
     * @return string
86
     */
87
    public function separator(): string
88
    {
89
        return $this->sSeparator;
90
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
91
92
    /**
93
     * @return array
94
     */
95
    public function protectedMethods(): array
96
    {
97
        return $this->aProtectedMethods;
98
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
99
100
    /**
101
     * @return array
102
     */
103
    public function beforeMethods(): array
104
    {
105
        return $this->aBeforeMethods;
106
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
107
108
    /**
109
     * @return array
110
     */
111
    public function afterMethods(): array
112
    {
113
        return $this->aAfterMethods;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * @return array
118
     */
119
    public function diOptions(): array
120
    {
121
        return $this->aDiOptions;
122
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
123
124
    /**
125
     * @return array
126
     */
127
    public function jsOptions(): array
128
    {
129
        return $this->aJsOptions;
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * Set hook methods
134
     *
135
     * @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...
136
     * @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...
137
     *
138
     * @return void
139
     */
140
    private function setHookMethods(array &$aHookMethods, $xValue)
141
    {
142
        foreach($xValue as $sCalledMethod => $xMethodToCall)
143
        {
144
            if(is_array($xMethodToCall))
145
            {
146
                $aHookMethods[$sCalledMethod] = $xMethodToCall;
147
            }
148
            elseif(is_string($xMethodToCall))
149
            {
150
                $aHookMethods[$sCalledMethod] = [$xMethodToCall];
151
            }
152
        }
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * @param array $aDiOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
157
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
158
    private function addDiOption(array $aDiOptions)
159
    {
160
        $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions);
161
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
162
163
    /**
164
     * Set configuration options / call options for each method
165
     *
166
     * @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...
167
     * @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...
168
     *
169
     * @return void
170
     */
171
    public function addOption(string $sName, $xValue)
172
    {
173
        switch($sName)
174
        {
175
        // Set the separator
176
        case 'separator':
177
            if($xValue === '_' || $xValue === '.')
178
            {
179
                $this->sSeparator = $xValue;
180
            }
181
            break;
182
        // Set the protected methods
183
        case 'protected':
184
            if(is_array($xValue))
185
            {
186
                $this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue);
187
            }
188
            break;
189
        // Set the methods to call before processing the request
190
        case '__before':
191
            $this->setHookMethods($this->aBeforeMethods, $xValue);
192
            break;
193
        // Set the methods to call after processing the request
194
        case '__after':
195
            $this->setHookMethods($this->aAfterMethods, $xValue);
196
            break;
197
        // Set the attributes to inject in the callable object
198
        case '__di':
199
            $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

199
            $this->addDiOption(/** @scrutinizer ignore-type */ $xValue);
Loading history...
200
            break;
201
        case 'excluded':
202
            $this->bExcluded = (bool)$xValue;
203
            break;
204
        default:
205
            break;
206
        }
207
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
208
209
    /**
210
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
211
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
212
     * @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...
213
     *
214
     * @return void
215
     */
216
    private function _addJsArrayOption(string $sFunctionName, string $sOptionName, $xOptionValue)
217
    {
218
        if(is_string($xOptionValue))
219
        {
220
            $xOptionValue = [$xOptionValue];
221
        }
222
        if(!is_array($xOptionValue))
223
        {
224
            return; // Do not save.
225
        }
226
        $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...
227
        $this->aJsOptions[$sFunctionName][$sOptionName] = array_merge($aOptions, $xOptionValue);
228
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
229
230
    /**
231
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
232
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
233
     * @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...
234
     *
235
     * @return void
236
     */
237
    private function _setJsOption(string $sFunctionName, string $sOptionName, $xOptionValue)
238
    {
239
        $this->aJsOptions[$sFunctionName][$sOptionName] = $xOptionValue;
240
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
241
242
    /**
243
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
244
     * @param string $sOptionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
245
     * @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...
246
     *
247
     * @return void
248
     */
249
    private function addJsOption(string $sFunctionName, string $sOptionName, $xOptionValue)
250
    {
251
        switch($sOptionName)
252
        {
253
        // For databags, all the value are merged in a single array.
254
        case 'bags':
255
            $this->_addJsArrayOption($sFunctionName, $sOptionName, $xOptionValue);
256
            return;
257
        // For all the other options, including callback, only the last value is kept.
258
        case 'callback':
259
        default:
260
            $this->_setJsOption($sFunctionName, $sOptionName, $xOptionValue);
261
        }
262
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
263
264
    /**
265
     * @param string $sFunctionName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
266
     * @param array $aFunctionOptions
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...
267
     *
268
     * @return void
269
     */
270
    private function addFunctionOptions(string $sFunctionName, array $aFunctionOptions)
271
    {
272
        foreach($aFunctionOptions as $sOptionName => $xOptionValue)
273
        {
274
            substr($sOptionName, 0, 2) === '__' ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
275
                // Options for PHP classes. They start with "__".
276
                $this->addOption($sOptionName, [$sFunctionName => $xOptionValue]) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
277
                // Options for javascript code.
278
                $this->addJsOption($sFunctionName, $sOptionName, $xOptionValue);
279
        }
280
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
281
282
    /**
283
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
284
     * @param array $aAnnotations
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
285
     *
286
     * @return void
287
     */
288
    public function setOptions(array $aOptions, array $aAnnotations)
289
    {
290
        [, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations;
291
292
        $this->addOption('separator', $aOptions['separator']);
293
        $this->addOption('protected', array_merge($aOptions['protected'], $aAnnotationProtected));
294
295
        foreach($aOptions['functions'] as $sNames => $aFunctionOptions)
296
        {
297
            $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list.
298
            foreach($aFunctionNames as $sFunctionName)
299
            {
300
                $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
301
            }
302
        }
303
        foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions)
304
        {
305
            $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
306
        }
307
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
308
309
    /**
310
     * @param string $sMethodName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
311
     *
312
     * @return array
313
     */
314
    public function getMethodOptions(string $sMethodName): array
315
    {
316
        // First take the common options.
317
        $aOptions = array_merge($this->aJsOptions['*'] ?? []); // Clone the array
318
        // Then add the method options.
319
        $aMethodOptions = $this->aJsOptions[$sMethodName] ?? [];
320
        foreach($aMethodOptions as $sOptionName => $xOptionValue)
321
        {
322
            // For databags, merge the values in a single array.
323
            // For all the other options, including callback, keep the last value.
324
            $aOptions[$sOptionName] = $sOptionName !== 'bags' ? $xOptionValue :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
325
                array_unique(array_merge($aOptions[$sOptionName] ?? [], $xOptionValue));
326
        }
327
        return $aOptions;
328
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
329
}
330