BuiltInFunctionsExtension   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 199
Duplicated Lines 16.08 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 2
cbo 3
dl 32
loc 199
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A startOfMonth() 16 16 2
A __construct() 0 5 1
A getFunctions() 0 12 1
A startOfDay() 0 12 2
A startOfWeek() 16 16 2
A startOfYear() 0 15 2
A currentUser() 0 4 1
A random() 0 4 1
A modifyDate() 0 10 2
A now() 0 16 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
3
namespace Netdudes\DataSourceryBundle\Extension;
4
5
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
8
class BuiltInFunctionsExtension implements UqlExtensionInterface
9
{
10
    /**
11
     * @var TokenStorageInterface
12
     */
13
    private $tokenStorage;
14
15
    /**
16
     * @var CurrentDateTimeProvider
17
     */
18
    private $dateTimeProvider;
19
20
    /**
21
     * @param TokenStorageInterface   $tokenStorage
22
     * @param CurrentDateTimeProvider $dateTimeProvider
23
     */
24
    public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider)
25
    {
26
        $this->tokenStorage = $tokenStorage;
27
        $this->dateTimeProvider = $dateTimeProvider;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getFunctions()
34
    {
35
        return [
36
            new UqlFunction('now', $this, 'now'),
37
            new UqlFunction('startOfDay', $this, 'startOfDay'),
38
            new UqlFunction('startOfWeek', $this, 'startOfWeek'),
39
            new UqlFunction('startOfMonth', $this, 'startOfMonth'),
40
            new UqlFunction('startOfYear', $this, 'startOfYear'),
41
            new UqlFunction('currentUser', $this, 'currentUser'),
42
            new UqlFunction('random', $this, 'random')
43
        ];
44
    }
45
46
    /**
47
     * Gets the current timestamp, with an offset string
48
     *
49
     * @param string $offset
50
     *
51
     * @return string
52
     * @throws \Exception
53
     */
54
    public function now($offset = null)
55
    {
56
        $now = clone $this->dateTimeProvider->get();
57
58
        if ($offset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
            $interval = \DateInterval::createFromDateString($offset);
60
            $now->add($interval);
61
62
            if ($now == $this->dateTimeProvider->get()) {
63
                // The date didn't change therefore we assume the given offset is not valid
64
                throw new \Exception($offset . ' is not a valid date/time interval.');
65
            }
66
        }
67
68
        return $now->format(\DateTime::ISO8601);
69
    }
70
71
    /**
72
     * Gets a date with the hour 00:00:00
73
     *
74
     * @param string $date
75
     *
76
     * @return string
77
     * @throws \Exception
78
     */
79
    public function startOfDay($date = null)
80
    {
81
        $now = clone $this->dateTimeProvider->get();
82
83
        if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
84
            $now = $this->modifyDate($now, $date);
85
        }
86
87
        $now->setTime(0, 0, 0);
88
89
        return $now->format(\DateTime::ISO8601);
90
    }
91
92
    /**
93
     * Gets the Monday of the week for the specified date with the hour 00:00:00
94
     *
95
     * @param string $date
96
     *
97
     * @return string
98
     * @throws \Exception
99
     */
100 View Code Duplication
    public function startOfWeek($date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
101
    {
102
        $now = clone $this->dateTimeProvider->get();
103
104
        if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
            $now = $this->modifyDate($now, $date);
106
        }
107
108
        $year = $now->format('o'); // o = ISO-8601 year number
109
        $week = $now->format('W'); // W = ISO-8601 week number of year, weeks starting on Monday
110
111
        $startOfWeek = $now->setISODate($year, $week);
112
        $startOfWeek->setTime(0, 0, 0);
113
114
        return $startOfWeek->format(\DateTime::ISO8601);
115
    }
116
117
    /**
118
     * Gets the first day of the month for the specified date with the hour 00:00:00
119
     *
120
     * @param string $date
121
     *
122
     * @return string
123
     * @throws \Exception
124
     */
125 View Code Duplication
    public function startOfMonth($date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
126
    {
127
        $now = clone $this->dateTimeProvider->get();
128
129
        if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
130
            $now = $this->modifyDate($now, $date);
131
        }
132
133
        $year = $now->format('Y');
134
        $month = $now->format('m');
135
136
        $startOfMonth = $now->setDate($year, $month, 1);
137
        $startOfMonth->setTime(0, 0, 0);
138
139
        return $startOfMonth->format(\DateTime::ISO8601);
140
    }
141
142
    /**
143
     * Gets the first day of the year for the specified date with the hour 00:00:00
144
     *
145
     * @param string $date
146
     *
147
     * @return string
148
     * @throws \Exception
149
     */
150
    public function startOfYear($date = null)
151
    {
152
        $now = clone $this->dateTimeProvider->get();
153
154
        if ($date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
155
            $now = $this->modifyDate($now, $date);
156
        }
157
158
        $year = $now->format('Y');
159
160
        $startOfYear = $now->setDate($year, 1, 1);
161
        $startOfYear->setTime(0, 0, 0);
162
163
        return $startOfYear->format(\DateTime::ISO8601);
164
    }
165
166
    /**
167
     * Gets the current users' username
168
     *
169
     * @return string
170
     */
171
    public function currentUser()
172
    {
173
        return $this->tokenStorage->getToken()->getUsername();
174
    }
175
176
    /**
177
     * Gets a random value between $min and $max
178
     *
179
     * @param int $min
180
     * @param int $max
181
     *
182
     * @return int
183
     */
184
    public function random($min = 0, $max = 10)
185
    {
186
        return rand($min, $max);
187
    }
188
189
    /**
190
     * @param \DateTime $date
191
     * @param string    $change
192
     *
193
     * @return \DateTime
194
     * @throws \Exception
195
     */
196
    private function modifyDate(\DateTime $date, $change)
197
    {
198
        try {
199
            $date->modify($change);
200
        } catch (\Exception $e) {
201
            throw new \Exception($change . ' is not a valid date or date offset.');
202
        }
203
204
        return ($date);
205
    }
206
}
207