Passed
Push — master ( a07882...2b8759 )
by Thierry
02:21
created

CallableDirPlugin::checkDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
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\RegistryPlugin;
26
use Jaxon\Utils\Translation\Translator;
27
use Jaxon\Exception\SetupException;
28
29
use function is_array;
30
use function is_dir;
31
use function is_string;
32
use function realpath;
33
use function rtrim;
34
use function trim;
35
36
class CallableDirPlugin extends RegistryPlugin
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CallableDirPlugin
Loading history...
37
{
38
    /**
39
     * The callable registrar
40
     *
41
     * @var CallableRegistry
42
     */
43
    protected $xRegistry;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
44
45
    /**
46
     * @var Translator
47
     */
48
    protected $xTranslator;
49
50
    /**
51
     * The class constructor
52
     *
53
     * @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...
54
     * @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...
55
     */
56
    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...
57
    {
58
        $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...
59
        $this->xTranslator = $xTranslator;
60
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
61
62
    /**
63
     * @inheritDoc
64
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
65
    public function getName(): string
66
    {
67
        return Jaxon::CALLABLE_DIR;
68
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
69
70
    /**
71
     * Check the directory
72
     *
73
     * @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...
74
     *
75
     * @return string
76
     * @throws SetupException
77
     */
78
    private function checkDirectory(string $sDirectory): string
79
    {
80
        $sDirectory = rtrim(trim($sDirectory), '/\\');
81
        if(!is_dir($sDirectory))
82
        {
83
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
84
        }
85
        return realpath($sDirectory);
86
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
87
88
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $xOptions 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...
89
     * @inheritDoc
90
     * @throws SetupException
91
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
92
    public function checkOptions(string $sCallable, $xOptions): array
93
    {
94
        if(is_string($xOptions))
95
        {
96
            $xOptions = ['namespace' => $xOptions];
97
        }
98
        if(!is_array($xOptions))
99
        {
100
            throw new SetupException($this->xTranslator->trans('errors.objects.invalid-declaration'));
101
        }
102
        // Check the directory
103
        $xOptions['directory'] = $this->checkDirectory($sCallable);
104
        // Check the namespace
105
        $sNamespace = $xOptions['namespace'] ?? '';
106
        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...
107
        {
108
            $xOptions['namespace'] = '';
109
        }
110
111
        // Change the keys in $xOptions to have "\" as separator
112
        $_aOptions = [];
113
        foreach($xOptions as $sName => $aOption)
114
        {
115
            $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...
116
            $_aOptions[$sName] = $aOption;
117
        }
118
        return $_aOptions;
119
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
120
121
    /**
122
     * Register a callable class
123
     *
124
     * @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...
125
     * @param string $sCallable    The path of the directory being registered
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
126
     * @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...
127
     *
128
     * @return bool
129
     */
130
    public function register(string $sType, string $sCallable, array $aOptions): bool
131
    {
132
        if(($aOptions['namespace']))
133
        {
134
            $this->xRegistry->addNamespace($aOptions['namespace'], $aOptions);
135
            return true;
136
        }
137
        $this->xRegistry->addDirectory($aOptions['directory'], $aOptions);
138
        return true;
139
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
140
}
141