Completed
Push — master ( bea36f...b0555d )
by Gareth
11:40
created

TldrApplication::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace GarethEllis\Tldr\Console;
4
5
use GarethEllis\Tldr\Console\Command\TldrCommand;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\Console\Input\ArgvInput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class TldrApplication extends Application
12
{
13
    /**
14
     * Gets the name of the command based on input.
15
     *
16
     * @param InputInterface $input The input interface
17
     *
18
     * @return string The command name
19
     */
20
    protected function getCommandName(InputInterface $input)
21
    {
22
        // This should return the name of your command.
23
        return 'tldr';
24
    }
25
26
    /**
27
     * Gets the default commands that should always be available.
28
     *
29
     * @return array An array of default Command instances
30
     */
31
    protected function getDefaultCommands()
32
    {
33
        // Keep the core default commands to have the HelpCommand
34
        // which is used when using the --help option
35
        $defaultCommands = parent::getDefaultCommands();
36
        $defaultCommands[] = new TldrCommand();
37
        return $defaultCommands;
38
    }
39
40
    /**
41
     * Overridden so that the application doesn't expect the command
42
     * name to be the first argument.
43
     */
44
    public function getDefinition()
45
    {
46
        $inputDefinition = parent::getDefinition();
47
        // clear out the normal first argument, which is the command name
48
        $inputDefinition->setArguments();
49
50
        return $inputDefinition;
51
    }
52
53
    /**
54
     * Override parent method so that --help options is used when app is called with no arguments or options
55
     *
56
     * @param InputInterface|null $input
57
     * @param OutputInterface|null $output
58
     * @return int
59
     * @throws \Exception
60
     */
61
    public function run(InputInterface $input = null, OutputInterface $output = null)
0 ignored issues
show
Coding Style introduced by
run uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
62
    {
63
        if ($input === null) {
64
            if (count($_SERVER["argv"]) <= 1) {
65
                $args = array_merge($_SERVER["argv"], ["--help"]);
66
                $input = new ArgvInput($args);
67
            }
68
        }
69
        return parent::run($input, $output);
70
    }
71
}
72