Argv   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 80
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 6
loc 80
c 0
b 0
f 0
wmc 20
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 14 5
A has() 3 10 4
A remove() 3 10 4
A getArguments() 0 12 3
A getOptions() 0 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace nyx\console\input\formats\tokens;
2
3
// External dependencies
4
use nyx\core;
5
6
// Internal dependencies
7
use nyx\console\input;
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 input\formats\interfaces\Tokens
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function get($parameter, $default = null)
27
    {
28
        foreach ($this->items as $token) {
29
            if ($token === $parameter || 0 === strpos($token, $parameter . '=')) {
30
                if (false !== $pos = strpos($parameter, '=')) {
31
                    return substr($token, $pos + 1);
32
                }
33
34
                return $token;
35
            }
36
        }
37
38
        return $default;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function has($parameter) : bool
45
    {
46
        foreach ($this->items as $token) {
47 View Code Duplication
            if ($token === $parameter || 0 === strpos($token, $parameter . '=')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function remove($parameter) : core\collections\interfaces\Map
59
    {
60
        foreach ($this->items as $key => $token) {
61 View Code Duplication
            if ($token === $parameter || 0 === strpos($token, $parameter . '=')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
                unset($this->items[$key]);
63
            }
64
        }
65
66
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (nyx\console\input\formats\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...
67
    }
68
69
    /**
70
     * {@inheritDoc}
71
     */
72
    public function getArguments() : array
73
    {
74
        $arguments = [];
75
76
        foreach ($this->items as $token) {
77
            if ($token[0] !== '-') {
78
                $arguments[] = $token;
79
            }
80
        }
81
82
        return $arguments;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function getOptions() : array
89
    {
90
        $options = [];
91
92
        foreach ($this->items as $token) {
93
            if ($token !== '--' && $token[0] === '-') {
94
                $options[] = $token;
95
            }
96
        }
97
98
        return $options;
99
    }
100
}
101