Passed
Push — v5.x ( 223ede...01c17e )
by Thierry
02:01
created

Selector::toInt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Selector.php - A jQuery selector
5
 *
6
 * This class is used to create client side requests to the Jaxon functions and callable objects.
7
 *
8
 * When inserted into a Jaxon response, a Selector object must be converted to the corresponding jQuery code.
9
 * Therefore, the Selector class implements the JsonSerializable interface.
10
 *
11
 * When used as a parameter of a Jaxon call, the Selector must be converted to Jaxon js call parameter.
12
 * Therefore, the Selector class also implements the Jaxon\Js\ParameterInterface interface.
13
 *
14
 * @package jaxon-jquery
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-jquery" is not valid; consider "Jaxonjquery" instead
Loading history...
15
 * @author Thierry Feuzeu <[email protected]>
16
 * @copyright 2016 Thierry Feuzeu <[email protected]>
17
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
18
 * @link https://github.com/jaxon-php/jaxon-jquery
19
 */
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...
20
21
namespace Jaxon\Js;
22
23
use function implode;
24
use function is_a;
25
use function trim;
26
27
class Selector implements ParameterInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Selector
Loading history...
28
{
29
    use Traits\ToInt;
30
31
    /**
32
     * The jQuery selector path
33
     *
34
     * @var string
35
     */
36
    protected $sPath;
37
38
    /**
39
     * The jQuery selector context
40
     *
41
     * @var mixed
42
     */
43
    protected $xContext;
44
45
    /**
46
     * The actions to be applied on the selected element
47
     *
48
     * @var array
49
     */
50
    protected $aCalls = [];
51
52
    /**
53
     * The constructor.
54
     *
55
     * @param string $sPath    The jQuery selector path
56
     * @param mixed $xContext    A context associated to the selector
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
57
     */
58
    public function __construct(string $sPath, $xContext)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
59
    {
60
        $this->sPath = trim($sPath, " \t");
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...
61
        $this->xContext = $xContext;
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 getType(): string
68
    {
69
        return 'expr';
70
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
71
72
    /**
73
     * Add a call to a jQuery method on the selected elements
74
     *
75
     * @param string  $sMethod
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...
76
     * @param array  $aArguments
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     *
78
     * @return Selector
79
     */
80
    public function __call(string $sMethod, array $aArguments)
81
    {
82
        // Append the action into the array
83
        $this->aCalls[] = new Selector\Method($sMethod, $aArguments);
84
        return $this;
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
86
87
    /**
88
     * Get the value of an attribute on the first selected element
89
     *
90
     * @param string  $sAttribute
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...
91
     *
92
     * @return Selector
93
     */
94
    public function __get(string $sAttribute)
95
    {
96
        // Append the action into the array
97
        $this->aCalls[] = new Selector\AttrGet($sAttribute);
98
        return $this;
99
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
100
101
    /**
102
     * Set the value of an attribute on the first selected element
103
     *
104
     * @param string $sAttribute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     * @param mixed $xValue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
106
     *
107
     * @return void
108
     */
109
    public function __set(string $sAttribute, $xValue)
110
    {
111
        // Append the action into the array
112
        $this->aCalls[] = new Selector\AttrSet($sAttribute, $xValue);
113
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Js\Selector which is incompatible with the documented return type void.
Loading history...
114
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
115
116
    /**
117
     * Set an event handler on the first selected element
118
     *
119
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
120
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
121
     *
122
     * @return Selector
123
     */
124
    public function on(string $sName, Call $xHandler): Selector
125
    {
126
        $this->aCalls[] = new Selector\Event($sName, $xHandler);
127
        return $this;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Set an "click" event handler on the first selected element
132
     *
133
     * @param Call $xHandler
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
134
     *
135
     * @return Selector
136
     */
137
    public function click(Call $xHandler): Selector
138
    {
139
        $this->on('click', $xHandler);
140
        return $this;
141
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
142
143
    /**
144
     * Get the selector js.
145
     *
146
     * @return string
147
     */
148
    private function getPathAsStr()
149
    {
150
        $jQuery = 'jaxon.jq'; // The JQuery selector
151
        if(!$this->sPath)
152
        {
153
            // If an empty selector is given, use the event target instead
154
            return "$jQuery(e.currentTarget)";
155
        }
156
        if(!$this->xContext)
157
        {
158
            return "$jQuery('" . $this->sPath . "')";
159
        }
160
161
        $sContext = is_a($this->xContext, self::class) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
162
            $this->xContext->getScript() :
0 ignored issues
show
Bug introduced by
The method getScript() does not exist on Jaxon\Js\Selector. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
            $this->xContext->/** @scrutinizer ignore-call */ 
163
                             getScript() :
Loading history...
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
163
            "$jQuery('" . trim("{$this->xContext}") . "')";
164
        return "$jQuery('{$this->sPath}', $sContext)";
165
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
166
167
    /**
168
     * Generate the jQuery call.
169
     *
170
     * @return string
171
     */
172
    public function __toString(): string
173
    {
174
        $sScript = $this->getPathAsStr() . implode('', $this->aCalls);
175
        return $this->bToInt ? "parseInt($sScript)" : $sScript;
176
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
177
178
    /**
179
     * @return array
180
     */
181
    private function getPathAsArray()
182
    {
183
        $sName = $this->sPath ?? 'this';
184
        $aCall = ['_type' => 'select', '_name' => $sName];
185
        if(($this->xContext))
186
        {
187
            $aCall['context'] = is_a($this->xContext, self::class) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
188
                $this->xContext->jsonSerialize() :$this->xContext;
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; 0 found
Loading history...
189
        }
190
        return $aCall;
191
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
192
193
    /**
194
     * Generate the jQuery call, when converting the response into json.
195
     *
196
     * @return array
197
     */
198
    public function jsonSerialize(): array
199
    {
200
        $aCalls = [$this->getPathAsArray()];
201
        foreach($this->aCalls as $xCall)
202
        {
203
            $aCalls[] = $xCall->jsonSerialize();
204
        }
205
        if($this->bToInt)
206
        {
207
            $aCalls[] = $this->toIntCall();
208
        }
209
        return ['_type' => $this->getType(), 'calls' => $aCalls];
210
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
211
}
212