UserEditCommand::execute()   B
last analyzed

Complexity

Conditions 9
Paths 14

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.4698
c 0
b 0
f 0
cc 9
nc 14
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Question\ChoiceQuestion;
8
use Symfony\Component\Console\Question\ConfirmationQuestion;
9
10
class UserEditCommand extends AbstractUserCommand
11
{
12
    const FIELD_PASSWORD = 'Password';
13
    const FIELD_REAL_NAME = 'Real Name';
14
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('gitki:user:edit')
19
            ->setDescription('Edits an existing user');
20
    }
21
22
    protected function execute(InputInterface $input, OutputInterface $output)
23
    {
24
        $userManager = $this->getUserManager();
25
        $questionHelper = $this->getQuestionHelper();
26
27
        $user = $this->selectUser($input, $output);
28
29
        $fields = [
30
            self::FIELD_REAL_NAME,
31
            'Email',
32
            'Role',
33
            self::FIELD_PASSWORD,
34
            'Github Login',
35
            'Google Login',
36
            'Done'
37
        ];
38
39
        $fieldQuestion = new ChoiceQuestion(
40
            'Select Field: ',
41
            $fields
42
        );
43
        do {
44
            $this->printUser($user, $output);
45
            $field = $questionHelper->ask($input, $output, $fieldQuestion);
46
            switch ($field) {
47
                case self::FIELD_REAL_NAME:
48
                    $user = $this->editRealName($input, $output, $user, $questionHelper);
49
                    break;
50
                case 'Email':
51
                    $user = $this->editEmail($input, $output, $user, $questionHelper);
52
                    break;
53
                case 'Role':
54
                    $user = $this->editRole($input, $output, $user, $questionHelper);
55
                    break;
56
                case self::FIELD_PASSWORD:
57
                    $user = $this->editPassword($input, $output, $user, $questionHelper, $userManager);
0 ignored issues
show
Bug introduced by
It seems like $userManager defined by $this->getUserManager() on line 24 can be null; however, App\Command\AbstractUserCommand::editPassword() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
58
                    break;
59
                case 'Github Login':
60
                    $user = $this->editGithubId($input, $output, $user, $questionHelper);
61
                    break;
62
                case 'Google Login':
63
                    $user = $this->editGoogleId($input, $output, $user, $questionHelper);
64
                    break;
65
            }
66
        } while ('Done' !== $field);
67
68
        $this->printUser($user, $output);
69
70
        $saveQuestion = new ConfirmationQuestion('Save? ', false);
71
        if ($questionHelper->ask($input, $output, $saveQuestion)) {
72
            $userManager->updateUser($user);
73
        }
74
    }
75
}
76