Completed
Push — master ( a7df58...69cda4 )
by Toan
16s
created

WorklogResponse   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 17 17 4
B renderListWorklogs() 32 32 4
A renderAddUpdateWorklog() 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 /worklog
17
 *
18
 * @package Gojira\Jira\Response
19
 * @author  Toan Nguyen <[email protected]>
20
 */
21 View Code Duplication
class WorklogResponse extends IssueResponse implements ResponseInterface
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in 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...
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function render($type = null)
27
    {
28
        switch ($type) {
29
            case 'worklog:show':
30
                $result = $this->renderListWorklogs();
31
                break;
32
            case 'worklog:add':
33
            case 'worklog:update':
34
                $result = $this->renderAddUpdateWorklog();
35
                break;
36
            default:
37
                $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...
38
                break;
39
        }
40
41
        return $result;
42
    }
43
44
    /**
45
     * Returns list of worklogs in a JIRA ticket
46
     * - Command: worklog:show (['worklog', 'wlog:s'])
47
     *
48
     * @return array
49
     */
50
    protected function renderListWorklogs()
51
    {
52
        $rows = [];
53
54
        if ($this->response[self::TOTAL] === 0) {
55
            $rows[] = [new TableCell('No work yet logged.', ['colspan' => 5])];
56
        }
57
58
        $worklogs = $this->response[self::WORKLOGS];
59
        $totalWorklogs = count($worklogs);
60
        for ($counter = 0; $counter < $totalWorklogs; $counter++) {
61
            $worklogId = $worklogs[$counter][self::ID];
62
            $startDate = $worklogs[$counter][self::CREATED];
63
            $author = $worklogs[$counter][self::AUTHOR][self::DISPLAY_NAME];
64
            $timeSpent = $worklogs[$counter][self::TIME_SPENT];
65
            $comment = $worklogs[$counter][self::COMMENT];
66
67
            if (strlen($comment) > 50) {
68
                $comment = substr($comment, 0, 47) . '...';
69
            }
70
71
            $rows[] = [
72
                $worklogId,
73
                $startDate,
74
                $author,
75
                $timeSpent,
76
                $comment
77
            ];
78
        }
79
80
        return $rows;
81
    }
82
83
    /**
84
     * Returns worklog data after added/updated in a JIRA ticket
85
     * - Command: worklog:add (['worklogadd', 'wlog:a'])
86
     * - Command: worklog:update (['worklogupdate', 'wlog:u'])
87
     *
88
     * @return array
89
     */
90
    protected function renderAddUpdateWorklog()
91
    {
92
        $rows = [];
93
        $worklogId = $this->response[self::ID];
94
        $startDate = $this->response[self::CREATED];
95
        $author = $this->response[self::AUTHOR][self::DISPLAY_NAME];
96
        $timeSpent = $this->response[self::TIME_SPENT];
97
        $comment = $this->response[self::COMMENT];
98
99
        if (strlen($comment) > 50) {
100
            $comment = substr($comment, 0, 47) . '...';
101
        }
102
103
        $rows[] = [
104
            $worklogId,
105
            $startDate,
106
            $author,
107
            $timeSpent,
108
            $comment
109
        ];
110
111
        return $rows;
112
    }
113
}
114