Completed
Push — master ( 95af33...058941 )
by Wachter
10:15 queued 07:52
created

DebugTasksCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 2
Metric Value
c 2
b 1
f 2
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Task\TaskBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Task\Storage\StorageInterface;
11
12
/**
13
 * Run pending tasks.
14
 *
15
 * @author @wachterjohannes <[email protected]>
16
 */
17
class DebugTasksCommand extends Command
18
{
19
    /**
20
     * @var StorageInterface
21
     */
22
    private $storage;
23
24
    public function __construct($name, StorageInterface $storage)
25
    {
26
        parent::__construct($name);
27
28
        $this->storage = $storage;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function configure()
35
    {
36
        $this->setDescription('Debug tasks')
37
            ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, '', null)
38
            ->addOption('key', 'k', InputOption::VALUE_REQUIRED, '', null);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46
        $key = $input->getOption('key');
47
        $limit = $input->getOption('limit');
48
49
        if (null !== $key) {
50
            $tasks = $this->storage->findByKey($key, $limit);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Task\Storage\StorageInterface as the method findByKey() does only exist in the following implementations of said interface: Task\TaskBundle\Storage\DoctrineStorage.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
        } else {
52
            $tasks = $this->storage->findAll($limit);
0 ignored issues
show
Unused Code introduced by
The call to StorageInterface::findAll() has too many arguments starting with $limit.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
53
        }
54
55
        $table = new Table($output);
56
        $table->setHeaders(array('uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration'));
57
58
        foreach ($tasks as $task) {
59
            $start = null;
60
            $duration = null;
61
            if ($task->getLastExecution()) {
62
                $start = $task->getLastExecution()->getFinishedAtAsDateTime()->format(\DateTime::RFC3339);
63
                $duration = $task->getLastExecution()->getExecutionDuration();
64
            }
65
66
            $table->addRow(
67
                [
68
                    $task->getUuid(),
69
                    $task->getKey(),
70
                    $task->getTaskName(),
71
                    $task->getExecutionDate()->format(\DateTime::RFC3339),
72
                    $task->isCompleted(),
73
                    $start,
74
                    $duration,
75
                ]
76
            );
77
        }
78
79
        $table->render();
80
    }
81
}
82