Completed
Push — 2.0 ( 29c56f...2f2bc4 )
by Marco
14:43
created

WorklogCommands   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 47.06 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 48
loc 102
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 5 1
B list() 0 30 2
A byId() 13 13 2
A byJid() 11 11 1
A byUid() 13 13 2
A byPid() 11 11 1
A getManager() 0 9 1

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 namespace Comodojo\Extender\Socket;
2
3
use \Comodojo\Daemon\Process;
4
use \Comodojo\Extender\Worklog\Manager;
5
use \Comodojo\Extender\Worklog\Transformer;
6
use \Comodojo\Foundation\Utils\ArrayOps;
7
use \League\Fractal\Manager as FractalManager;
8
use \League\Fractal\Resource\Item;
9
use \League\Fractal\Resource\Collection;
10
11
/**
12
* @package     Comodojo Extender
13
* @author      Marco Giovinazzi <[email protected]>
14
* @license     MIT
15
*
16
* LICENSE:
17
*
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
* THE SOFTWARE.
25
 */
26
27
class WorklogCommands {
28
29
    public function count(Process $daemon) {
30
31
        return self::getManager($daemon)->count();
32
33
    }
34
35
    public function list(Process $daemon, array $payload = null) {
36
37
        if ( empty($payload) ) {
38
39
            $data = self::getManager($daemon)->get();
40
41
        } else {
42
43
            $options = ArrayOps::replaceStrict([
44
                'limit' => 10,
45
                'offset' => 0,
46
                'reverse' => false
47
            ], $payload);
48
49
            $data = self::getManager($daemon)->get(
50
                [],
51
                $options['limit'],
52
                $options['offset'],
53
                $options['reverse']
54
            );
55
56
        }
57
58
        $resource = new Collection($data, new Transformer);
59
        $fractal = new FractalManager();
60
        $data = $fractal->createData($resource)->toArray();
61
62
        return $data['data'];
63
64
    }
65
66 View Code Duplication
    public function byId(Process $daemon, $id) {
0 ignored issues
show
Duplication introduced by
This method 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...
67
68
        $data = self::getManager($daemon)->getOne(['id' => $id]);
69
70
        if ( empty($data) ) return null;
71
72
        $resource = new Item($data, new Transformer);
73
        $fractal = new FractalManager();
74
        $data = $fractal->createData($resource)->toArray();
75
76
        return $data['data'];
77
78
    }
79
80 View Code Duplication
    public function byJid(Process $daemon, $jid) {
0 ignored issues
show
Duplication introduced by
This method 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...
81
82
        $data = self::getManager($daemon)->get(['jid' => $jid]);
83
84
        $resource = new Collection($data, new Transformer);
85
        $fractal = new FractalManager();
86
        $data = $fractal->createData($resource)->toArray();
87
88
        return $data['data'];
89
90
    }
91
92 View Code Duplication
    public function byUid(Process $daemon, $uid) {
0 ignored issues
show
Duplication introduced by
This method 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...
93
94
        $data = self::getManager($daemon)->getOne(['uid' => $uid]);
95
96
        if ( empty($data) ) return null;
97
98
        $resource = new Item($data, new Transformer);
99
        $fractal = new FractalManager();
100
        $data = $fractal->createData($resource)->toArray();
101
102
        return $data['data'];
103
104
    }
105
106 View Code Duplication
    public function byPid(Process $daemon, $pid) {
0 ignored issues
show
Duplication introduced by
This method 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...
107
108
        $data = self::getManager($daemon)->get(['pid' => $pid]);
109
110
        $resource = new Collection($data, new Transformer);
111
        $fractal = new FractalManager();
112
        $data = $fractal->createData($resource)->toArray();
113
114
        return $data['data'];
115
116
    }
117
118
    protected static function getManager(Process $daemon) {
119
120
        return new Manager(
121
            $daemon->getConfiguration(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Comodojo\Daemon\Process as the method getConfiguration() does only exist in the following sub-classes of Comodojo\Daemon\Process: Comodojo\Extender\ExtenderDaemon. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
122
            $daemon->getLogger(),
123
            $daemon->getEvents()
124
        );
125
126
    }
127
128
}
129