Completed
Push — master ( a3d6a1...753d5f )
by Peter
02:58
created

src/TreeHouse/WorkerBundle/Command/ListCommand.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace TreeHouse\WorkerBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\Table;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use TreeHouse\WorkerBundle\QueueManager;
12
13
class ListCommand extends Command
14
{
15
    /**
16
     * @var QueueManager
17
     */
18
    protected $manager;
19
20
    /**
21
     * @var int
22
     */
23
    protected $lineCount;
24
25
    /**
26
     * @param QueueManager $queueManager
27
     */
28
    public function __construct(QueueManager $queueManager)
29
    {
30
        $this->manager = $queueManager;
31
32
        parent::__construct();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('worker:list')
42
            ->addArgument('action', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Selects which actions to clear. Defaults to all actions')
43
            ->addOption('refresh', null, InputOption::VALUE_OPTIONAL, 'Refresh rate in seconds, only applies for interactive commands', 1)
44
            ->addOption('empty', null, InputOption::VALUE_NONE, 'Show empty actions')
45
            ->setDescription('List actions and their stats')
46
        ;
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $actions = $input->getArgument('action');
55
        if (empty($actions)) {
56
            $actions = array_keys($this->manager->getExecutors());
57
        }
58
59
        $empty   = $input->getOption('empty');
60
        $refresh = $input->getOption('refresh');
61
62
        if ($input->isInteractive()) {
63
            $output->writeln([
0 ignored issues
show
array('', sprintf('This ...o exit', $refresh), '') is of type array<integer,string,{"0..."string","2":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
                '',
65
                sprintf('This list refreshes every <comment>%d</comment> second(s), type CTRL-C to exit', $refresh),
66
                '',
67
            ]);
68
69
            while (true) {
70
                $this->render($output, $actions, $empty);
71
                sleep($refresh);
72
            }
73
        } else {
74
            $this->render($output, $actions, $input->getOption('empty'));
75
        }
76
77
        return 0;
78
    }
79
80
    /**
81
     * @param OutputInterface $output
82
     * @param array           $actions
83
     * @param bool            $empty
84
     */
85
    protected function render(OutputInterface $output, array $actions, $empty = false)
86
    {
87
        $fields = [
88
            'action'   => 'name',
89
            'workers'  => 'current-watching',
90
            'reserved' => 'current-jobs-reserved',
91
            'ready'    => 'current-jobs-ready',
92
            'urgent'   => 'current-jobs-urgent',
93
            'delayed'  => 'current-jobs-delayed',
94
            'buried'   => 'current-jobs-buried',
95
        ];
96
97
        $table = new Table($output);
98
        $table->setHeaders(array_keys($fields));
99
100
        $rows = [];
101
        foreach ($actions as $action) {
102
            if (!$stats = $this->manager->getActionStats($action)) {
103
                if (!$empty) {
104
                    continue;
105
                }
106
107
                $stats = array_combine(array_values($fields), array_fill(0, sizeof($fields), '-'));
108
                $stats['name'] = $action;
109
            }
110
111
            $rows[$action] = array_map(
112
                function ($field) use ($stats) {
113
                    return $stats[$field];
114
                },
115
                $fields
116
            );
117
        }
118
119
        ksort($rows);
120
121
        $table->addRows($rows);
122
123
        if ($this->lineCount) {
124
            // move back to the beginning
125
            $output->write("\033[0G");
126
            $output->write(sprintf("\033[%dA", $this->lineCount));
127
128
            // overwrite the complete table before rendering the new one
129
            $width = $this->getApplication()->getTerminalDimensions()[0];
0 ignored issues
show
The method getTerminalDimensions() does not seem to exist on object<Symfony\Component\Console\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            $lines = array_fill(0, $this->lineCount, str_pad('', $width, ' '));
131
            $output->writeln($lines);
0 ignored issues
show
$lines is of type array, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
133
            $output->write(sprintf("\033[%dA", $this->lineCount));
134
        }
135
136
        // render the new table
137
        $table->render();
138
139
        // top table border + header + header border + bottom table border = 4
140
        $this->lineCount = 4 + sizeof($rows);
141
    }
142
}
143