Completed
Pull Request — master (#6)
by
unknown
05:22
created

BuiltInFunctionsExtension   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 152
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 8
Bugs 1 Features 4
Metric Value
wmc 14
c 8
b 1
f 4
lcom 2
cbo 4
dl 24
loc 152
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getFunctions() 0 10 1
A now() 0 16 3
A startOfDay() 12 12 2
A endOfDay() 12 12 2
A currentUser() 0 4 1
A random() 0 4 1
A modifyDate() 0 10 2

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\Extension\Type\TableBundleFunctionExtension;
6
use Netdudes\DataSourceryBundle\Util\CurrentDateTimeProvider;
7
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
8
use Symfony\Component\Security\Core\SecurityContextInterface;
9
10
class BuiltInFunctionsExtension extends AbstractTableBundleExtension
11
{
12
    /**
13
     * @var TokenStorageInterface
14
     */
15
    private $tokenStorage;
16
17
    /**
18
     * @var CurrentDateTimeProvider
19
     */
20
    private $dateTimeProvider;
21
22
    /**
23
     * @param TokenStorageInterface   $tokenStorage
24
     * @param CurrentDateTimeProvider $dateTimeProvider
25
     */
26
    public function __construct(TokenStorageInterface $tokenStorage, CurrentDateTimeProvider $dateTimeProvider)
27
    {
28
        $this->tokenStorage = $tokenStorage;
29
        $this->dateTimeProvider = $dateTimeProvider;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function getName()
36
    {
37
        return 'table_bundle_extension_built_in';
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFunctions()
44
    {
45
        return [
46
            new TableBundleFunctionExtension('now', $this, 'now'),
47
            new TableBundleFunctionExtension('startOfDay', $this, 'startOfDay'),
48
            new TableBundleFunctionExtension('endOfDay', $this, 'endOfDay'),
49
            new TableBundleFunctionExtension('currentUser', $this, 'currentUser'),
50
            new TableBundleFunctionExtension('random', $this, 'random')
51
        ];
52
    }
53
54
    /**
55
     * Gets the current timestamp, with an offset string
56
     *
57
     * @param string $offset
58
     *
59
     * @return string
60
     * @throws \Exception
61
     */
62
    public function now($offset = null)
63
    {
64
        $now = clone $this->dateTimeProvider->get();
65
66
        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...
67
            $interval = \DateInterval::createFromDateString($offset);
68
            $now->add($interval);
69
70
            if ($now == $this->dateTimeProvider->get()) {
71
                // The date didn't change therefore we assume the given offset is not valid
72
                throw new \Exception($offset . ' is not a valid date/time interval.');
73
            }
74
        }
75
76
        return $now->format(\DateTime::ISO8601);
77
    }
78
79
    /**
80
     * Gets a date with the hour 00:00:00
81
     *
82
     * @param string $date
83
     *
84
     * @return string
85
     * @throws \Exception
86
     */
87 View Code Duplication
    public function startOfDay($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...
88
    {
89
        $now = clone $this->dateTimeProvider->get();
90
91
        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...
92
            $now = $this->modifyDate($now, $date);
93
        }
94
95
        $now->setTime(0, 0, 0);
96
97
        return $now->format(\DateTime::ISO8601);
98
    }
99
100
    /**
101
     * Gets a date with the hour 23:59:59
102
     *
103
     * @param string $date
104
     *
105
     * @return string
106
     * @throws \Exception
107
     */
108 View Code Duplication
    public function endOfDay($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...
109
    {
110
        $now = clone $this->dateTimeProvider->get();
111
112
        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...
113
            $now = $this->modifyDate($now, $date);
114
        }
115
116
        $now->setTime(23, 59, 59);
117
118
        return $now->format(\DateTime::ISO8601);
119
    }
120
121
    /**
122
     * Gets the current users' username
123
     *
124
     * @return string
125
     */
126
    public function currentUser()
127
    {
128
        return $this->tokenStorage->getToken()->getUsername();
129
    }
130
131
    /**
132
     * Gets a random value between $min and $max
133
     *
134
     * @param int $min
135
     * @param int $max
136
     *
137
     * @return int
138
     */
139
    public function random($min = 0, $max = 10)
140
    {
141
        return rand($min, $max);
142
    }
143
144
    /**
145
     * @param \DateTime $date
146
     * @param string    $change
147
     *
148
     * @return \DateTime
149
     * @throws \Exception
150
     */
151
    private function modifyDate(\DateTime $date, $change)
152
    {
153
        try {
154
            $date->modify($change);
155
        } catch (\Exception $e) {
156
            throw new \Exception($change . ' is not a valid date or date offset.');
157
        }
158
159
        return ($date);
160
    }
161
}
162