Completed
Push — symfony-console ( c96862...f4655c )
by Arnaud
02:14
created

CommandSelfUpdate::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Command;
10
11
use Humbug\SelfUpdate\Updater;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputDefinition;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class CommandSelfUpdate extends Command
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $version;
24
    /**
25
     * @var Updater
26
     */
27
    protected $updater;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('self-update')
36
            ->setAliases(['selfupdate'])
37
            ->setDescription('Update Cecil to the latest version')
38
            ->setDefinition(
39
                new InputDefinition([
40
                    new InputArgument('path', InputArgument::OPTIONAL, 'If specified, use the given path as working directory'),
41
                ])
42
            )
43
            ->setHelp('The self-update command checks for a newer version, and, if found, downloads and installs the latest.');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        $this->version = $this->getApplication()->getVersion();
52
53
        $this->updater = new Updater(null, false, Updater::STRATEGY_GITHUB);
54
        /* @var $strategy \Humbug\SelfUpdate\Strategy\GithubStrategy */
55
        $strategy = $this->updater->getStrategy();
56
        $strategy->setPackageName('cecil/cecil');
57
        $strategy->setPharName('cecil.phar');
58
        $strategy->setCurrentLocalVersion($this->version);
59
        $strategy->setStability('any');
60
61
        try {
62
            $output->writeln('Checks for updates...');
63
            $result = $this->updater->update();
64 View Code Duplication
            if ($result) {
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...
65
                $new = $this->updater->getNewVersion();
66
                $old = $this->updater->getOldVersion();
67
                $output->writeln(sprintf('Updated from %s to %s.', $old, $new));
68
69
                return 0;
70
            }
71
            $output->writeln(sprintf('You are already using last version (%s).', $this->version));
72
73
            return 0;
74
        } catch (\Exception $e) {
75
            echo $e->getMessage();
76
77
            return 1;
78
        }
79
80
        return 0;
0 ignored issues
show
Unused Code introduced by
return 0; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
81
    }
82
}
83