PingCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
1
<?php
2
namespace Workana\AsyncJobs\Console;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputOption;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Workana\AsyncJobs\Util\Ping;
9
use Workana\AsyncJobs\AsyncAction;
10
use Workana\AsyncJobs\JobManager;
11
12
/**
13
 * Produces a ping
14
 *
15
 * @author Carlos Frutos <[email protected]>
16
 */
17
class PingCommand extends Command
18
{
19
    /**
20
     * @var JobManager
21
     */
22
    protected $jm;
23
24
    /**
25
     * Creates a new Command
26
     *
27
     * @param JobManager $jm
28
     */
29
    public function __construct(JobManager $jm)
30
    {
31
        parent::__construct('ping');
32
33
        $this->jm = $jm;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function configure()
40
    {
41
        $this->setDescription('Send a ping')
42
             ->addOption(
43
                'queues',
44
                null,
45
                InputOption::VALUE_REQUIRED,
46
                'Comma separated names of one or more queues that will be pinned.'
47
            );
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $queueNames = array_map('trim', explode(',', $input->getOption('queues')));
0 ignored issues
show
Unused Code introduced by
$queueNames 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...
56
        $queueNames = !empty($queues) ?: $this->jm->getDriver()->listQueues();
0 ignored issues
show
Bug introduced by
The variable $queues seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
57
58
        foreach ($queueNames as $queueName) {
0 ignored issues
show
Bug introduced by
The expression $queueNames of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
59
            $action = new AsyncAction(Ping::class, 'ping');
60
            $action->withPreferredQueueName($queueName);
61
            $this->jm->dispatch($action);
62
63
            $output->writeln('Ping sent to: ' . $queueName);
64
        }
65
    }
66
}