Completed
Push — master ( ab60f2...c901a1 )
by Ricardo
07:00
created

TimeTrackTrait   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 187
Duplicated Lines 47.06 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 44
lcom 0
cbo 0
dl 88
loc 187
rs 8.8798
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B ttCheck() 0 23 9
C ttHandleInM() 36 53 13
F ttHandle() 52 91 22

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TimeTrackTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TimeTrackTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Fabrica\System\Eloquent\SysSetting;
6
7
trait TimeTrackTrait
8
{
9
    /**
10
     * check the timetracking.
11
     *
12
     * @return bool
13
    */
14
    public function ttCheck($ttString)
15
    {
16
        $ttString = strtolower(trim($ttString));
17
        $ttValues = explode(' ', $ttString);
18
        foreach ($ttValues as $ttValue)
19
        {
20
            if (!$ttValue)
21
            {
22
                continue;
23
            }
24
            $lastChr = substr($ttValue, -1);
25
            if ($lastChr !== 'w' && $lastChr !== 'd' && $lastChr !== 'h' && $lastChr !== 'm')
26
            {
27
                return false;
28
            }
29
            $ttNum = substr($ttValue, 0, -1);
30
            if ($ttNum && !is_numeric($ttNum))
31
            {
32
                return false;
33
            }
34
        }
35
        return true;
36
    }
37
38
    /**
39
     * handle the timetracking in the minute.
40
     *
41
     * @return string
42
     */
43
    public function ttHandleInM($ttString)
44
    {
45
        if (!$ttString)
46
        {
47
            return '';
48
        }
49
        $W2D = 5;
50
        $D2H = 8;
51
        $setting = SysSetting::first();
52 View Code Duplication
        if ($setting && isset($setting->properties))
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...
53
        {
54
            if (isset($setting->properties['week2day']))
55
            {
56
                $W2D = $setting->properties['week2day'];
57
            }
58
            if (isset($setting->properties['day2hour']))
59
            {
60
                $D2H = $setting->properties['day2hour'];
61
            }
62
        }
63
        $W2M = $W2D * $D2H * 60;
64
        $D2M = $D2H * 60;
65
        $H2M = 60;
66
        $tt_in_min = 0;
67
        $ttString = strtolower(trim($ttString));
68
        $ttValues = explode(' ', $ttString);
69 View Code Duplication
        foreach ($ttValues as $ttValue)
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...
70
        {
71
            if (!$ttValue)
72
            {
73
                continue;
74
            }
75
            $lastChr = substr($ttValue, -1);
76
            $ttNum   = substr($ttValue, 0, -1) === '' ? 1 : substr($ttValue, 0, -1);
77
            if ($lastChr == 'w')
78
            {
79
                $tt_in_min += $ttNum * $W2M;
80
            }
81
            else if ($lastChr == 'd')
82
            {
83
                $tt_in_min += $ttNum * $D2M;
84
            }
85
            else if ($lastChr == 'h')
86
            {
87
                $tt_in_min += $ttNum * $H2M;
88
            }
89
            else if ($lastChr == 'm')
90
            {
91
                $tt_in_min += $ttNum;
92
            }
93
        }
94
        return $tt_in_min;
95
    }
96
    
97
    /**
98
     * handle the timetracking.
99
     *
100
     * @return string
101
     */
102
    public function ttHandle($ttString)
103
    {
104
        if (!$ttString)
105
        {
106
            return '';
107
        }
108
        
109
        $W2D = 5;
110
        $D2H = 8;
111
        $setting = SysSetting::first();
112 View Code Duplication
        if ($setting && isset($setting->properties))
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...
113
        {
114
            if (isset($setting->properties['week2day']))
115
            {
116
                $W2D = $setting->properties['week2day'];
117
            }
118
            if (isset($setting->properties['day2hour']))
119
            {
120
                $D2H = $setting->properties['day2hour'];
121
            }
122
        }
123
        $W2M = $W2D * $D2H * 60;
124
        $D2M = $D2H * 60;
125
        $H2M = 60;
126
        $tt_in_min = 0;
127
        $ttString = strtolower(trim($ttString));
128
        $ttValues = explode(' ', $ttString);
129 View Code Duplication
        foreach ($ttValues as $ttValue)
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...
130
        {
131
            if (!$ttValue)
132
            {
133
                continue;
134
            }
135
            $lastChr = substr($ttValue, -1);
136
            $ttNum   = substr($ttValue, 0, -1) === '' ? 1 : abs(substr($ttValue, 0, -1));
137
            if ($lastChr == 'w')
138
            {
139
                $tt_in_min += $ttNum * $W2M;
140
            }
141
            else if ($lastChr == 'd')
142
            {
143
                $tt_in_min += $ttNum * $D2M;
144
            }
145
            else if ($lastChr == 'h')
146
            {
147
                $tt_in_min += $ttNum * $H2M;
148
            }
149
            else if ($lastChr == 'm')
150
            {
151
                $tt_in_min += $ttNum;
152
            }
153
        }
154
        $newTT = [];
155
        $new_remain_min = ceil($tt_in_min);
156
        if ($new_remain_min >= 0)
157
        {
158
            $new_weeknum = floor($tt_in_min / $W2M);
159
            if ($new_weeknum > 0)
160
            {
161
                $newTT[] = $new_weeknum . 'w';
162
            }
163
        }
164
        $new_remain_min = $tt_in_min % $W2M;
165 View Code Duplication
        if ($new_remain_min >= 0)
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...
166
        {
167
            $new_daynum = floor($new_remain_min / $D2M);
168
            if ($new_daynum > 0)
169
            {
170
                $newTT[] = $new_daynum . 'd';
171
            }
172
        }
173
        $new_remain_min = $new_remain_min % $D2M;
174 View Code Duplication
        if ($new_remain_min >= 0)
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...
175
        {
176
            $new_hournum = floor($new_remain_min / $H2M);
177
            if ($new_hournum > 0)
178
            {
179
                $newTT[] = $new_hournum . 'h';
180
            }
181
        }
182
        $new_remain_min = $new_remain_min % $H2M;
183
        if ($new_remain_min > 0)
184
        {
185
            $newTT[] = $new_remain_min . 'm';
186
        }
187
        if (!$newTT)
0 ignored issues
show
Bug Best Practice introduced by
The expression $newTT of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
188
        {
189
            $newTT[] = '0m';
190
        }
191
        return (substr($ttString, 0, 1) == '-' ? '-' : '') . implode(' ', $newTT);
192
    }
193
}
194