Passed
Push — master ( 68b778...1b1614 )
by Thierry
07:15 queued 04:29
created

CallableRepository::createCallableObject()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 52
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 52
rs 8.4444
cc 8
nc 14
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * CallableRepository.php - Jaxon callable object repository
5
 *
6
 * This class stores all the callable object already created.
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 2019 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\Request\Support;
16
17
use Jaxon\Request\Factory\CallableClass\Request as RequestFactory;
18
19
use RecursiveDirectoryIterator;
20
use RecursiveIteratorIterator;
21
22
class CallableRepository
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableRepository
Loading history...
23
{
24
    /**
25
     * The classes
26
     *
27
     * These are all the registered classes.
28
     *
29
     * @var array
30
     */
31
    protected $aClasses = [];
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 namespaces
35
     *
36
     * These are all the namespaces found in registered directories.
37
     *
38
     * @var array
39
     */
40
    protected $aNamespaces = [];
41
42
    /**
43
     * The created callable objects.
44
     *
45
     * @var array
46
     */
47
    protected $aCallableObjects = [];
48
49
    /**
50
     * The options to be applied to callable objects.
51
     *
52
     * @var array
53
     */
54
    protected $aCallableOptions = [];
55
56
    /**
57
     * Get a given class options from specified directory options
58
     *
59
     * @param string        $sClassName         The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
60
     * @param array         $aClassOptions      The default class options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
61
     * @param array         $aDirectoryOptions  The directory options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
62
     *
63
     * @return array
64
     */
65
    public function makeClassOptions($sClassName, array $aClassOptions, array $aDirectoryOptions)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
66
    {
67
        $aOptions = $aClassOptions;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 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...
68
        $aOptions['functions'] = [];
69
        foreach(['separator', 'protected'] as $sName)
70
        {
71
            if(key_exists($sName, $aDirectoryOptions))
72
            {
73
                $aOptions[$sName] = $aDirectoryOptions[$sName];
74
            }
75
        }
76
77
        $aFunctionOptions = key_exists('classes', $aDirectoryOptions) ? $aDirectoryOptions['classes'] : [];
78
        // Sort the options by asc key length
79
        uksort($aFunctionOptions, function($key1, $key2) {
80
            return strlen($key1) - strlen($key2);
81
        });
82
        foreach($aFunctionOptions as $sName => $xValue)
83
        {
84
            if($sName === '*')
85
            {
86
                $aOptions['functions'] = array_merge($aOptions['functions'], $xValue);
87
            }
88
            elseif(strncmp($sClassName, $sName, strlen($sName)) === 0)
89
            {
90
                $aOptions['functions'] = array_merge($aOptions['functions'], $xValue);
91
            }
92
        }
93
94
        // This value will be used to compute hash
95
        if(!key_exists('timestamp', $aOptions))
96
        {
97
            $aOptions['timestamp'] = 0;
98
        }
99
100
        return $aOptions;
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     *
105
     * @param string        $sClassName         The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter name; 9 found
Loading history...
106
     * @param array         $aClassOptions      The default class options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter name; 6 found
Loading history...
107
     * @param array         $aDirectoryOptions  The directory options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
108
     *
109
     * @return void
110
     */
111
    public function addClass($sClassName, array $aClassOptions, array $aDirectoryOptions = [])
112
    {
113
        $this->aClasses[$sClassName] = $this->makeClassOptions($sClassName, $aClassOptions, $aDirectoryOptions);
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     *
118
     * @param string        $sNamespace     The namespace
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
119
     * @param array|string  $aOptions       The associated options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
120
     *
121
     * @return void
122
     */
123
    public function addNamespace($sNamespace, $aOptions)
124
    {
125
        $this->aNamespaces[$sNamespace] = $aOptions;
126
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
127
128
    /**
129
     * Find the options associated with a registered class name
130
     *
131
     * @param string        $sClassName            The class name
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
132
     *
133
     * @return array|null
134
     */
135
    public function getClassOptions($sClassName)
136
    {
137
        if(!key_exists($sClassName, $this->aClasses))
138
        {
139
            // Class not found
140
            return null;
141
        }
142
        return $this->aClasses[$sClassName];
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * Find a callable object by class name
147
     *
148
     * @param string        $sClassName            The class name of the callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
149
     *
150
     * @return CallableObject|null
151
     */
152
    public function getCallableObject($sClassName)
153
    {
154
        return key_exists($sClassName, $this->aCallableObjects) ? $this->aCallableObjects[$sClassName] : null;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Create a new callable object
159
     *
160
     * @param string        $sClassName            The class name of the callable object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 12 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
161
     * @param array         $aOptions              The callable object options
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 14 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 9 found
Loading history...
162
     *
163
     * @return CallableObject|null
164
     */
165
    public function createCallableObject($sClassName, array $aOptions)
166
    {
167
        // Make sure the registered class exists
168
        if(key_exists('include', $aOptions))
169
        {
170
            require_once($aOptions['include']);
171
        }
172
        if(!class_exists($sClassName))
173
        {
174
            return null;
175
        }
176
177
        // Create the callable object
178
        $xCallableObject = new CallableObject($sClassName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 21 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...
179
        $this->aCallableOptions[$sClassName] = [];
180
        foreach(['namespace', 'separator', 'protected'] as $sName)
181
        {
182
            if(key_exists($sName, $aOptions))
183
            {
184
                $xCallableObject->configure($sName, $aOptions[$sName]);
185
            }
186
        }
187
188
        // Functions options
189
        if(key_exists('functions', $aOptions))
190
        {
191
            foreach($aOptions['functions'] as $sName => $xValue)
192
            {
193
                // Options for PHP classes. They start with "__"
194
                $aPhpOptions = array_filter($xValue, function($sOptionName) {
195
                    return substr($sOptionName, 0, 2) === '__';
196
                }, ARRAY_FILTER_USE_KEY);
197
                foreach($aPhpOptions as $sOptionName => $xOptionValue)
198
                {
199
                    $xCallableObject->configure($sOptionName, [$sName => $xOptionValue]);
200
                    continue;
201
                }
202
203
                // Options for javascript code.
204
                $aJsOptions = array_filter($xValue, function($sOptionName) {
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 34 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...
205
                    return substr($sOptionName, 0, 2) !== '__';
206
                }, ARRAY_FILTER_USE_KEY);
207
                $this->aCallableOptions[$sClassName][$sName] = $aJsOptions;
208
            }
209
        }
210
211
        $this->aCallableObjects[$sClassName] = $xCallableObject;
212
213
        // Register the request factory for this callable object
214
        jaxon()->di()->setCallableClassRequestFactory($sClassName, $xCallableObject);
215
216
        return $xCallableObject;
217
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
218
219
    /**
220
     * Get all registered classes
221
     *
222
     * @return array
223
     */
224
    public function getClasses()
225
    {
226
        return $this->aClasses;
227
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
228
229
    /**
230
     * Get all registered namespaces
231
     *
232
     * @return array
233
     */
234
    public function getNamespaces()
235
    {
236
        return $this->aNamespaces;
237
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
238
239
    /**
240
     * Get all registered callable objects
241
     *
242
     * @return array
243
     */
244
    public function getCallableObjects()
245
    {
246
        return $this->aCallableObjects;
247
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
248
249
    /**
250
     * Get all registered callable objects options
251
     *
252
     * @return array
253
     */
254
    public function getCallableOptions()
255
    {
256
        return $this->aCallableOptions;
257
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
258
}
259