Spreadsheet   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 43.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 28
loc 64
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A name() 0 4 1
A sheets() 0 4 1
A add() 14 14 2
A replace() 14 14 2
A has() 0 4 1
A get() 0 4 1

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
    SheetAlreadyExistsException,
8
    SheetNotFoundException
9
};
10
use Innmind\Immutable\{
11
    MapInterface,
12
    Map
13
};
14
15
final class Spreadsheet implements SpreadsheetInterface
16
{
17
    private $name;
18
    private $sheets;
19
20 14
    public function __construct(string $name)
21
    {
22 14
        $this->name = $name;
23 14
        $this->sheets = new Map('string', SheetInterface::class);
24 14
    }
25
26 11
    public function name(): string
27
    {
28 11
        return $this->name;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 9
    public function sheets(): MapInterface
35
    {
36 9
        return $this->sheets;
37
    }
38
39 11 View Code Duplication
    public function add(SheetInterface $sheet): SpreadsheetInterface
40
    {
41 11
        if ($this->has($sheet->name())) {
42 1
            throw new SheetAlreadyExistsException;
43
        }
44
45 11
        $self = new self($this->name);
46 11
        $self->sheets = $this->sheets->put(
47 11
            $sheet->name(),
48
            $sheet
49
        );
50
51 11
        return $self;
52
    }
53
54 2 View Code Duplication
    public function replace(SheetInterface $sheet): SpreadsheetInterface
55
    {
56 2
        if (!$this->has($sheet->name())) {
57 1
            throw new SheetNotFoundException;
58
        }
59
60 1
        $self = new self($this->name);
61 1
        $self->sheets = $this->sheets->put(
62 1
            $sheet->name(),
63
            $sheet
64
        );
65
66 1
        return $self;
67
    }
68
69 12
    public function has(string $sheet): bool
70
    {
71 12
        return $this->sheets->contains($sheet);
72
    }
73
74 4
    public function get(string $sheet): SheetInterface
75
    {
76 4
        return $this->sheets->get($sheet);
77
    }
78
}
79