Passed
Push — v5.x ( 57c2ac...73bdb8 )
by Thierry
16:58 queued 05:55
created

CallableDirPlugin::checkOptions()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 27
rs 9.5222
cc 5
nc 10
nop 2
1
<?php
2
3
/**
4
 * CallableDirPlugin.php - Jaxon callable dir plugin
5
 *
6
 * This class registers directories containing user defined callable classes,
7
 * and generates client side javascript code.
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
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
21
22
namespace Jaxon\Plugin\Request\CallableClass;
23
24
use Jaxon\Jaxon;
25
use Jaxon\App\I18n\Translator;
26
use Jaxon\Exception\SetupException;
27
use Jaxon\Plugin\CallableRegistryInterface;
28
use Jaxon\Plugin\PluginInterface;
29
30
use function is_array;
31
use function is_dir;
32
use function is_string;
33
use function realpath;
34
use function rtrim;
35
use function str_replace;
36
use function trim;
37
38
class CallableDirPlugin implements PluginInterface, CallableRegistryInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableDirPlugin
Loading history...
39
{
40
    /**
41
     * The callable registry
42
     *
43
     * @var CallableRegistry
44
     */
45
    protected $xRegistry;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
46
47
    /**
48
     * @var Translator
49
     */
50
    protected $xTranslator;
51
52
    /**
53
     * The class constructor
54
     *
55
     * @param CallableRegistry  $xRegistry
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
56
     * @param Translator  $xTranslator
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 2 found
Loading history...
57
     */
58
    public function __construct(CallableRegistry $xRegistry, Translator $xTranslator)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
59
    {
60
        $this->xRegistry = $xRegistry;
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...
61
        $this->xTranslator = $xTranslator;
62
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
63
64
    /**
65
     * @inheritDoc
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function getName(): string
68
    {
69
        return Jaxon::CALLABLE_DIR;
70
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
71
72
    /**
73
     * Check the directory
74
     *
75
     * @param string $sDirectory    The path of teh directory being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
76
     *
77
     * @return string
78
     * @throws SetupException
79
     */
80
    private function checkDirectory(string $sDirectory): string
81
    {
82
        $sDirectory = rtrim(trim($sDirectory), '/\\');
83
        if(!is_dir($sDirectory))
84
        {
85
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
86
        }
87
        return realpath($sDirectory);
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
89
90
    /**
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...
91
     * @inheritDoc
92
     * @throws SetupException
93
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
94
    public function checkOptions(string $sCallable, $xOptions): array
95
    {
96
        if(is_string($xOptions))
97
        {
98
            $xOptions = ['namespace' => $xOptions];
99
        }
100
        if(!is_array($xOptions))
101
        {
102
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
103
        }
104
        // Check the directory
105
        $xOptions['directory'] = $this->checkDirectory($sCallable);
106
        // Check the namespace
107
        $sNamespace = $xOptions['namespace'] ?? '';
108
        if(!($xOptions['namespace'] = trim($sNamespace, ' \\')))
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
109
        {
110
            $xOptions['namespace'] = '';
111
        }
112
113
        // Change the keys in $xOptions to have "\" as separator
114
        $_aOptions = [];
115
        foreach($xOptions as $sName => $aOption)
116
        {
117
            $sName = trim(str_replace('.', '\\', $sName), ' \\');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
118
            $_aOptions[$sName] = $aOption;
119
        }
120
        return $_aOptions;
121
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
122
123
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sType should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $aOptions should have a doc-comment as per coding-style.
Loading history...
124
     * @inheritDoc
125
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
126
    public function register(string $sType, string $sCallable, array $aOptions): bool
127
    {
128
        if(($aOptions['namespace']))
129
        {
130
            $this->xRegistry->addNamespace($aOptions['namespace'], $aOptions);
131
            return true;
132
        }
133
        $this->xRegistry->addDirectory($aOptions['directory'], $aOptions);
134
        return true;
135
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
136
137
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $sCallable should have a doc-comment as per coding-style.
Loading history...
138
     * @inheritDoc
139
     * @throws SetupException
140
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
141
    public function getCallable(string $sCallable)
142
    {
143
        return $this->xRegistry->getCallableObject($sCallable);
144
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
145
}
146