Completed
Push — master ( eb931e...d2a66f )
by Hannah
11:28
created

AliasCommand::execute()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 64
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 64
rs 6.8232
cc 8
eloc 38
nc 8
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 Geekish\Crap\Command;
4
5
use Geekish\Crap\CrapException;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ConfirmationQuestion;
11
use Symfony\Component\Console\Question\Question;
12
13
/**
14
 * Class AliasCommand
15
 * @package Geekish\Crap\Command
16
 */
17
final class AliasCommand extends BaseCommand
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 View Code Duplication
    protected function configure()
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...
23
    {
24
        $this->setName("alias");
25
        $this->setDescription("Defines an alias for a package to be used by crap.");
26
        $this->addArgument("alias", InputArgument::REQUIRED, "Package alias");
27
        $this->addArgument("package", InputArgument::REQUIRED, "Package");
28
        $this->addOption(
29
            "dry-run",
30
            null,
31
            InputOption::VALUE_NONE,
32
            "Run command without writing to your `crap.json`, useful for testing."
33
        );
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $alias = $input->getArgument("alias");
42
        $package = $input->getArgument("package");
43
44
        if (!$this->helper->validateAlias($alias)) {
45
            throw CrapException::create(
46
                "The alias `%s` is invalid, it should be lowercase, and match: [a-z0-9_.-]+",
47
                $alias
48
            );
49
        }
50
51
        if (!$this->helper->validatePackage($package)) {
52
            throw CrapException::create(
53
                "The package `%s` is invalid, it should match: [a-z0-9_.-]+/[a-z0-9_.-]+",
54
                $input->getArgument("package")
55
            );
56
        }
57
58
        $override = false;
59
60
        if ($this->helper->hasAlias($alias)) {
61
            $current = $this->helper->getAlias($alias);
62
63
            if ($current == $package) {
64
                $output->writeln(sprintf(
65
                    "<comment>Alias `%s` to package `%s` already exists, silly.</comment>",
66
                    $alias,
67
                    $package
68
                ));
69
                return 0;
70
            }
71
72
            $helper = $this->getHelper("question");
73
74
            $output->writeln(sprintf(
75
                "<comment>Alias `%s` exists and is set to `%s`.</comment>",
76
                $alias,
77
                $current
78
            ));
79
80
            $ask = sprintf("Override alias `%s` with `%s`? (y/n) ", $alias, $package);
81
            $question = new ConfirmationQuestion($ask, false);
82
83
            if (!$helper->ask($input, $output, $question)) {
84
                return 0;
85
            }
86
87
            $override = true;
88
        }
89
90
        if ($input->getOption("dry-run") !== true) {
91
            $this->helper->setAlias($alias, $package);
92
        }
93
94
        $output->writeln(sprintf(
95
            "<success>Alias `%s` to package `%s` successfully %s.</success>",
96
            $alias,
97
            $package,
98
            $override ? "updated" : "added"
99
        ));
100
101
        return 0;
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    protected function interact(InputInterface $input, OutputInterface $output)
108
    {
109
        $args = array_values($input->getArguments());
110
111
        $helper = $this->getHelper("question");
112
113
        if ($args[2] == null && $this->helper->validatePackage($args[1]) === true) {
114
            $package = $args[1];
115
116
            $output->writeln("<comment>You provided the package but no alias!</comment>");
117
118
            $message = sprintf("<question>What do you want to use an an alias for `%s`?</question>", $package);
119
            $question = new Question($message . PHP_EOL, false);
120
121
            $alias = $helper->ask($input, $output, $question);
122
123
            $input->setArgument("alias", $alias);
124
            $input->setArgument("package", $package);
125
        } elseif ($args[2] == null && $this->helper->validateAlias($args[1])) {
126
            $alias = $args[1];
127
128
            $output->writeln("<info>You provided the alias but no package!</info>");
129
130
            $message = sprintf("<question>What package do you want to alias `%s` to?</question>", $alias);
131
            $question = new Question($message . PHP_EOL, false);
132
133
            $package = $helper->ask($input, $output, $question);
134
135
            $input->setArgument("package", $package);
136
        } elseif ($this->helper->validateAlias($args[2]) && $this->helper->validatePackage($args[1])) {
137
            $output->writeln("<info>It looks like you swapped the package and alias.</info>");
138
139
            $message = sprintf(
140
                "<question>Did you mean to alias `%s` to package `%s`?</question> (y/n) ",
141
                $args[2],
142
                $args[1]
143
            );
144
145
            $question = new ConfirmationQuestion($message, false);
146
147
            if ($helper->ask($input, $output, $question)) {
148
                $input->setArgument("alias", $args[2]);
149
                $input->setArgument("package", $args[1]);
150
            }
151
        } elseif ($this->helper->validateAlias($args[1]) && $this->helper->hasAlias($args[2])) {
152
            $output->writeln("<info>You provided an existing alias instead of a package.</info>");
153
154
            $existing = $this->helper->getAlias($args[2]);
155
156
            $message = sprintf(
157
                "<question>Do you want to alias `%s` to `%s`?</question> (y/n) ",
158
                $args[1],
159
                $existing
160
            );
161
162
            $question = new ConfirmationQuestion($message, false);
163
164
            if ($helper->ask($input, $output, $question)) {
165
                $input->setArgument("package", $existing);
166
            }
167
        }
168
    }
169
}
170