Completed
Push — master ( 465639...b2ebb1 )
by Michał
03:09
created

Argv::getOptions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 0
1
<?php namespace nyx\console\input\sources\tokens;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\console\interfaces;
8
9
/**
10
 * Argv Input Tokens
11
 *
12
 * This Tokens collection is able to resolve Input Options by name, as the name is contained in the argv data,
13
 * but will not be able to do the same for Input Arguments, as their names are unknown until the input gets
14
 * bound to a Definition.
15
 *
16
 * @version     0.1.0
17
 * @author      Michal Chojnacki <[email protected]>
18
 * @copyright   2012-2017 Nyx Dev Team
19
 * @link        https://github.com/unyx/nyx
20
 */
21
class Argv extends core\collections\Map implements interfaces\input\Tokens
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function get($parameter, $default = null)
27
    {
28
        foreach ($this->items as $token) {
29
30
            if ($token === $parameter || 0 === strpos($token, $parameter . '=')) {
31
                if (false !== $pos = strpos($parameter, '=')) {
32
                    return substr($token, $pos + 1);
33
                }
34
35
                return $token;
36
            }
37
        }
38
39
        return $default;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function has($parameter) : bool
46
    {
47
        return in_array($parameter, $this->items);
48
    }
49
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function remove($parameter) : core\collections\interfaces\Map
55
    {
56
        if (false !== $key = array_search($parameter, $this->items)) {
57
            unset($this->items[$key]);
58
        }
59
60
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (nyx\console\input\sources\tokens\Argv) is incompatible with the return type of the parent method nyx\core\collections\Map::remove of type nyx\core\collections\traits\Map.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
    }
62
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function getArguments() : array
68
    {
69
        $arguments = [];
70
71
        foreach ($this->items as $token) {
72
            if ($token[0] !== '-') {
73
                $arguments[] = $token;
74
            }
75
        }
76
77
        return $arguments;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function getOptions() : array
84
    {
85
        $options = [];
86
87
        foreach ($this->items as $token) {
88
            if ($token[0] === '-' && $token !== '--') {
89
                $options[] = $token;
90
            }
91
        }
92
93
        return $options;
94
    }
95
}
96