ArgvArgs::getScriptName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Args;
13
14
use Webmozart\Console\Api\Args\RawArgs;
15
16
/**
17
 * Console arguments passed via PHP's "argv" variable.
18
 *
19
 * @since  1.0
20
 *
21
 * @author Bernhard Schussek <[email protected]>
22
 */
23
class ArgvArgs implements RawArgs
24
{
25
    /**
26
     * @var string
27
     */
28
    private $scriptName;
29
30
    /**
31
     * @var string[]
32
     */
33
    private $tokens;
34
35
    /**
36
     * Creates the console arguments.
37
     *
38
     * @param array $argv The contents of the "argv" variable or `null` to read
0 ignored issues
show
Documentation introduced by
Should the type for parameter $argv not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     *                    the global "argv" variable.
40
     */
41 5
    public function __construct(array $argv = null)
0 ignored issues
show
Coding Style introduced by
__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...
42
    {
43 5
        if (null === $argv) {
44 1
            $argv = $_SERVER['argv'];
45
        }
46
47 5
        $this->scriptName = array_shift($argv);
48 5
        $this->tokens = $argv;
49 5
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getScriptName()
55
    {
56 3
        return $this->scriptName;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    public function hasToken($token)
63
    {
64 1
        return in_array($token, $this->tokens);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function getTokens()
71
    {
72 3
        return $this->tokens;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function toString($scriptName = true)
79
    {
80 1
        $string = implode(' ', $this->tokens);
81
82 1
        if ($scriptName) {
83 1
            $string = ltrim($this->scriptName.' ').$string;
84
        }
85
86 1
        return $string;
87
    }
88
}
89