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

Add::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51
Code Lines 29

Duplication

Lines 3
Ratio 5.88 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 3
loc 51
ccs 0
cts 36
cp 0
rs 8.6588
c 0
b 0
f 0
cc 6
eloc 29
nc 10
nop 2
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Add {
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 (empty($schedule_message['name']) || empty($schedule_message['expression'])) {
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
            throw new RpcException("Missing schedule name or invalid expression", -32600);
23
        }
24
25
        try {
26
27
            $request = TaskRequest::createFromMessage(
28
                TaskRequestMessage::createFromExport($request_message)
29
            );
30
31
        } catch (Exception $e) {
32
            throw new RpcException("Invalid message payload in request", -32600);
33
        }
34
35
        try {
36
37
            $schedule = new Schedule();
38
            $schedule->setName($schedule_message['name']);
39
            $schedule->setExpression(CronExpression::factory($schedule_message['expression']));
40
            $schedule->setDescription($schedule_message['description']);
41
            $schedule->setEnabled($schedule_message['enabled']);
42
            $schedule->setRequest($request);
43
44
        } catch (Exception $e) {
45
            throw new RpcException("Invalid message payload in schedule", -32600);
46
        }
47
48
        $manager = new Manager(
49
            $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...
50
            $daemon->getLogger(),
51
            $daemon->getEvents()
52
        );
53
54
        try {
55
            $id = $manager->add($schedule);
56
        } catch (Exception $e) {
57
            throw new RpcException($e->getMessage(), -32500);
58
        }
59
60
        $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...
61
62
        // should method ignore invalid refresh message here?
63
64
        return $id;
65
66
    }
67
68
}
69