Passed
Push — main ( 79e33c...18c043 )
by Thierry
03:12
created

CallableObjectOptions::__construct()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 23
rs 9.2222
cc 6
nc 14
nop 2
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
     * The constructor
76
     *
77
     * @param array $aOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     * @param array $aAnnotations
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     */
80
    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...
81
    {
82
        [$bExcluded, $aAnnotationOptions, $aAnnotationProtected] = $aAnnotations;
83
        $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...
84
        if($this->bExcluded)
85
        {
86
            return;
87
        }
88
89
        $this->addOption('separator', $aOptions['separator']);
90
        $this->addOption('protected', array_merge($aOptions['protected'], $aAnnotationProtected));
91
92
        foreach($aOptions['functions'] as $sNames => $aFunctionOptions)
93
        {
94
            $aFunctionNames = explode(',', $sNames); // Names are in comma-separated list.
95
            foreach($aFunctionNames as $sFunctionName)
96
            {
97
                $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
98
            }
99
        }
100
        foreach($aAnnotationOptions as $sFunctionName => $aFunctionOptions)
101
        {
102
            $this->addFunctionOptions($sFunctionName, $aFunctionOptions);
103
        }
104
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
105
106
    /**
107
     * Check if the js code for this object must be generated
108
     *
109
     * @return bool
110
     */
111
    public function excluded(): bool
112
    {
113
        return $this->bExcluded;
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * @return string
118
     */
119
    public function separator(): string
120
    {
121
        return $this->sSeparator;
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 protectedMethods(): array
128
    {
129
        return $this->aProtectedMethods;
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
131
132
    /**
133
     * @return array
134
     */
135
    public function beforeMethods(): array
136
    {
137
        return $this->aBeforeMethods;
138
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
139
140
    /**
141
     * @return array
142
     */
143
    public function afterMethods(): array
144
    {
145
        return $this->aAfterMethods;
146
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
147
148
    /**
149
     * @return array
150
     */
151
    public function diOptions(): array
152
    {
153
        return $this->aDiOptions;
154
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
155
156
    /**
157
     * @return array
158
     */
159
    public function jsOptions(): array
160
    {
161
        return $this->aJsOptions;
162
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
163
164
    /**
165
     * Set hook methods
166
     *
167
     * @param array $aHookMethods    The array of hook methods
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
168
     * @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...
169
     *
170
     * @return void
171
     */
172
    private function setHookMethods(array &$aHookMethods, $xValue)
173
    {
174
        foreach($xValue as $sCalledMethod => $xMethodToCall)
175
        {
176
            if(is_array($xMethodToCall))
177
            {
178
                $aHookMethods[$sCalledMethod] = $xMethodToCall;
179
            }
180
            elseif(is_string($xMethodToCall))
181
            {
182
                $aHookMethods[$sCalledMethod] = [$xMethodToCall];
183
            }
184
        }
185
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
186
187
    /**
188
     * @param array $aDiOptions
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
189
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
190
    private function addDiOption(array $aDiOptions)
191
    {
192
        $this->aDiOptions = array_merge($this->aDiOptions, $aDiOptions);
193
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
194
195
    /**
196
     * Set configuration options / call options for each method
197
     *
198
     * @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...
199
     * @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...
200
     *
201
     * @return void
202
     */
203
    public function addOption(string $sName, $xValue)
204
    {
205
        switch($sName)
206
        {
207
        // Set the separator
208
        case 'separator':
209
            if($xValue === '_' || $xValue === '.')
210
            {
211
                $this->sSeparator = $xValue;
212
            }
213
            break;
214
        // Set the protected methods
215
        case 'protected':
216
            if(is_array($xValue))
217
            {
218
                $this->aProtectedMethods = array_merge($this->aProtectedMethods, $xValue);
219
            }
220
            break;
221
        // Set the methods to call before processing the request
222
        case '__before':
223
            $this->setHookMethods($this->aBeforeMethods, $xValue);
224
            break;
225
        // Set the methods to call after processing the request
226
        case '__after':
227
            $this->setHookMethods($this->aAfterMethods, $xValue);
228
            break;
229
        // Set the attributes to inject in the callable object
230
        case '__di':
231
            $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

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