Passed
Push — v5.x ( 8eee51...080647 )
by Thierry
10:22
created

CommandTrait::str()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Response\Traits;
4
5
use Jaxon\Plugin\ResponsePlugin;
6
use Jaxon\Response\ResponseInterface;
7
use JsonSerializable;
8
9
use function array_filter;
10
use function array_merge;
11
use function count;
12
use function trim;
13
14
trait CommandTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait CommandTrait
Loading history...
15
{
16
    /**
17
     * The commands that will be sent to the browser in the response
18
     *
19
     * @var array
20
     */
21
    protected $aCommands = [];
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
22
23
    /**
24
     * Get the commands in the response
25
     *
26
     * @return array
27
     */
28
    public function getCommands(): array
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
29
    {
30
        return $this->aCommands;
31
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
32
33
    /**
34
     * Get the number of commands in the response
35
     *
36
     * @return int
37
     */
38
    public function getCommandCount(): int
39
    {
40
        return count($this->aCommands);
41
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
42
43
    /**
44
     * Clear all the commands already added to the response
45
     *
46
     * @return void
47
     */
48
    public function clearCommands()
49
    {
50
        $this->aCommands = [];
51
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
52
53
    /**
54
     * Merge the commands with those in this <Response> object
55
     *
56
     * @param array $aCommands    The commands to merge
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
57
     * @param bool $bBefore    Add the new commands to the beginning of the list
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
58
     *
59
     * @return void
60
     */
61
    public function appendCommands(array $aCommands, bool $bBefore = false)
62
    {
63
        $this->aCommands = ($bBefore) ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
64
            array_merge($aCommands, $this->aCommands) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
65
            array_merge($this->aCommands, $aCommands);
66
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
67
68
    /**
69
     * Merge the response commands with those in this <Response> object
70
     *
71
     * @param ResponseInterface $xResponse    The <Response> object
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
72
     * @param bool $bBefore    Add the new commands to the beginning of the list
0 ignored issues
show
Coding Style introduced by
Expected 14 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
73
     *
74
     * @return void
75
     */
76
    public function appendResponse(ResponseInterface $xResponse, bool $bBefore = false)
77
    {
78
        $this->appendCommands($xResponse->getCommands(), $bBefore);
79
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
80
81
    /**
82
     * Add a response command to the array of commands that will be sent to the browser
83
     *
84
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
85
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
86
     *
87
     * @return ResponseInterface
88
     */
89
    public function addCommand(string $sName, array|JsonSerializable $aOptions): ResponseInterface
90
    {
91
        $this->aCommands[] = [
92
            'cmd' => $this->str($sName),
93
            'options' => $aOptions,
94
        ];
95
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\CommandTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
96
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
    /**
99
     * Add a response command that is generated by a plugin
100
     *
101
     * @param ResponsePlugin $xPlugin    The plugin object
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
102
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
103
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
104
     *
105
     * @return ResponseInterface
106
     */
107
    public function addPluginCommand(ResponsePlugin $xPlugin, string $sName,
108
        array|JsonSerializable $aOptions): ResponseInterface
109
    {
110
        $this->aCommands[] = [
111
            'cmd' => $this->str($sName),
112
            'plg' => $xPlugin->getName(),
113
            'options' => $aOptions,
114
        ];
115
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Jaxon\Response\Traits\CommandTrait which is incompatible with the type-hinted return Jaxon\Response\ResponseInterface.
Loading history...
116
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
117
118
    /**
119
     * Convert to string
120
     *
121
     * @param mixed $xData
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
122
     *
123
     * @return string
124
     */
125
    protected function str($xData): string
126
    {
127
        return trim((string)$xData, " \t\n");
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Add a response command to the array of commands that will be sent to the browser
132
     *
133
     * @param string $sName    The command name
0 ignored issues
show
Coding Style introduced by
Expected 8 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 17 spaces after parameter type; 1 found
Loading history...
134
     * @param array|JsonSerializable $aOptions    The command options
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 4 found
Loading history...
135
     * @param bool $bRemoveEmpty    If true, remove empty options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
136
     *
137
     * @return ResponseInterface
138
     */
139
    protected function _addCommand(string $sName, array|JsonSerializable $aOptions,
140
        bool $bRemoveEmpty = false): ResponseInterface
141
    {
142
        if($bRemoveEmpty)
143
        {
144
            $aOptions = array_filter($aOptions, function($xOption) {
145
                return $xOption === '';
146
            });
147
        }
148
        return $this->addCommand($this->str($sName), $aOptions);
149
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
150
}
151