Completed
Push — master ( 5733a1...525834 )
by Michał
02:54
created

Options   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 81
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A replace() 0 18 3
A add() 0 12 2
A getShortcuts() 0 4 1
A getByShortcut() 0 8 2
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 array   A map of Option shortcuts to their actual names.
21
     */
22
    private $shortcuts = [];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function __construct($options = null)
28
    {
29
        $this->setCollectedType(input\Option::class);
30
31
        parent::__construct($options);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function replace($items) : core\collections\interfaces\Collection
38
    {
39
        $this->items     = [];
40
        $this->shortcuts = [];
41
42
        foreach ($this->extractItems($items) as $item) {
43
44
            // Allow for defining Option instances without explicitly instantiating them
45
            // when creating Input Definitions.
46
            if (is_array($item)) {
47
                $item = new input\Option(...$item);
0 ignored issues
show
Documentation introduced by
$item is of type array, 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...
48
            }
49
50
            $this->add($item);
51
        }
52
53
        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...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function add(core\interfaces\Named $option) : core\collections\interfaces\NamedObjectSet
60
    {
61
        parent::add($option);
62
63
        // If the Option has a shortcut...
64
        /* @var input\Option $option */
65
        if ($shortcut = $option->getShortcut()) {
66
            $this->shortcuts[$shortcut] = $option->getName();
67
        }
68
69
        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...
70
    }
71
72
    /**
73
     * Returns the map of Option shortcuts to their actual names.
74
     *
75
     * @return  array
76
     */
77
    public function getShortcuts() : array
78
    {
79
        return $this->shortcuts;
80
    }
81
82
    /**
83
     * Returns the Option matching a given shortcut name.
84
     *
85
     * @param   string          $shortcut   The name of the shortcut.
86
     * @return  input\Option
87
     * @throws  \OutOfBoundsException       When the given shortcut is not defined.
88
     */
89
    public function getByShortcut(string $shortcut) : input\Option
90
    {
91
        if (!isset($this->shortcuts[$shortcut])) {
92
            throw new \OutOfBoundsException("The short option [$shortcut] is not defined.");
93
        }
94
95
        return $this->get($this->shortcuts[$shortcut]);
96
    }
97
}
98