Passed
Push — master ( 888408...eb1365 )
by Thierry
02:11
created

CallableFunctionPlugin::getCallable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * CallableFunctionPlugin.php - Jaxon user function plugin
5
 *
6
 * This class registers user defined functions, generates client side javascript code,
7
 * and calls them on user request
8
 *
9
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
10
 * @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...
11
 * @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...
12
 * @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...
13
 * @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...
14
 * @author Thierry Feuzeu <[email protected]>
15
 * @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...
16
 * @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...
17
 * @copyright 2016 Thierry Feuzeu <[email protected]>
18
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
19
 * @link https://github.com/jaxon-php/jaxon-core
20
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
PHP version not specified
Loading history...
21
22
namespace Jaxon\Request\Plugin\CallableFunction;
23
24
use Jaxon\Jaxon;
25
use Jaxon\Container\Container;
26
use Jaxon\Plugin\RequestPlugin;
27
use Jaxon\Request\Handler\RequestHandler;
28
use Jaxon\Request\Target;
29
use Jaxon\Request\Validator;
30
use Jaxon\Response\ResponseManager;
31
use Jaxon\Utils\Template\Engine as TemplateEngine;
32
use Jaxon\Utils\Translation\Translator;
33
use Jaxon\Exception\RequestException;
34
use Jaxon\Exception\SetupException;
35
36
use function array_keys;
37
use function implode;
38
use function is_array;
39
use function is_string;
40
use function md5;
41
use function trim;
42
43
class CallableFunctionPlugin extends RequestPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableFunctionPlugin
Loading history...
44
{
45
    /**
46
     * @var string
47
     */
48
    private $sPrefix;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
49
50
    /**
51
     * The request handler
52
     *
53
     * @var RequestHandler
54
     */
55
    protected $xRequestHandler;
56
57
    /**
58
     * The response manager
59
     *
60
     * @var ResponseManager
61
     */
62
    protected $xResponseManager;
63
64
    /**
65
     * The request data validator
66
     *
67
     * @var Validator
68
     */
69
    protected $xValidator;
70
71
    /**
72
     * @var TemplateEngine
73
     */
74
    protected $xTemplateEngine;
75
76
    /**
77
     * @var Translator
78
     */
79
    protected $xTranslator;
80
81
    /**
82
     * The registered functions names
83
     *
84
     * @var array
85
     */
86
    protected $aFunctions = [];
87
88
    /**
89
     * The registered functions options
90
     *
91
     * @var array
92
     */
93
    protected $aOptions = [];
94
95
    /**
96
     * The name of the function that is being requested (during the request processing phase)
97
     *
98
     * @var string
99
     */
100
    protected $sRequestedFunction = null;
101
102
    /**
103
     * The constructor
104
     *
105
     * @param string  $sPrefix
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 2 found
Loading history...
106
     * @param RequestHandler  $xRequestHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     * @param ResponseManager  $xResponseManager
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
108
     * @param TemplateEngine  $xTemplateEngine
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
109
     * @param Translator  $xTranslator
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
110
     * @param Validator  $xValidator
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
111
     */
112
    public function __construct(string $sPrefix, RequestHandler $xRequestHandler,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
113
        ResponseManager $xResponseManager, TemplateEngine $xTemplateEngine,
114
        Translator $xTranslator, Validator $xValidator)
115
    {
116
        $this->sPrefix = $sPrefix;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
117
        $this->xRequestHandler = $xRequestHandler;
118
        $this->xResponseManager = $xResponseManager;
119
        $this->xTemplateEngine = $xTemplateEngine;
120
        $this->xTranslator = $xTranslator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
121
        $this->xValidator = $xValidator;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
122
123
        if(isset($_GET['jxnfun']))
124
        {
125
            $this->sRequestedFunction = $_GET['jxnfun'];
126
        }
127
        if(isset($_POST['jxnfun']))
128
        {
129
            $this->sRequestedFunction = $_POST['jxnfun'];
130
        }
131
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
132
133
    /**
134
     * @inheritDoc
135
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
136
    public function getName(): string
137
    {
138
        return Jaxon::CALLABLE_FUNCTION;
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
    /**
142
     * @inheritDoc
143
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
144
    public function getTarget(): ?Target
145
    {
146
        if(!$this->sRequestedFunction)
147
        {
148
            return null;
149
        }
150
        return Target::makeFunction($this->sRequestedFunction);
151
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
152
153
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $xOptions should have a doc-comment as per coding-style.
Loading history...
154
     * @inheritDoc
155
     * @throws SetupException
156
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
157
    public function checkOptions(string $sCallable, $xOptions): array
158
    {
159
        if(!$this->xValidator->validateFunction(trim($sCallable)))
160
        {
161
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
162
        }
163
        if(is_string($xOptions))
164
        {
165
            $xOptions = ['include' => $xOptions];
166
        }
167
        elseif(!is_array($xOptions))
168
        {
169
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
170
        }
171
        return $xOptions;
172
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
173
174
    /**
175
     * Register a user defined function
176
     *
177
     * @param string $sType    The type of request handler being registered
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
178
     * @param string $sCallable    The name of the function being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
179
     * @param array $aOptions    The associated options
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
180
     *
181
     * @return bool
182
     */
183
    public function register(string $sType, string $sCallable, array $aOptions): bool
184
    {
185
        $sPhpFunction = trim($sCallable);
186
        $sFunction = $sPhpFunction;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 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...
187
        // Check if an alias is defined
188
        if(isset($aOptions['alias']))
189
        {
190
            $sFunction = (string)$aOptions['alias'];
191
            unset($aOptions['alias']);
192
        }
193
        $this->aFunctions[$sFunction] = $sPhpFunction;
194
        $this->aOptions[$sFunction] = $aOptions;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 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...
195
        return true;
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
197
198
    /**
199
     * @inheritDoc
200
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
201
    public function getHash(): string
202
    {
203
        return md5(implode('', array_keys($this->aFunctions)));
204
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
    /**
207
     * Get the callable object for a registered function
208
     *
209
     * @param string $sFunction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
210
     *
211
     * @return CallableFunction|null
212
     */
213
    public function getCallable(string $sFunction): ?CallableFunction
214
    {
215
        if(!isset($this->aFunctions[$sFunction]))
216
        {
217
            return null;
218
        }
219
        $xCallable = new CallableFunction($sFunction, $this->sPrefix . $sFunction, $this->aFunctions[$sFunction]);
220
        foreach($this->aOptions[$sFunction] as $sName => $sValue)
221
        {
222
            $xCallable->configure($sName, $sValue);
223
        }
224
        return $xCallable;
225
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
226
227
    /**
228
     * Generate the javascript function stub that is sent to the browser on initial page load
229
     *
230
     * @param CallableFunction $xFunction
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
231
     *
232
     * @return string
233
     */
234
    private function getCallableScript(CallableFunction $xFunction): string
235
    {
236
        return $this->xTemplateEngine->render('jaxon::callables/function.js', [
237
            'sName' => $xFunction->getName(),
238
            'sJsName' => $xFunction->getJsName(),
239
            'aOptions' => $xFunction->getOptions(),
240
        ]);
241
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
242
243
    /**
244
     * @inheritDoc
245
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
246
    public function getScript(): string
247
    {
248
        $code = '';
249
        foreach(array_keys($this->aFunctions) as $sFunction)
250
        {
251
            $xFunction = $this->getCallable($sFunction);
252
            $code .= $this->getCallableScript($xFunction);
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...
Bug introduced by
It seems like $xFunction can also be of type null; however, parameter $xFunction of Jaxon\Request\Plugin\Cal...in::getCallableScript() does only seem to accept Jaxon\Request\Plugin\Cal...nction\CallableFunction, 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

252
            $code .= $this->getCallableScript(/** @scrutinizer ignore-type */ $xFunction);
Loading history...
253
        }
254
        return $code;
255
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
256
257
    /**
258
     * @inheritDoc
259
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
260
    public function canProcessRequest(): bool
261
    {
262
        // Check the validity of the function name
263
        if(($this->sRequestedFunction) && !$this->xValidator->validateFunction($this->sRequestedFunction))
264
        {
265
            $this->sRequestedFunction = null;
266
        }
267
        return ($this->sRequestedFunction != null);
268
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
269
270
    /**
271
     * @inheritDoc
272
     * @throws RequestException
273
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
274
    public function processRequest(): bool
275
    {
276
        // Security check: make sure the requested function was registered.
277
        if(!isset($this->aFunctions[$this->sRequestedFunction]))
278
        {
279
            // Unable to find the requested function
280
            throw new RequestException($this->xTranslator->trans('errors.functions.invalid',
281
                ['name' => $this->sRequestedFunction]));
282
        }
283
284
        $xFunction = $this->getCallable($this->sRequestedFunction);
285
        $aArgs = $this->xRequestHandler->processArguments();
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...
286
        $xResponse = $xFunction->call($aArgs);
287
        if(($xResponse))
288
        {
289
            $this->xResponseManager->append($xResponse);
290
        }
291
        return true;
292
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
293
}
294