Completed
Pull Request — master (#342)
by Luc
05:03
created

AgeRange   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 124
Duplicated Lines 8.06 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 22
lcom 0
cbo 2
dl 10
loc 124
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A guardValidAgeRange() 0 6 4
A getFrom() 0 4 1
A getTo() 0 4 1
A __toString() 0 7 3
C fromString() 10 43 11
A sameAs() 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
3
namespace CultuurNet\UDB3\Offer;
4
5
use ValueObjects\Person\Age;
6
7
class AgeRange
8
{
9
    /**
10
     * @var null|Age
11
     */
12
    private $from;
13
14
    /**
15
     * @var null|Age
16
     */
17
    private $to;
18
19
    /**
20
     * AgeRange constructor.
21
     * @param null|Age $from
22
     * @param null|Age $to
23
     */
24
    public function __construct(Age $from = null, Age $to = null)
25
    {
26
        $this->guardValidAgeRange($from, $to);
27
28
        $this->from = $from;
29
        $this->to = $to;
30
    }
31
32
    /**
33
     * @param null|Age $from
34
     * @param null|Age $to
35
     *
36
     * @throws InvalidAgeRangeException
37
     */
38
    private function guardValidAgeRange(Age $from = null, Age $to = null)
39
    {
40
        if ($from && $to && $from > $to) {
41
            throw new InvalidAgeRangeException('"from" age should not exceed "to" age');
42
        }
43
    }
44
45
    /**
46
     * @return null|Age
47
     */
48
    public function getFrom()
49
    {
50
        return $this->from;
51
    }
52
53
    /**
54
     * @return null|Age
55
     */
56
    public function getTo()
57
    {
58
        return $this->to;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function __toString()
65
    {
66
        $from = $this->from ? (string) $this->from : '';
67
        $to = $this->to ? (string) $this->to : '';
68
69
        return $from . '-' . $to;
70
    }
71
72
    /**
73
     * @param string $ageRangeString
74
     * @return AgeRange
75
     *
76
     * @throws InvalidAgeRangeException
77
     */
78
    public static function fromString($ageRangeString)
79
    {
80
        if (!is_string($ageRangeString)) {
81
            throw new InvalidAgeRangeException(
82
                'Date-range should be of type string.'
83
            );
84
        }
85
86
        $stringValues = explode('-', $ageRangeString);
87
88
        if (empty($stringValues) || !isset($stringValues[1])) {
89
            throw new InvalidAgeRangeException(
90
                'Date-range string is not valid because it is missing a hyphen.'
91
            );
92
        }
93
94
        if (count($stringValues) !== 2) {
95
            throw new InvalidAgeRangeException(
96
                'Date-range string is not valid because it has too many hyphens.'
97
            );
98
        }
99
100
        $fromString = $stringValues[0];
101
        $toString = $stringValues[1];
102
103 View Code Duplication
        if (is_numeric($fromString) || empty($fromString)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $from = is_numeric($fromString) ? new Age($fromString) : null;
105
        } else {
106
            throw new InvalidAgeRangeException(
107
                'The "from" age should be a natural number or empty.'
108
            );
109
        }
110
111 View Code Duplication
        if (is_numeric($toString) || empty($toString)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
            $to = is_numeric($toString) ? new Age($toString) : null;
113
        } else {
114
            throw new InvalidAgeRangeException(
115
                'The "to" age should be a natural number or empty.'
116
            );
117
        }
118
119
        return new self($from, $to);
120
    }
121
122
    /**
123
     * @param AgeRange $otherAgeRange
124
     * @return bool
125
     */
126
    public function sameAs(AgeRange $otherAgeRange)
127
    {
128
        return "$this" === "$otherAgeRange";
129
    }
130
}
131