InitCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getStubPath() 0 3 1
A __construct() 0 7 1
A executeWizard() 0 11 1
A askGithubUsername() 0 6 1
A defaultUsername() 0 4 1
A askGithubToken() 0 9 2
A askGithubPassword() 0 7 1
A init() 0 13 2
1
<?php
2
3
namespace Acacha\Llum\Console;
4
5
use Acacha\Llum\Compiler\RCFileCompiler;
6
use Acacha\Llum\Filesystem\Filesystem;
7
use Acacha\Llum\Github\GithubAPI;
8
use Acacha\Llum\LlumRCFile;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
use Symfony\Component\Console\Question\Question;
13
14
/**
15
 * Class InitCommand
16
 * @package Acacha\Llum\Console
17
 */
18
class InitCommand extends LlumCommand
19
{
20
21
    /**
22
     * Github api service class.
23
     *
24
     * @var GithubAPI
25
     */
26
    protected $api;
27
28
    /**
29
     * Filesystem.
30
     *
31
     * @var Filesystem
32
     */
33
    protected $filesystem;
34
35
    /**
36
     * Compiler for llumrc file.
37
     *
38
     * @var RCFileCompiler
39
     */
40
    protected $compiler;
41
42
    /**
43
     * Command name.
44
     *
45
     * @var string
46
     */
47
    protected $commandName = 'init';
48
49
    /**
50
     * Command description.
51
     *
52
     * @var string
53
     */
54
    protected $commandDescription = 'Create ~/.llumrc file with llum configuration data';
55
56
    /**
57
     * Method to execute.
58
     *
59
     * @var string
60
     */
61
    protected $method = 'init';
62
63
    /**
64
     * Github username
65
     *
66
     * @var
67
     */
68
    protected $github_username;
69
70
    /**
71
     * Github token.
72
     *
73
     * @var string
74
     */
75
    protected $github_token = "";
76
77
    /**
78
     * Get path to stub.
79
     *
80
     * @return string
81
     */
82
    protected function getStubPath() {
83
        return __DIR__ . '/stubs/llumrc.stub';
84
    }
85
86
87
    /**
88
     * InitCommand constructor.
89
     *
90
     * @param Filesystem $filesystem
91
     * @param RCFileCompiler $compiler
92
     * @param GithubAPI $api
93
     */
94
    public function __construct(Filesystem $filesystem, RCFileCompiler $compiler, GithubAPI $api)
95
    {
96
        parent::__construct();
97
        $this->filesystem = $filesystem;
98
        $this->compiler = $compiler;
99
        $this->api = $api;
100
    }
101
102
    /**
103
     * init command.
104
     */
105
    public function init(InputInterface $input, OutputInterface $output)
106
    {
107
        try {
108
            $this->executeWizard($input,$output);
109
            $this->filesystem->overwrite(
110
                (new LlumRCFile())->path(),
111
                $this->compiler->compile(
112
                    $this->filesystem->get($this->getStubPath()),
113
                    $this->data));
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
114
        } catch (\Exception $e) {
115
            print_r($e->xdebug_message);
0 ignored issues
show
Bug introduced by
The property xdebug_message does not seem to exist. Did you mean message?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
116
        }
117
    }
118
119
    /**
120
     * Executes wizard.
121
     *
122
     * @param InputInterface $input
123
     * @param OutputInterface $output
124
     */
125
    protected function executeWizard(InputInterface $input, OutputInterface $output){
126
127
        $this->askGithubUsername($input,$output);
128
        $this->askGithubToken($input,$output);
129
130
        $this->data = [
131
            "GITHUB_USERNAME" => $this->github_username,
132
            "GITHUB_TOKEN" => $this->github_token,
133
            "GITHUB_TOKEN_NAME" => $this->api->tokenName(),
134
        ];
135
    }
136
137
    /**
138
     * @param InputInterface $input
139
     * @param OutputInterface $output
140
     */
141
    public function askGithubUsername(InputInterface $input, OutputInterface $output)
142
    {
143
        $defaultusername = $this->defaultUsername();
144
        $question = new Question('<info>Please enter your github username (' . $defaultusername . ') ? </info>', $defaultusername);
145
        $this->github_username = $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    protected function defaultUsername()
152
    {
153
        return get_current_user();
154
    }
155
156
    /**
157
     * @param InputInterface $input
158
     * @param OutputInterface $output
159
     */
160
    public function askGithubToken(InputInterface $input, OutputInterface $output)
161
    {
162
        $question = new ConfirmationQuestion('<info>Do you want to use our assistant to obtain token via Github API (Y/n)? </info>', true);
163
        if ($this->getHelper('question')->ask($input, $output, $question)) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
164
            $this->github_token =$this->api->getPersonalToken(
165
                $this->github_username,
166
                $this->askGithubPassword($input,$output));
167
        }
168
    }
169
170
    /**
171
     * @param InputInterface $input
172
     * @param OutputInterface $output
173
     * @return mixed
174
     */
175
    protected function askGithubPassword(InputInterface $input, OutputInterface $output) {
176
        $question = new Question('<info>Github password? </info>');
177
        $question->setHidden(true);
178
        $question->setHiddenFallback(false);
179
180
        return $this->getHelper('question')->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
181
    }
182
}