Sheet   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 98.08%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 7
dl 12
loc 126
ccs 51
cts 52
cp 0.9808
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A name() 0 4 1
A columns() 0 4 1
A rows() 0 4 1
A add() 0 8 2
A replace() 0 8 2
A get() 0 12 2
A has() 0 11 3
A position() 12 48 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Spreadsheet;
5
6
use Spreadsheet\Exception\{
7
    CellAlreadyExistsException,
8
    CellNotFoundException
9
};
10
use Innmind\Immutable\{
11
    Map,
12
    MapInterface
13
};
14
15
final class Sheet implements SheetInterface
16
{
17
    private $name;
18
    private $columns;
19
    private $rows;
20
21 17
    public function __construct(string $name)
22
    {
23 17
        $this->name = $name;
24 17
        $this->columns = new Map('scalar', ColumnInterface::class);
25 17
        $this->rows = new Map('scalar', RowInterface::class);
26 17
    }
27
28 15
    public function name(): string
29
    {
30 15
        return $this->name;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 9
    public function columns(): MapInterface
37
    {
38 9
        return $this->columns;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 9
    public function rows(): MapInterface
45
    {
46 9
        return $this->rows;
47
    }
48
49 11
    public function add(CellInterface $cell): SheetInterface
50
    {
51 11
        if ($this->has($cell->position())) {
52 1
            throw new CellAlreadyExistsException;
53
        }
54
55 11
        return $this->position($cell);
56
    }
57
58 2
    public function replace(CellInterface $cell): SheetInterface
59
    {
60 2
        if (!$this->has($cell->position())) {
61 1
            throw new CellNotFoundException;
62
        }
63
64 1
        return $this->position($cell);
65
    }
66
67 4
    public function get(Position $position): CellInterface
68
    {
69 4
        if (!$this->has($position)) {
70
            throw new CellNotFoundException;
71
        }
72
73
        return $this
74 4
            ->columns
75 4
            ->get($position->column())
76 4
            ->cells()
77 4
            ->get($position->row());
78
    }
79
80 12
    public function has(Position $position): bool
81
    {
82
        if (
83 12
            $this->columns->contains($position->column()) &&
84 12
            $this->columns->get($position->column())->cells()->contains($position->row())
85
        ) {
86 6
            return true;
87
        }
88
89 12
        return false;
90
    }
91
92 11
    private function position(CellInterface $cell): self
93
    {
94 11
        $column = $cell->position()->column();
95 11
        $row = $cell->position()->row();
96 11
        $columns = $this->columns;
97 11
        $rows = $this->rows;
98
99 11 View Code Duplication
        if (!$columns->contains($column)) {
100 11
            $columns = $columns->put(
101
                $column,
102 11
                new Column($column, new Map('scalar', CellInterface::class))
103
            );
104
        }
105
106 11 View Code Duplication
        if (!$rows->contains($row)) {
107 11
            $rows = $rows->put(
108
                $row,
109 11
                new Row($row, new Map('scalar', CellInterface::class))
110
            );
111
        }
112
113 11
        $columns = $columns->put(
114
            $column,
115 11
            new Column(
116
                $column,
117 11
                $columns->get($column)->cells()->put(
118
                    $row,
119
                    $cell
120
                )
121
            )
122
        );
123 11
        $rows = $rows->put(
124
            $row,
125 11
            new Row(
126
                $row,
127 11
                $rows->get($row)->cells()->put(
128
                    $column,
129
                    $cell
130
                )
131
            )
132
        );
133
134 11
        $sheet = new self($this->name);
135 11
        $sheet->rows = $rows;
136 11
        $sheet->columns = $columns;
137
138 11
        return $sheet;
139
    }
140
}
141