Completed
Push — master ( 4e7071...99053a )
by Sébastien
02:43
created

StoryPointManager::getDoneStoryPoints()   C

Complexity

Conditions 11
Paths 3

Size

Total Lines 36
Code Lines 21

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 19
CRAP Score 11.0151

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 3
loc 36
ccs 19
cts 20
cp 0.95
rs 5.2653
cc 11
eloc 21
nc 3
nop 4
crap 11.0151

How to fix   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 TrelloBurndown\Manager;
4
5
use Trello\Model\Cardlist;
6
use TrelloBurndown\Client\TrelloClient;
7
use TrelloBurndown\Model\Sprint;
8
9
/**
10
 * Class StoryPointManager.
11
 */
12
class StoryPointManager
13
{
14
    /**
15
     * @var ActionManager
16
     */
17
    private $actionManager;
18
19
    /**
20
     * @var string
21
     */
22
    private $pattern = '/\(([0-9]*.?[0-9]*)\)/';
23
24
    /**
25
     * @var \Trello\Client
26
     */
27
    private $client;
28
29
    /**
30
     * StoryPointsManager constructor.
31
     *
32
     * @param TrelloClient  $trelloClient
33
     * @param ActionManager $actionManager
34
     * @param string        $pattern
35
     */
36 25
    public function __construct(TrelloClient $trelloClient, ActionManager $actionManager, String $pattern = null)
37
    {
38 25
        $this->client = $trelloClient->getClient();
39 25
        $this->actionManager = $actionManager;
40
41 25
        if (!is_null($pattern)) {
42 1
            $this->pattern = $pattern;
43
        }
44 25
    }
45
46
    /**
47
     * @param string $pattern
48
     */
49 1
    public function setPattern(String $pattern)
50
    {
51 1
        $this->pattern = $pattern;
52 1
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getPattern()
58
    {
59 1
        return $this->pattern;
60
    }
61
62
    /**
63
     * @param $name
64
     *
65
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be double|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
66
     */
67 13
    public function parseStoryPoints($name)
68
    {
69 13
        preg_match($this->pattern, $name, $matches);
70
71 13
        return isset($matches[1]) ? floatval($matches[1]) : 0;
72
    }
73
74
    /**
75
     * @param Cardlist $list
76
     *
77
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be double|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
     */
79 4
    public function countListStoryPoints(Cardlist $list)
80
    {
81 4
        $cards = $this->client->api('list')->cards()->all($list->getId());
82 4
        $totalSP = 0;
83
84 4
        foreach ($cards as $card) {
85 4
            if (!$card['closed']) {
86 4
                $totalSP += $this->parseStoryPoints($card['name']);
87
            }
88
        }
89
90 4
        return $totalSP;
91
    }
92
93
    /**
94
     * Return an array of done story points per day.
95
     *
96
     * @param array  $todoLists
97
     * @param array  $wipLists
98
     * @param array  $doneLists
99
     * @param Sprint $sprint
100
     *
101
     * @return array|null
102
     */
103 4
    public function getDoneStoryPoints(array $todoLists, array $wipLists, array $doneLists, Sprint $sprint)
104
    {
105 4
        $doneCards = $this->actionManager->getCardsMovedFromTodoToDone($todoLists, $wipLists, $doneLists);
106 4
        $sprintDays = $sprint->getSprintDays();
107 4
        $sp = [];
108
109 4
        if (!$sprintDays instanceof \DatePeriod) {
110
            return;
111
        }
112
113 4
        foreach ($sprintDays as $day) {
114 4
            $countSP = 0;
115 4
            if ($day instanceof \DateTime &&
116 4
                ($day->getTimestamp() > $sprint->getNextDayInSprint()->getTimestamp())) {
117 4
                break;
118
            }
119
120 4 View Code Duplication
            if ($day instanceof \DateTime && ($day->format('N') == 6 || $day->format('N') == 7)) {
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...
121 4
                continue;
122
            }
123
124 4
            foreach ($doneCards as $card) {
125 4
                $actionDate = new \DateTime($card['date']);
126
                if (
127 4
                    $actionDate->getTimestamp() > $sprint->getStart()->getTimestamp() &&
128 4
                    $actionDate->getTimestamp() < $day->getTimestamp()
129
                ) {
130 4
                    $countSP += $this->parseStoryPoints($card['card']);
131
                }
132
            }
133
134 4
            $sp[] = ['date' => $day, 'count' => $countSP];
135
        }
136
137 4
        return $sp;
138
    }
139
140
    /**
141
     * @param array  $todoLists
142
     * @param array  $wipLists
143
     * @param array  $doneLists
144
     * @param Sprint $sprint
145
     *
146
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
147
     */
148 3
    public function getTotalSprintStoryPoints(array $todoLists, array $wipLists, array $doneLists, Sprint $sprint)
149
    {
150 3
        $todoSP = 0;
151 3
        $wipSP = 0;
152
153 3
        foreach ($todoLists as $list) {
154 3
            $todoSP += $this->countListStoryPoints($list);
155
        }
156
157 3
        foreach ($wipLists as $list) {
158 3
            $wipSP += $this->countListStoryPoints($list);
159
        }
160
161 3
        $doneSP = $this->getDoneStoryPoints($todoLists, $wipLists, $doneLists, $sprint);
162
163 3
        $doneSP = end($doneSP);
164
165 3
        return $todoSP + $wipSP + $doneSP['count'];
166
    }
167
168
    /**
169
     * @param $totalOfSprint
170
     * @param Sprint $sprint
171
     *
172
     * @return float
173
     */
174 2
    public function getAverageStoryPointsPerDay($totalOfSprint, Sprint $sprint)
175
    {
176 2
        return round($totalOfSprint / $sprint->getTotalWorkDays(), 2);
177
    }
178
}
179