AbstractCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 77
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 6 1
A table() 0 10 2
A elapsed() 0 18 2
A getStartTime() 0 4 1
A iAmHungry() 0 5 1
1
<?php namespace Tequilarapido\Cli\Commands\Base;
2
3
use Datum\Datum;
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\ConsoleEvents;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
abstract class AbstractCommand extends Command
10
{
11
    protected $startTime;
12
    protected $progress;
13
14
    /**
15
     * @var InputInterface
16
     */
17
    protected $input;
18
19
20
    /**
21
     * @var OutputInterface
22
     * We need it public, as it is used inside a closure in AbstractCommand::elapsed method
23
     */
24
    public $output;
25
26
27
    public function __construct($name = null)
28
    {
29
        parent::__construct($name);
30
    }
31
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        // Output
35
        $this->output = $output;
36
        $this->input = $input;
37
    }
38
39
    public function table($data)
40
    {
41
        $table = $this->getHelperSet()->get('table');
42
        $table->setHeaders(array('Property', 'Value'));
43
        foreach ($data as $key => $value) {
44
            $table->addRow(array($key, $value));
45
        }
46
47
        $table->render($this->output);
48
    }
49
50
51
    //
52
    // Time / Benchmarking stuff
53
    //
54
    protected function elapsed()
55
    {
56
        // Start
57
        $this->startTime = time();
58
        $self = $this;
59
60
        try {
61
            $this->getApplication()->getDispatcher()->addListener(ConsoleEvents::TERMINATE, function () use ($self) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getDispatcher() does only exist in the following sub-classes of Symfony\Component\Console\Application: Tequilarapido\Cli\Application. 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...
62
                $dt = Datum::createFromTimestamp($self->getStartTime());
63
                $elapsed = time() - $self->getStartTime();
64
65
                $self->output->info('');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method info() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
66
                $self->output->info('Took about ' . $dt->diffInMinutes() . ' min. ( ' . number_format($elapsed) . ' sec.) ');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method info() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
67
            });
68
        } catch (\Exception $e) {
69
            // Do nothing.
70
        }
71
    }
72
73
    public function getStartTime()
74
    {
75
        return $this->startTime;
76
    }
77
78
    protected function iAmHungry($memory_limit = '1024M')
79
    {
80
        ini_set('memory_limit', $memory_limit);
81
        set_time_limit(0);
82
    }
83
84
85
}