Options::unpack()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php namespace nyx\console\input\parameter\definitions;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\console\input;
8
9
/**
10
 * Input Option Definitions
11
 *
12
 * @version     0.1.0
13
 * @author      Michal Chojnacki <[email protected]>
14
 * @copyright   2012-2017 Nyx Dev Team
15
 * @link        https://github.com/unyx/nyx
16
 */
17
class Options extends input\parameter\Definitions
18
{
19
    /**
20
     * @var input\Option[]  A map of Option names to their Definitions.
21
     */
22
    protected $items = [];
23
24
    /**
25
     * @var input\Option[]  A map of Option shortcuts to their Definitions.
26
     */
27
    protected $shortcuts = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function __construct($options = null)
33
    {
34
        $this->setCollectedType(input\Option::class);
35
36
        parent::__construct($options);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function replace($items) : core\collections\interfaces\Collection
43
    {
44
        $this->items     = [];
45
        $this->shortcuts = [];
46
47
        foreach ($this->extractItems($items) as $item) {
48
            $this->add(is_array($item) ? $this->unpack($item) : $item);
49
        }
50
51
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (nyx\console\input\parameter\definitions\Options) is incompatible with the return type of the parent method nyx\core\collections\NamedObjectSet::replace of type nyx\core\collections\traits\NamedObjectSet.

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...
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function add(core\interfaces\Named $option) : core\collections\interfaces\NamedObjectSet
58
    {
59
        parent::add($option);
60
61
        // If the Option has a shortcut...
62
        /* @var input\Option $option */
63
        if ($shortcut = $option->getShortcut()) {
64
            if (isset($this->shortcuts[$shortcut])) {
65
                throw new core\collections\exceptions\KeyAlreadyExists($this, $shortcut, $option, "An option with this shortcut [$shortcut] has already been defined.");
66
            }
67
68
            $this->shortcuts[$shortcut] = $option;
69
        }
70
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (nyx\console\input\parameter\definitions\Options) is incompatible with the return type of the parent method nyx\core\collections\NamedObjectSet::add of type nyx\core\collections\traits\NamedObjectSet.

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...
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function remove(string $name): core\collections\interfaces\NamedObjectSet
78
    {
79
        if (!isset($this->items[$name])) {
80
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (nyx\console\input\parameter\definitions\Options) is incompatible with the return type of the parent method nyx\core\collections\NamedObjectSet::remove of type nyx\core\collections\traits\NamedObjectSet.

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...
81
        }
82
83
        if ($shortcut = $this->items[$name]->getShortcut()) {
84
            unset($this->shortcuts[$shortcut]);
85
        }
86
87
        return parent::remove($name);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return parent::remove($name); (nyx\console\input\parameter\definitions\Options) is incompatible with the return type of the parent method nyx\core\collections\NamedObjectSet::remove of type nyx\core\collections\traits\NamedObjectSet.

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...
88
    }
89
90
    /**
91
     * Returns the map of Option shortcuts to their Definitions.
92
     *
93
     * @return  input\Option[]
94
     */
95
    public function getShortcuts() : array
96
    {
97
        return $this->shortcuts;
98
    }
99
100
    /**
101
     * Returns the Option matching a given shortcut name.
102
     *
103
     * @param   string          $shortcut                   The name of the shortcut.
104
     * @return  input\Option                                The Option matching the shortcut name.
105
     * @throws  core\collections\exceptions\KeyNotExists    When the given shortcut is not defined.
106
     */
107
    public function getByShortcut(string $shortcut) : input\Option
108
    {
109
        if (!isset($this->shortcuts[$shortcut])) {
110
            throw new core\collections\exceptions\KeyNotExists($this, $shortcut, "The short option [$shortcut] is not defined.");
111
        }
112
113
        return $this->shortcuts[$shortcut];
114
    }
115
116
    /**
117
     * Unpacks a sequence of Option constructor arguments into an Option instance.
118
     *
119
     * @see     \nyx\console\input\Option::__construct()
120
     *
121
     * @param   array           $definition     The arguments to unpack. The order must match the constructor's signature.
122
     * @return  input\Option
123
     */
124
    protected function unpack(array $definition) : input\Option
125
    {
126
        // If the 4th argument is an integer, we are going to assume it's one of the input\Value
127
        // class constants defining the mode and attempt to instantiate a input\Value with such.
128
        if (isset($definition[3]) && is_int($definition[3])) {
129
            $definition[3] = new input\Value($definition[3]);
130
        }
131
132
        return new input\Option(...$definition);
0 ignored issues
show
Documentation introduced by
$definition is of type array<integer,?,{"3":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
    }
134
}
135