Failed Conditions
Pull Request — develop_3.0 (#434)
by Hura
03:29
created

Row::isEmpty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Box\Spout\Writer\Common\Entity;
4
5
use Box\Spout\Writer\Common\Entity\Style\Style;
6
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
7
8
class Row
9
{
10
    /**
11
     * The cells in this row
12
     * @var array
13
     */
14
    protected $cells = [];
15
16
    /**
17
     * The row style
18
     * @var null|Style
19
     */
20
    protected $style = null;
21
22
    /**
23
     * @var StyleMerger
24
     */
25
    protected $styleMerger;
26
27
    /**
28
     * Row constructor.
29
     * @param Cell[] $cells
30
     * @param Style|null $style
31
     */
32 88
    public function __construct(array $cells = [], Style $style = null)
33
    {
34
        $this
35 88
            ->setCells($cells)
36 88
            ->setStyle($style);
0 ignored issues
show
Bug introduced by
It seems like $style defined by parameter $style on line 32 can be null; however, Box\Spout\Writer\Common\Entity\Row::setStyle() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
37
38 88
        $this->styleMerger = new StyleMerger();
39 88
    }
40
41
    /**
42
     * @return Cell[] $cells
43
     */
44 76
    public function getCells()
45
    {
46 76
        return $this->cells;
47
    }
48
49
    /**
50
     * @param array $cells
51
     * @return $this
52
     */
53 88
    public function setCells(array $cells)
54
    {
55 88
        $this->cells = [];
56 88
        foreach ($cells as $cell) {
57 87
            $this->addCell($cell);
58
        }
59 88
        return $this;
60
    }
61
62
    /**
63
     * @return Style
64
     */
65 64
    public function getStyle()
66
    {
67 64
        if (!isset($this->style)) {
68 48
            $this->setStyle(new Style());
69
        }
70 64
        return $this->style;
71
    }
72
73
    /**
74
     * @param Style $style
75
     * @return Row
76
     */
77 88
    public function setStyle($style)
78
    {
79 88
        $this->style = $style;
80 88
        return $this;
81
    }
82
83
    /**
84
     * @param Style $style|null
0 ignored issues
show
Bug introduced by
There is no parameter named $style|null. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
85
     * @return Row
86
     */
87
    public function applyStyle(Style $style = null)
88
    {
89
        if ($style === null) {
90
            return $this;
91
        }
92
        $merged = $this->styleMerger->merge($this->getStyle(), $style);
0 ignored issues
show
Bug introduced by
It seems like $this->getStyle() can be null; however, merge() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
93
        $this->setStyle($merged);
94
        return $this;
95
    }
96
97
    /**
98
     * @param Cell $cell
99
     * @return Row
100
     */
101 88
    public function addCell(Cell $cell)
102
    {
103 88
        $this->cells[] = $cell;
104 88
        return $this;
105
    }
106
107
    /**
108
     * Detect whether this row is considered empty.
109
     * An empty row has either no cells at all - or only empty cells
110
     *
111
     * @return bool
112
     */
113 33
    public function isEmpty()
114
    {
115 33
        return count($this->cells) === 0 || (count($this->cells) === 1 && $this->cells[0]->isEmpty());
116
    }
117
}
118