Passed
Push — master ( 27d45a...b61896 )
by Thierry
02:21
created

CallableDirPlugin::getCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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