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

RowManager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 39
ccs 6
cts 10
cp 0.6
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A applyStyle() 0 5 1
A isEmpty() 0 5 3
1
<?php
2
3
namespace Box\Spout\Writer\Common\Manager;
4
5
use Box\Spout\Writer\Common\Entity\Row;
6
use Box\Spout\Writer\Common\Entity\Style\Style;
7
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
8
9
class RowManager
10
{
11
    /**
12
     * @var StyleMerger
13
     */
14
    protected $styleMerger;
15
16
    /**
17
     * RowManager constructor.
18
     * @param StyleMerger $styleMerger
19
     */
20 81
    public function __construct(StyleMerger $styleMerger)
21
    {
22 81
        $this->styleMerger = $styleMerger;
23 81
    }
24
25
    /**
26
     * @param Style $style
27
     * @return $this
28
     */
29
    public function applyStyle(Row $row, Style $style)
30
    {
31
        $mergedStyle = $this->styleMerger->merge($row->getStyle(), $style);
0 ignored issues
show
Bug introduced by
It seems like $row->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...
32
        $row->setStyle($mergedStyle);
33
    }
34
35
    /**
36
     * Detect whether a row is considered empty.
37
     * An empty row has either no cells at all - or only one empty cell
38
     *
39
     * @param Row $row
40
     * @return bool
41
     */
42 71
    public function isEmpty(Row $row)
43
    {
44 71
        $cells = $row->getCells();
45 71
        return count($cells) === 0 || (count($cells) === 1 && $cells[0]->isEmpty());
46
    }
47
}