AgeRange::fromString()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 43

Duplication

Lines 10
Ratio 23.26 %

Importance

Changes 0
Metric Value
dl 10
loc 43
rs 7.3166
c 0
b 0
f 0
cc 11
nc 10
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use CultuurNet\UDB3\Model\ValueObject\Audience\AgeRange as Udb3ModelAgeRange;
6
use ValueObjects\Person\Age;
7
8
class AgeRange
9
{
10
    /**
11
     * @var null|Age
12
     */
13
    private $from;
14
15
    /**
16
     * @var null|Age
17
     */
18
    private $to;
19
20
    /**
21
     * AgeRange constructor.
22
     * @param null|Age $from
23
     * @param null|Age $to
24
     */
25
    public function __construct(Age $from = null, Age $to = null)
26
    {
27
        $from = $from ?: new Age(0);
28
29
        $this->guardValidAgeRange($from, $to);
30
31
        $this->from = $from;
32
        $this->to = $to;
33
    }
34
35
    /**
36
     * @param Age $from
37
     * @param null|Age $to
38
     *
39
     * @throws InvalidAgeRangeException
40
     */
41
    private function guardValidAgeRange(Age $from, Age $to = null)
42
    {
43
        if ($from && $to && $from > $to) {
44
            throw new InvalidAgeRangeException('"from" age should not exceed "to" age');
45
        }
46
    }
47
48
    /**
49
     * @return Age
50
     */
51
    public function getFrom()
52
    {
53
        return $this->from;
54
    }
55
56
    /**
57
     * @return null|Age
58
     */
59
    public function getTo()
60
    {
61
        return $this->to;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function __toString()
68
    {
69
        $from = $this->from ? (string) $this->from : '';
70
        $to = $this->to ? (string) $this->to : '';
71
72
        return $from . '-' . $to;
73
    }
74
75
    /**
76
     * @param string $ageRangeString
77
     * @return AgeRange
78
     *
79
     * @throws InvalidAgeRangeException
80
     */
81
    public static function fromString($ageRangeString)
82
    {
83
        if (!is_string($ageRangeString)) {
84
            throw new InvalidAgeRangeException(
85
                'Date-range should be of type string.'
86
            );
87
        }
88
89
        $stringValues = explode('-', $ageRangeString);
90
91
        if (empty($stringValues) || !isset($stringValues[1])) {
92
            throw new InvalidAgeRangeException(
93
                'Date-range string is not valid because it is missing a hyphen.'
94
            );
95
        }
96
97
        if (count($stringValues) !== 2) {
98
            throw new InvalidAgeRangeException(
99
                'Date-range string is not valid because it has too many hyphens.'
100
            );
101
        }
102
103
        $fromString = $stringValues[0];
104
        $toString = $stringValues[1];
105
106 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...
107
            $from = is_numeric($fromString) ? new Age($fromString) : null;
108
        } else {
109
            throw new InvalidAgeRangeException(
110
                'The "from" age should be a natural number or empty.'
111
            );
112
        }
113
114 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...
115
            $to = is_numeric($toString) ? new Age($toString) : null;
116
        } else {
117
            throw new InvalidAgeRangeException(
118
                'The "to" age should be a natural number or empty.'
119
            );
120
        }
121
122
        return new self($from, $to);
123
    }
124
125
    /**
126
     * @param AgeRange $otherAgeRange
127
     * @return bool
128
     */
129
    public function sameAs(AgeRange $otherAgeRange)
130
    {
131
        return "$this" === "$otherAgeRange";
132
    }
133
134
    /**
135
     * @param Udb3ModelAgeRange $udb3ModelAgeRange
136
     * @return AgeRange
137
     */
138
    public static function fromUbd3ModelAgeRange(Udb3ModelAgeRange $udb3ModelAgeRange)
139
    {
140
        $from = null;
0 ignored issues
show
Unused Code introduced by
$from is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
141
        if ($from = $udb3ModelAgeRange->getFrom()) {
142
            $from = new Age($from->toInteger());
143
        }
144
145
        $to = null;
0 ignored issues
show
Unused Code introduced by
$to is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
146
        if ($to = $udb3ModelAgeRange->getTo()) {
147
            $to = new Age($to->toInteger());
148
        }
149
150
        return new AgeRange($from, $to);
151
    }
152
}
153