Completed
Pull Request — master (#43)
by Greg
02:21
created

AlterOptionsCommandEvent::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Consolidation\AnnotatedCommand;
3
4
use Symfony\Component\Console\Command\Command;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Symfony\Component\Console\Application;
9
use Symfony\Component\Console\ConsoleEvents;
10
use Symfony\Component\Console\Event\ConsoleCommandEvent;
11
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12
13
use Consolidation\AnnotatedCommand\ExitCodeInterface;
14
use Consolidation\AnnotatedCommand\OutputDataInterface;
15
use Consolidation\AnnotatedCommand\AnnotationData;
16
use Consolidation\AnnotatedCommand\AnnotatedCommand;
17
18
/**
19
 * AlterOptionsCommandEvent is a subscriber to the Command Event
20
 * that looks up any additional options (e.g. from an OPTION_HOOK)
21
 * that should be added to the command.  Options need to be added
22
 * in two circumstances:
23
 *
24
 * 1. When 'help' for the command is called, so that the additional
25
 *    command options may be listed in the command description.
26
 *
27
 * 2. When the command itself is called, so that option validation
28
 *    may be done.
29
 *
30
 * We defer the addition of options until these times so that we
31
 * do not invoke the option hooks for every command on every run
32
 * of the program, and so that we do not need to defer the addition
33
 * of all of the application hooks until after all of the application
34
 * commands have been added. (Hooks may appear in the same command files
35
 * as command implementations; applications may support command file
36
 * plug-ins, and hooks may add options to commands defined in other
37
 * commandfiles.)
38
 */
39
class AlterOptionsCommandEvent implements EventSubscriberInterface
40
{
41
    /** var Application */
42
    protected $application;
43
44
    public function __construct(Application $application)
45
    {
46
        $this->application = $application;
47
    }
48
49
    /**
50
     * @param ConsoleCommandEvent $event
51
     */
52
    public function alterCommandOptions(ConsoleCommandEvent $event)
53
    {
54
        /* @var Command $command */
55
        $command = $event->getCommand();
56
        $names = [$command->getName()];
0 ignored issues
show
Unused Code introduced by
$names 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...
57
        if ($command->getName() == 'help') {
58
            $nameOfCommandToDescribe = $event->getInput()->getArgument('command_name');
59
            $commandToDescribe = $this->application->find($nameOfCommandToDescribe);
60
            $this->findAndAddHookOptions($commandToDescribe);
61
        }
62
        else {
63
            $this->findAndAddHookOptions($command);
64
        }
65
    }
66
67
    public function findAndAddHookOptions($command)
68
    {
69
        if (!$command instanceof AnnotatedCommand) {
70
            return;
71
        }
72
        $command->optionsHook();
73
    }
74
75
76
    /**
77
     * @{@inheritdoc}
78
     */
79
    public static function getSubscribedEvents()
80
    {
81
        return [ConsoleEvents::COMMAND => 'alterCommandOptions'];
82
    }
83
}
84