Passed
Push — v5.x ( 26d105...7911f3 )
by Thierry
03:11
created

CommandTrait::appendResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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