Completed
Push — 2.0 ( 2f2bc4...95e7c3 )
by Marco
35:36 queued 01:24
created

Edit   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 11.67 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 9
dl 7
loc 60
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 7 56 7

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\Commands\Scheduler;
2
3
use \Comodojo\Extender\Schedule\Manager;
4
use \Comodojo\Daemon\Daemon;
5
use \Comodojo\RpcServer\Request\Parameters;
6
use \Comodojo\Extender\Socket\Messages\Task\Request as TaskRequestMessage;
7
use \Comodojo\Extender\Socket\Messages\Scheduler\Schedule as ScheduleMessage;
8
use \Comodojo\Extender\Task\Request as TaskRequest;
9
use \Comodojo\Extender\Orm\Entities\Schedule;
10
use \Cron\CronExpression;
11
use \Comodojo\Exception\RpcException;
12
use \Exception;
13
14
class Edit {
15
16
    public static function execute(Parameters $params, Daemon $daemon) {
17
18
        $schedule_message = $params->get('schedule');
19
        $request_message = $params->get('request');
20
21 View Code Duplication
        if (
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...
22
            empty($schedule_message['id']) ||
23
            empty($schedule_message['name']) ||
24
            empty($schedule_message['expression'])
25
        ) {
26
            throw new RpcException("Missing schedule name, id or invalid expression", -32600);
27
        }
28
29
        try {
30
31
            $request = TaskRequest::createFromMessage(
32
                TaskRequestMessage::createFromExport($request_message)
33
            );
34
35
        } catch (Exception $e) {
36
            throw new RpcException("Invalid message payload in request", -32600);
37
        }
38
39
        try {
40
41
            $schedule = new Schedule();
42
            $schedule->setId($schedule_message['id']);
43
            $schedule->setName($schedule_message['name']);
44
            $schedule->setExpression(CronExpression::factory($schedule_message['expression']));
45
            $schedule->setDescription($schedule_message['description']);
46
            $schedule->setEnabled($schedule_message['enabled']);
47
            $schedule->setRequest($request);
48
49
        } catch (Exception $e) {
50
            throw new RpcException("Invalid message payload in schedule", -32600);
51
        }
52
53
        $manager = new Manager(
54
            $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\Daemon as the method getConfiguration() does only exist in the following sub-classes of Comodojo\Daemon\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...
55
            $daemon->getLogger(),
56
            $daemon->getEvents()
57
        );
58
59
        try {
60
            $result = $manager->edit($schedule);
61
        } catch (Exception $e) {
62
            throw new RpcException($e->getMessage(), -32500);
63
        }
64
65
        $refresh = Refresh::execute($params, $daemon);
0 ignored issues
show
Unused Code introduced by
$refresh is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
67
        // should method ignore invalid refresh message here?
68
69
        return $result;
70
71
    }
72
73
}
74