Completed
Push — dev ( a8234e...9312cf )
by Zach
04:40
created

Argument   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 11.63 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 86
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 10 1
A setOptional() 0 10 2
A setDefault() 10 10 2
A setRequired() 0 8 2
A getAttributes() 0 9 1

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
2
3
namespace Yarak\Console\Input;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
7
class Argument extends Input
8
{
9
    /**
10
     * Default arguement value.
11
     *
12
     * @var null|string
13
     */
14
    protected $default = null;
15
16
    /**
17
     * Parse the set argument string.
18
     *
19
     * @return $this
20
     */
21
    public function parse()
22
    {
23
        return $this->setDescription()
24
            ->setArray('IS_ARRAY')
25
            ->setOptional()
26
            ->setDefault()
27
            ->setRequired()
28
            ->calculateMode(InputArgument::class)
29
            ->setName();
30
    }
31
32
    /**
33
     * Set optional argument mode.
34
     *
35
     * @return $this
36
     */
37
    protected function setOptional()
38
    {
39
        if (substr($this->input, -1) === '?') {
40
            $this->modeArray[] = 'OPTIONAL';
41
42
            $this->input = str_replace('?', '', $this->input);
43
        }
44
45
        return $this;
46
    }
47
48
    /**
49
     * Set default option value.
50
     *
51
     * @return $this
52
     */
53 View Code Duplication
    protected function setDefault()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
54
    {
55
        if (strpos($this->input, '=')) {
56
            list($this->input, $this->default) = explode('=', $this->input);
57
58
            $this->modeArray[] = 'OPTIONAL';
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set required mode if optional not already set.
66
     *
67
     * @return $this
68
     */
69
    protected function setRequired()
70
    {
71
        if (!in_array('OPTIONAL', $this->modeArray)) {
72
            $this->modeArray[] = 'REQUIRED';
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * Get the argument attributes.
80
     *
81
     * @return array
82
     */
83
    public function getAttributes()
84
    {
85
        return [
86
            'name'        => $this->name,
87
            'mode'        => $this->mode,
88
            'description' => $this->description,
89
            'default'     => $this->default,
90
        ];
91
    }
92
}
93