Completed
Push — develop ( 8fda15...dbbcf5 )
by Jaap
14s
created

src/phpDocumentor/Console/Input/ArgvInput.php (1 issue)

Severity

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
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Console\Input;
13
14
use Symfony\Component\Console\Input\ArgvInput as BaseArgvInput;
15
use Symfony\Component\Console\Input\InputDefinition;
16
17
/**
18
 * Argv input for the Console component of Symfony adapted to phpDocumentor.
19
 *
20
 * This InputStream for the Symfony Console component prepends the namespace and command
21
 * name `project:run` to the Argv array if no command has been provided.
22
 */
23
class ArgvInput extends BaseArgvInput
24
{
25
    /**
26
     * Constructor.
27
     *
28
     * The constructor has been overridden to check whether the first element in
29
     * the argument list is an argument (as it represents the command name).
30
     *
31
     * If it is not then we insert the command name: *project:run*.
32
     *
33
     * This way we can ensure that if no command name is given that the project
34
     * defaults to the execution of phpDocumentor. This is behavior that is
35
     * expected from previous releases of phpDocumentor.
36
     *
37
     * @param mixed[][] $argv An array of parameters from the CLI (in the argv format)
38
     * @api
39
     */
40 4
    public function __construct(array $argv = null, InputDefinition $definition = null)
0 ignored issues
show
__construct 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...
41
    {
42 4
        if (null === $argv) {
43 1
            $argv = $_SERVER['argv'];
44
        }
45
46 4
        if ((count($argv) === 1) || ($argv[1][0] === '-')) {
47 2
            array_splice($argv, 1, 0, 'project:run');
48
        }
49
50 4
        parent::__construct($argv, $definition);
51 4
    }
52
}
53