TimeTrackTrait   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 158
Duplicated Lines 55.7 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B ttCheck() 0 20 9
C ttHandleInM() 33 44 13
F ttHandle() 52 74 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
                continue;
22
            }
23
            $lastChr = substr($ttValue, -1);
24
            if ($lastChr !== 'w' && $lastChr !== 'd' && $lastChr !== 'h' && $lastChr !== 'm') {
25
                return false;
26
            }
27
            $ttNum = substr($ttValue, 0, -1);
28
            if ($ttNum && !is_numeric($ttNum)) {
29
                return false;
30
            }
31
        }
32
        return true;
33
    }
34
35
    /**
36
     * handle the timetracking in the minute.
37
     *
38
     * @return string
39
     */
40
    public function ttHandleInM($ttString)
41
    {
42
        if (!$ttString) {
43
            return '';
44
        }
45
        $W2D = 5;
46
        $D2H = 8;
47
        $setting = SysSetting::first();
48 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...
49
            if (isset($setting->properties['week2day'])) {
50
                $W2D = $setting->properties['week2day'];
51
            }
52
            if (isset($setting->properties['day2hour'])) {
53
                $D2H = $setting->properties['day2hour'];
54
            }
55
        }
56
        $W2M = $W2D * $D2H * 60;
57
        $D2M = $D2H * 60;
58
        $H2M = 60;
59
        $tt_in_min = 0;
60
        $ttString = strtolower(trim($ttString));
61
        $ttValues = explode(' ', $ttString);
62 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...
63
        {
64
            if (!$ttValue) {
65
                continue;
66
            }
67
            $lastChr = substr($ttValue, -1);
68
            $ttNum   = substr($ttValue, 0, -1) === '' ? 1 : substr($ttValue, 0, -1);
69
            if ($lastChr == 'w') {
70
                $tt_in_min += $ttNum * $W2M;
71
            }
72
            else if ($lastChr == 'd') {
73
                $tt_in_min += $ttNum * $D2M;
74
            }
75
            else if ($lastChr == 'h') {
76
                $tt_in_min += $ttNum * $H2M;
77
            }
78
            else if ($lastChr == 'm') {
79
                $tt_in_min += $ttNum;
80
            }
81
        }
82
        return $tt_in_min;
83
    }
84
    
85
    /**
86
     * handle the timetracking.
87
     *
88
     * @return string
89
     */
90
    public function ttHandle($ttString)
91
    {
92
        if (!$ttString) {
93
            return '';
94
        }
95
        
96
        $W2D = 5;
97
        $D2H = 8;
98
        $setting = SysSetting::first();
99 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...
100
            if (isset($setting->properties['week2day'])) {
101
                $W2D = $setting->properties['week2day'];
102
            }
103
            if (isset($setting->properties['day2hour'])) {
104
                $D2H = $setting->properties['day2hour'];
105
            }
106
        }
107
        $W2M = $W2D * $D2H * 60;
108
        $D2M = $D2H * 60;
109
        $H2M = 60;
110
        $tt_in_min = 0;
111
        $ttString = strtolower(trim($ttString));
112
        $ttValues = explode(' ', $ttString);
113 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...
114
        {
115
            if (!$ttValue) {
116
                continue;
117
            }
118
            $lastChr = substr($ttValue, -1);
119
            $ttNum   = substr($ttValue, 0, -1) === '' ? 1 : abs(substr($ttValue, 0, -1));
120
            if ($lastChr == 'w') {
121
                $tt_in_min += $ttNum * $W2M;
122
            }
123
            else if ($lastChr == 'd') {
124
                $tt_in_min += $ttNum * $D2M;
125
            }
126
            else if ($lastChr == 'h') {
127
                $tt_in_min += $ttNum * $H2M;
128
            }
129
            else if ($lastChr == 'm') {
130
                $tt_in_min += $ttNum;
131
            }
132
        }
133
        $newTT = [];
134
        $new_remain_min = ceil($tt_in_min);
135
        if ($new_remain_min >= 0) {
136
            $new_weeknum = floor($tt_in_min / $W2M);
137
            if ($new_weeknum > 0) {
138
                $newTT[] = $new_weeknum . 'w';
139
            }
140
        }
141
        $new_remain_min = $tt_in_min % $W2M;
142 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...
143
            $new_daynum = floor($new_remain_min / $D2M);
144
            if ($new_daynum > 0) {
145
                $newTT[] = $new_daynum . 'd';
146
            }
147
        }
148
        $new_remain_min = $new_remain_min % $D2M;
149 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...
150
            $new_hournum = floor($new_remain_min / $H2M);
151
            if ($new_hournum > 0) {
152
                $newTT[] = $new_hournum . 'h';
153
            }
154
        }
155
        $new_remain_min = $new_remain_min % $H2M;
156
        if ($new_remain_min > 0) {
157
            $newTT[] = $new_remain_min . 'm';
158
        }
159
        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...
160
            $newTT[] = '0m';
161
        }
162
        return (substr($ttString, 0, 1) == '-' ? '-' : '') . implode(' ', $newTT);
163
    }
164
}
165