Passed
Push — master ( d23948...c73ccc )
by Thierry
02:16
created

DataBag::readData()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 4
nc 3
nop 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Response\Plugin;
4
5
use function is_string;
6
use function json_decode;
7
use function is_array;
8
9
class DataBag extends \Jaxon\Plugin\Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class DataBag
Loading history...
10
{
11
    /**
12
     * @var DataBag\Bag
13
     */
14
    protected $xBag;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
15
16
    /**
17
     * The constructor
18
     */
19
    public function __construct()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
20
    {
21
        $aData = isset($_POST['jxnbags']) ? $this->readData($_POST) :
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ":"; newline found
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 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...
22
            (isset($_GET['jxnbags']) ? $this->readData($_GET) : []);
23
        $this->xBag = new DataBag\Bag($aData);
24
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
25
26
    /**
27
     * @param array $aFrom
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
28
     *
29
     * @return array
30
     */
31
    private function readData(array $aFrom)
32
    {
33
        // Todo: clean input data.
34
        if(is_string($aFrom['jxnbags']))
35
        {
36
            return json_decode($aFrom['jxnbags'], true) ?: [];
37
        }
38
        if(is_array($aFrom['jxnbags']))
39
        {
40
            return $aFrom['jxnbags'];
41
        }
42
        return [];
43
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
44
45
    /**
46
     * @inheritDoc
47
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
48
    public function getName()
49
    {
50
        return 'bags';
51
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
52
53
    /**
54
     * @inheritDoc
55
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
56
    public function getHash()
57
    {
58
        // Use the version number as hash
59
        return '1.0.0';
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 getReadyScript()
66
    {
67
        return '
68
    jaxon.command.handler.register("bags.set", function(args) {
69
        for (const bag in args.data) {
70
            jaxon.ajax.parameters.bags[bag] = args.data[bag];
71
        }
72
    });
73
';
74
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
75
76
    /**
77
     * @return bool
78
     */
79
    public function writeCommand()
80
    {
81
        if($this->xBag->touched())
82
        {
83
            $this->addCommand(['cmd' => 'bags.set'], $this->xBag->getAll());
0 ignored issues
show
Bug introduced by
$this->xBag->getAll() of type array is incompatible with the type string expected by parameter $sData of Jaxon\Plugin\Response::addCommand(). ( Ignorable by Annotation )

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

83
            $this->addCommand(['cmd' => 'bags.set'], /** @scrutinizer ignore-type */ $this->xBag->getAll());
Loading history...
84
        }
85
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
86
87
    /**
88
     * @param string $sName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
89
     *
90
     * @return DataBag\Context
91
     */
92
    public function bag($sName)
93
    {
94
        return new DataBag\Context($this->xBag, $sName);
95
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
96
}
97