TransitionResponse   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 95
loc 95
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 19 19 6
B renderListTransitions() 32 32 4
A renderDoTransition() 23 23 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
 * Copyright © 2017 Toan Nguyen. All rights reserved.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Gojira\Jira\Response;
10
11
use Gojira\Api\Response\BaseResponse;
12
use Gojira\Api\Response\ResponseInterface;
13
use Symfony\Component\Console\Helper\TableCell;
14
15
/**
16
 * Render result for JIRA REST /transitions
17
 *
18
 * @package Gojira\Jira\Response
19
 * @author  Toan Nguyen <[email protected]>
20
 */
21 View Code Duplication
class TransitionResponse extends IssueResponse implements ResponseInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function render($type = null)
27
    {
28
        switch ($type) {
29
            case 'issue:transit:get':
30
                $result = $this->renderListTransitions();
31
                break;
32
            case 'issue:transit:start':
33
            case 'issue:transit:stop':
34
            case 'issue:transit:review':
35
            case 'issue:transit:done':
36
                $result = $this->renderDoTransition();
37
                break;
38
            default:
39
                $result = $this->renderNothing();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->renderNothing() (which targets Gojira\Api\Response\BaseResponse::renderNothing()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
40
                break;
41
        }
42
43
        return $result;
44
    }
45
46
    /**
47
     * Returns list of worklogs in a JIRA ticket
48
     * - Command: worklog:show (['worklog', 'wlog:s'])
49
     *
50
     * @return array
51
     */
52
    protected function renderListTransitions()
53
    {
54
        $rows = [];
55
56
        if ($this->response[self::TOTAL] === 0) {
57
            $rows[] = [new TableCell('No work yet logged.', ['colspan' => 5])];
58
        }
59
60
        $transitions = $this->response[self::TRANSITIONS];
61
        $totalTransitions = count($transitions);
62
        for ($counter = 0; $counter < $totalTransitions; $counter++) {
63
            $transitionId = $transitions[$counter][self::ID];
64
            $transitionName = $transitions[$counter][self::NAME];
65
            $author = $transitions[$counter][self::AUTHOR][self::DISPLAY_NAME];
66
            $timeSpent = $transitions[$counter][self::TIME_SPENT];
67
            $comment = $transitions[$counter][self::COMMENT];
68
69
            if (strlen($comment) > 50) {
70
                $comment = substr($comment, 0, 47) . '...';
71
            }
72
73
            $rows[] = [
74
                $transitionId,
75
                $transitionName,
76
                $author,
77
                $timeSpent,
78
                $comment
79
            ];
80
        }
81
82
        return $rows;
83
    }
84
85
    /**
86
     * Returns worklog data after added/updated in a JIRA ticket
87
     * - Command: worklog:add (['worklogadd', 'wlog:a'])
88
     * - Command: worklog:update (['worklogupdate', 'wlog:u'])
89
     *
90
     * @return array
91
     */
92
    protected function renderDoTransition()
93
    {
94
        $rows = [];
95
        $worklogId = $this->response[self::ID];
96
        $startDate = $this->response[self::CREATED];
97
        $author = $this->response[self::AUTHOR][self::DISPLAY_NAME];
98
        $timeSpent = $this->response[self::TIME_SPENT];
99
        $comment = $this->response[self::COMMENT];
100
101
        if (strlen($comment) > 50) {
102
            $comment = substr($comment, 0, 47) . '...';
103
        }
104
105
        $rows[] = [
106
            $worklogId,
107
            $startDate,
108
            $author,
109
            $timeSpent,
110
            $comment
111
        ];
112
113
        return $rows;
114
    }
115
}
116