Passed
Push — master ( 1d8e46...0e8320 )
by Théo
02:15
created

src/Command/BaseCommand.php (3 issues)

1
<?php
2
/**
3
 * This code is licensed under the BSD 3-Clause License.
4
 *
5
 * Copyright (c) 2017, Maks Rafalko
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * * Redistributions of source code must retain the above copyright notice, this
12
 *   list of conditions and the following disclaimer.
13
 *
14
 * * Redistributions in binary form must reproduce the above copyright notice,
15
 *   this list of conditions and the following disclaimer in the documentation
16
 *   and/or other materials provided with the distribution.
17
 *
18
 * * Neither the name of the copyright holder nor the names of its
19
 *   contributors may be used to endorse or promote products derived from
20
 *   this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 */
33
34
declare(strict_types=1);
35
36
namespace Infection\Command;
37
38
use Infection\Console\Application;
0 ignored issues
show
The type Infection\Console\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
use Infection\Console\IO;
40
use Symfony\Component\Console\Command\Command;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Output\OutputInterface;
43
use Webmozart\Assert\Assert;
44
45
/**
46
 * @internal
47
 */
48
abstract class BaseCommand extends Command
49
{
50
    /**
51
     * @var IO|null
52
     */
53
    private $io;
54
55
    final public function getApplication(): Application
56
    {
57
        $application = parent::getApplication();
58
59
        Assert::isInstanceOf(
60
            $application,
61
            Application::class,
62
            'Cannot access to the command application if the command has not been '
63
            . 'registered to the application yet'
64
        );
65
66
        return $application;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $application returns the type Symfony\Component\Console\Application|null which is incompatible with the type-hinted return Infection\Console\Application.
Loading history...
67
    }
68
69
    protected function initialize(InputInterface $input, OutputInterface $output): void
70
    {
71
        parent::initialize($input, $output);
72
73
        $this->io = new IO($input, $output);
74
    }
75
76
    protected function execute(InputInterface $input, OutputInterface $output): int
77
    {
78
        $this->executeCommand($this->getIO());
79
80
        return 0;
81
    }
82
83
    abstract protected function executeCommand(IO $io): void;
84
85
    final protected function getIO(): IO
86
    {
87
        Assert::notNull(
88
            $this->io,
89
            'Cannot retrieve the IO object before the command was initialized'
90
        );
91
92
        return $this->io;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->io could return the type null which is incompatible with the type-hinted return Infection\Console\IO. Consider adding an additional type-check to rule them out.
Loading history...
93
    }
94
}
95