Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

src/phpDocumentor/Command/Phar/UpdateCommand.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Command\Phar;
13
14
use Herrera\Json\Exception\FileException;
15
use Herrera\Phar\Update\Manager;
16
use Herrera\Phar\Update\Manifest;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Input\InputArgument;
22
23
/**
24
 * Updates phpDocumentor.phar to the latest version.
25
 *
26
 * ```
27
 * $ php phpDocumentor.phar phar:update [-m|--major] [-p|--pre] [version]
28
 * ```
29
 */
30
class UpdateCommand extends Command
31
{
32
    const MANIFEST_FILE = 'http://www.phpdoc.org/get/manifest.json';
33
34
    /**
35
     * Initializes this command and sets the name, description, options and arguments.
36
     *
37
     * @return void
38
     */
39
    protected function configure()
40
    {
41
        $this->setName('phar:update')
42
            ->setAliases(array('selfupdate', 'self-update'))
43
            ->setDescription('Updates phpDocumentor.phar to the indicated version or the latest if none is specified')
44
            ->addArgument(
45
                'version',
46
                InputArgument::OPTIONAL,
47
                'Updates to version-number (i.e. 2.6.0). When omitted phpDocumentor will update to the latest version'
48
            )
49
            ->addOption('major', 'm', InputOption::VALUE_NONE, 'Lock to current major version')
50
            ->addOption('pre', 'p', InputOption::VALUE_NONE, 'Allow pre-release version update');
51
    }
52
53
    /**
54
     * Executes the business logic involved with this command.
55
     *
56
     * @param \Symfony\Component\Console\Input\InputInterface $input
57
     * @param \Symfony\Component\Console\Output\OutputInterface $output
58
     *
59
     * @return int
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        $output->writeln('Looking for updates...');
64
65
        $manager         = $this->createManager($output);
66
        $version         = $input->getArgument('version')
67
            ? $input->getArgument('version')
68
            : $this->getApplication()->getVersion();
69
        $allowMajor      = $input->getOption('major');
70
        $allowPreRelease = $input->getOption('pre');
71
72
        if (strpos($version, 'v') === 0) {
73
            // strip v tag from phar version for self-update
74
            $version = str_replace('v', '', $version);
75
        }
76
77
        $this->updateCurrentVersion($manager, $version, $allowMajor, $allowPreRelease, $output);
0 ignored issues
show
It seems like $manager defined by $this->createManager($output) on line 65 can be null; however, phpDocumentor\Command\Ph...:updateCurrentVersion() 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...
78
79
        return 0;
80
    }
81
82
    /**
83
     * Returns manager instance or exit with status code 1 on failure.
84
     *
85
     * @param OutputInterface $output
86
     *
87
     * @return \Herrera\Phar\Update\Manager
88
     */
89
    private function createManager(OutputInterface $output)
90
    {
91
        try {
92
            return new Manager(Manifest::loadFile(self::MANIFEST_FILE));
93
        } catch (FileException $e) {
94
            $output->writeln('<error>Unable to search for updates.</error>');
95
96
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method createManager() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
97
        }
98
    }
99
100
    /**
101
     * Updates current version.
102
     *
103
     * @param Manager         $manager
104
     * @param string          $version
105
     * @param bool|null       $allowMajor
106
     * @param bool|null       $allowPreRelease
107
     * @param OutputInterface $output
108
     *
109
     * @return void
110
     */
111
    private function updateCurrentVersion(
112
        Manager $manager,
113
        $version,
114
        $allowMajor,
115
        $allowPreRelease,
116
        OutputInterface $output
117
    ) {
118
        if ($manager->update($version, $allowMajor, $allowPreRelease)) {
0 ignored issues
show
It seems like $allowMajor defined by parameter $allowMajor on line 114 can also be of type null; however, Herrera\Phar\Update\Manager::update() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
It seems like $allowPreRelease defined by parameter $allowPreRelease on line 115 can also be of type null; however, Herrera\Phar\Update\Manager::update() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
            $output->writeln('<info>Updated to latest version.</info>');
120
        } else {
121
            $output->writeln('<comment>Already up-to-date.</comment>');
122
        }
123
    }
124
}
125