TimeTrackTrait::ttHandle()   F
last analyzed

Complexity

Conditions 22
Paths 12961

Size

Total Lines 74

Duplication

Lines 52
Ratio 70.27 %

Importance

Changes 0
Metric Value
dl 52
loc 74
rs 0
c 0
b 0
f 0
cc 22
nc 12961
nop 1

How to fix   Long Method    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 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