GithubRepoCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 145
Duplicated Lines 5.52 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 7
dl 8
loc 145
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A githubCreateRepo() 0 15 3
A showError() 0 9 2
A getCredentials() 8 8 2
A repoName() 0 4 2
A repoDescription() 0 4 2
A configure() 0 10 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 Acacha\Llum\Console;
4
5
use Acacha\Llum\Github\GithubAPI;
6
use Acacha\Llum\Parser\LlumRCParser;
7
use GuzzleHttp\Exception\ClientException;
8
use GuzzleHttp\Exception\ServerException;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class GithubRepoCommand.
15
 *
16
 * @package Acacha\Llum\Console
17
 */
18
class GithubRepoCommand extends LlumCommand
19
{
20
21
    use GithubCommand;
22
23
    /**
24
     * Github api service class.
25
     *
26
     * @var GithubAPI
27
     */
28
    protected $api;
29
30
    /**
31
     * Llum rc file parser.
32
     *
33
     * @var LlumRCParser
34
     */
35
    protected $parser;
36
37
    /**
38
     * Command name.
39
     *
40
     * @var string
41
     */
42
    protected $commandName = 'github:repo';
43
44
    /**
45
     * Command description.
46
     *
47
     * @var string
48
     */
49
    protected $commandDescription = 'Creates a github repo';
50
51
    /**
52
     * Method to execute.
53
     *
54
     * @var string
55
     */
56
    protected $method = 'githubCreateRepo';
57
58
    /**
59
     * GithubRepoCommand constructor.
60
     * @param GithubAPI $api
61
     * @param LlumRCParser $parser
62
     */
63
    public function __construct(GithubAPI $api, LlumRCParser $parser)
64
    {
65
        parent::__construct();
66
        $this->api = $api;
67
        $this->parser = $parser;
68
    }
69
70
71
    /**
72
     * Github create repo command.
73
     *
74
     * @param InputInterface $input
75
     * @param OutputInterface $output
76
     */
77
    public function githubCreateRepo(InputInterface $input, OutputInterface $output)
78
    {
79
        $this->api->setCredentials($this->getCredentials($output));
0 ignored issues
show
Bug introduced by
It seems like $this->getCredentials($output) targeting Acacha\Llum\Console\Gith...mmand::getCredentials() can also be of type null; however, Acacha\Llum\Github\GithubAPI::setCredentials() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
        $name = $this->repoName($input);
81
        try {
82
            $this->api->createRepo($name,$this->repoDescription($input));
83
        } catch (ServerException $se) {
84
            //TODO
85
            $output->writeln('<error>Server exception thrown</error>');
86
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method githubCreateRepo() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
87
        } catch (ClientException $ce) {
88
            $this->showError( $ce , $output );
89
        }
90
        $output->writeln('<info>Repository ' . $name . ' created</info>');
91
    }
92
93
94
95
    /**
96
     * Show error.
97
     * @param ClientException $ce
98
     * @param OutputInterface $output
99
     */
100
    protected function showError(ClientException $ce, OutputInterface $output)
101
    {
102
        if ($ce->getResponse()->getStatusCode() == 422) {
103
            $output->writeln('<error>Repository already exists</error>');
104
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method showError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
105
        }
106
        $output->writeln('<error>Client Request exception thrown</error>');
107
        die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method showError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
108
    }
109
110
111
    /**
112
     * Get credentials.
113
     *
114
     * @param OutputInterface $input
0 ignored issues
show
Bug introduced by
There is no parameter named $input. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     * @return array
116
     */
117 View Code Duplication
    public function getCredentials(OutputInterface $output)
0 ignored issues
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...
118
    {
119
        $credentials = $this->parser->getCredentials();
120
        if ( is_null($credentials)) {
121
            $this->showErrorRunLlumInitFirst($output,"Credentials");
122
        };
123
        return $credentials;
124
    }
125
126
    /**
127
     * Obtain repo name.
128
     *
129
     * @param InputInterface $input
130
     * @return mixed|string
131
     */
132
    protected function repoName(InputInterface $input) {
133
        $name = $input->getArgument('name');
134
        return isset($name) ? $name : basename(getcwd());
135
    }
136
137
    /**
138
     * Obtain repo description.
139
     *
140
     * @param InputInterface $input
141
     * @return mixed|string
142
     */
143
    protected function repoDescription(InputInterface $input) {
144
        $description = $input->getArgument('description');
145
        return isset($description) ? $description : "";
146
    }
147
148
    /**
149
     * Configure command.
150
     */
151
    public function configure()
152
    {
153
        parent::configure();
154
155
        $this
156
            // configure an argument
157
            ->addArgument('name', InputArgument::OPTIONAL, 'Repository name')
158
            ->addArgument('description', InputArgument::OPTIONAL, 'Repository description')
159
        ;
160
    }
161
162
}