Completed
Push — master ( 05902f...9e6322 )
by mark
01:41 queued 11s
created

Breakpoint::execute()   C

Complexity

Conditions 15
Paths 28

Size

Total Lines 47

Duplication

Lines 6
Ratio 12.77 %

Code Coverage

Tests 2
CRAP Score 176.1442

Importance

Changes 0
Metric Value
dl 6
loc 47
ccs 2
cts 19
cp 0.1053
rs 5.9166
c 0
b 0
f 0
cc 15
nc 28
nop 2
crap 176.1442

How to fix   Complexity   

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
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace Phinx\Console\Command;
9
10
use InvalidArgumentException;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class Breakpoint extends AbstractCommand
16
{
17
    /**
18
     * @var string
19
     */
20
    protected static $defaultName = 'breakpoint';
21
22
    /**
23
     * {@inheritDoc}
24
     *
25
     * @return void
26
     */
27 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...
28
    {
29
        parent::configure();
30
31
        $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment.');
32
33
        $this->setDescription('Manage breakpoints')
34
            ->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to target for the breakpoint')
35
            ->addOption('--set', '-s', InputOption::VALUE_NONE, 'Set the breakpoint')
36
            ->addOption('--unset', '-u', InputOption::VALUE_NONE, 'Unset the breakpoint')
37
            ->addOption('--remove-all', '-r', InputOption::VALUE_NONE, 'Remove all breakpoints')
38
            ->setHelp(
39
                <<<EOT
40
The <info>breakpoint</info> command allows you to toggle, set, or unset a breakpoint against a specific target to inhibit rollbacks beyond a certain target.
41 35
If no target is supplied then the most recent migration will be used.
42
You cannot specify un-migrated targets
43 35
44
<info>phinx breakpoint -e development</info>
45 35
<info>phinx breakpoint -e development -t 20110103081132</info>
46
<info>phinx breakpoint -e development -r</info>
47 35
EOT
48 35
            );
49 35
    }
50 35
51 35
    /**
52
     * Toggle the breakpoint.
53
     *
54
     * @param \Symfony\Component\Console\Input\InputInterface $input Input
55
     * @param \Symfony\Component\Console\Output\OutputInterface $output Output
56
     *
57
     * @throws \InvalidArgumentException
58
     *
59
     * @return int integer 0 on success, or an error code.
60
     */
61 35
    protected function execute(InputInterface $input, OutputInterface $output)
62 35
    {
63
        $this->bootstrap($input, $output);
64
65
        $environment = $input->getOption('environment');
66
        $version = (int)$input->getOption('target') ?: null;
67
        $removeAll = $input->getOption('remove-all');
68
        $set = $input->getOption('set');
69
        $unset = $input->getOption('unset');
70
71 View Code Duplication
        if ($environment === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
            $environment = $this->getConfig()->getDefaultEnvironment();
73
            $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
74
        } else {
75
            $output->writeln('<info>using environment</info> ' . $environment);
76
        }
77
78
        if (!$this->getConfig()->hasEnvironment($environment)) {
79
            $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
80
81
            return self::CODE_ERROR;
82
        }
83
84
        if ($version && $removeAll) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
85
            throw new InvalidArgumentException('Cannot toggle a breakpoint and remove all breakpoints at the same time.');
86
        }
87
88
        if (($set && $unset) || ($set && $removeAll) || ($unset && $removeAll)) {
89
            throw new InvalidArgumentException('Cannot use more than one of --set, --unset, or --remove-all at the same time.');
90
        }
91
92
        if ($removeAll) {
93
            // Remove all breakpoints.
94
            $this->getManager()->removeBreakpoints($environment);
95
        } elseif ($set) {
96
            // Set the breakpoint.
97
            $this->getManager()->setBreakpoint($environment, $version);
98
        } elseif ($unset) {
99
            // Unset the breakpoint.
100
            $this->getManager()->unsetBreakpoint($environment, $version);
101
        } else {
102
            // Toggle the breakpoint.
103
            $this->getManager()->toggleBreakpoint($environment, $version);
104
        }
105
106
        return self::CODE_SUCCESS;
107
    }
108
}
109