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

ScheduleCommands   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 8.2 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A schedulerRefresh() 0 5 1
A schedulerAdd() 0 9 1
A schedulerGet() 0 5 1
A schedulerGetByName() 0 5 1
A schedulerEdit() 0 9 1
A schedulerRemove() 0 9 1
A schedulerRemoveByName() 0 9 1
A schedulerEnable() 0 9 1
A schedulerEnableByName() 0 9 1
A schedulerDisable() 0 9 1
A schedulerDisableByName() 0 9 1
A schedulerInfo() 10 10 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\Extender\Schedule\Manager;
4
use \Comodojo\Daemon\Process;
5
use \Comodojo\Extender\Orm\Entities\Schedule;
6
7
/**
8
* @package     Comodojo Extender
9
* @author      Marco Giovinazzi <[email protected]>
10
* @license     MIT
11
*
12
* LICENSE:
13
*
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
* THE SOFTWARE.
21
 */
22
23
class ScheduleCommands {
24
25
    public function schedulerRefresh(Process $daemon, $data = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
27
        return $daemon->getWorkers()->get("scheduler")->getOutputChannel()->send('refresh');
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 getWorkers() does only exist in the following sub-classes of Comodojo\Daemon\Process: Comodojo\Daemon\Daemon, Comodojo\Daemon\Tests\Mock\Daemon, 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...
28
29
    }
30
31
    public function schedulerAdd(Process $daemon, Schedule $schedule) {
32
33
        $id = self::getManager($daemon)->add($schedule);
34
35
        $this->schedulerRefresh($daemon);
36
37
        return $id;
38
39
    }
40
41
    public function schedulerGet(Process $daemon, $id) {
42
43
        return self::getManager($daemon)->get($id);
44
45
    }
46
47
    public function schedulerGetByName(Process $daemon, $name) {
48
49
        return self::getManager($daemon)->getByName($name);
50
51
    }
52
53
    public function schedulerEdit(Process $daemon, Schedule $schedule) {
54
55
        $edit = self::getManager($daemon)->edit($schedule);
56
57
        $this->schedulerRefresh($daemon);
58
59
        return $edit;
60
61
    }
62
63
    public function schedulerRemove(Process $daemon, $id) {
64
65
        $remove = self::getManager($daemon)->remove($id);
66
67
        $this->schedulerRefresh($daemon);
68
69
        return $remove;
70
71
    }
72
73
    public function schedulerRemoveByName(Process $daemon, $name) {
74
75
        $remove = self::getManager($daemon)->removeByName($name);
76
77
        $this->schedulerRefresh($daemon);
78
79
        return $remove;
80
81
    }
82
83
    public function schedulerEnable(Process $daemon, $id) {
84
85
        $enable = self::getManager($daemon)->enable($id);
86
87
        $this->schedulerRefresh($daemon);
88
89
        return $enable;
90
91
    }
92
93
    public function schedulerEnableByName(Process $daemon, $name) {
94
95
        $enable = self::getManager($daemon)->enableByName($name);
96
97
        $this->schedulerRefresh($daemon);
98
99
        return $enable;
100
101
    }
102
103
    public function schedulerDisable(Process $daemon, $id) {
104
105
        $disable = self::getManager($daemon)->disable($id);
106
107
        $this->schedulerRefresh($daemon);
108
109
        return $disable;
110
111
    }
112
113
    public function schedulerDisableByName(Process $daemon, $name) {
114
115
        $disable = self::getManager($daemon)->disableByName($name);
116
117
        $this->schedulerRefresh($daemon);
118
119
        return $disable;
120
121
    }
122
123 View Code Duplication
    public function schedulerInfo(Process $daemon, $data = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
124
125
        $configuration = $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...
126
        $base_path = $configuration->get('base-path');
127
        $lock_path = $configuration->get('run-path');
128
        $lock_file = "$base_path/$lock_path/schedule.worker.lock";
129
130
        return unserialize(file_get_contents($lock_file));
131
132
    }
133
134
    protected static function getManager(Process $daemon) {
135
136
        return new Manager(
137
            $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...
138
            $daemon->getLogger(),
139
            $daemon->getEvents()
140
        );
141
142
    }
143
144
}
145