Application   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 7
dl 0
loc 170
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A exitCode() 0 4 2
A add() 0 10 2
A throw() 0 12 2
A tryReadManifest() 0 8 4
A readManifest() 0 12 3
A bootHighlight() 0 9 1
A addLanguage() 0 6 1
A gracefulRun() 0 14 2
A bootPipes() 0 7 1
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Console;
11
12
use Railt\Console\Language\GraphQLLanguage;
13
use Railt\Console\Language\LanguageInterface;
14
use Railt\Console\Language\PHPLanguage;
15
use Symfony\Component\Console\Application as Symfony;
16
use Symfony\Component\Console\Command\Command;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Railt\Console\Command.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Symfony\Component\Console\Input\ArgvInput;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\ConsoleOutput;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Class Application
24
 */
25
class Application extends Symfony
26
{
27
    /**
28
     * @var string
29
     */
30
    public const APP_NAME = 'Railt';
31
32
    /**
33
     * @var string
34
     */
35
    public const APP_VERSION = '1.2';
36
37
    /**
38
     * @var ExceptionRenderer
39
     */
40
    private $renderer;
41
42
    /**
43
     * @var Highlighter
44
     */
45
    private $highlighter;
46
47
    /**
48
     * Application constructor.
49
     * @throws \InvalidArgumentException
50
     * @throws \Railt\Lexer\Exception\BadLexemeException
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct(self::APP_NAME, self::APP_VERSION);
55
56
        $this->highlighter = $this->bootHighlight();
57
        $this->renderer    = new ExceptionRenderer($this->highlighter);
58
    }
59
60
    /**
61
     * @param string ...$paths
62
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
63
     */
64
    public function tryReadManifest(string ...$paths): void
65
    {
66
        foreach ($paths as $path) {
67
            if (\is_file($path) && \is_readable($path)) {
68
                $this->readManifest($path);
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param string $path
75
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
76
     */
77
    public function readManifest(string $path): void
78
    {
79
        $manifest = \json_decode(\file_get_contents($path), true);
80
81
        try {
82
            foreach ($manifest['commands'] ?? [] as $command) {
83
                $this->add(new $command());
84
            }
85
        } catch (\Throwable $e) {
86
            $this->throw($e);
87
        }
88
    }
89
90
    /**
91
     * @return Highlighter
92
     * @throws \InvalidArgumentException
93
     * @throws \Railt\Lexer\Exception\BadLexemeException
94
     */
95
    private function bootHighlight(): Highlighter
96
    {
97
        $hl = new Highlighter();
98
99
        $hl->add(new PHPLanguage());
100
        $hl->add(new GraphQLLanguage());
101
102
        return $hl;
103
    }
104
105
    /**
106
     * @param LanguageInterface $language
107
     * @return Application
108
     */
109
    public function addLanguage(LanguageInterface $language): self
110
    {
111
        $this->highlighter->add($language);
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param InputInterface|null $input
118
     * @param OutputInterface|null $output
119
     * @throws \Throwable
120
     */
121
    public function gracefulRun(InputInterface $input = null, OutputInterface $output = null): void
122
    {
123
        [$input, $output] = $this->bootPipes($input, $output);
124
125
        try {
126
            $this->setCatchExceptions(false);
127
            $code = parent::run($input, $output);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (run() instead of gracefulRun()). Are you sure this is correct? If so, you might want to change this to $this->run().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
128
        } catch (\Throwable $e) {
129
            $code = $this->exitCode($e);
130
            $this->throw($e);
131
        } finally {
132
            exit($code);
0 ignored issues
show
Bug introduced by
The variable $code does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
133
        }
134
    }
135
136
    /**
137
     * @param InputInterface|null $input
138
     * @param OutputInterface|null $output
139
     * @return array
140
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
141
     * @throws \Symfony\Component\Console\Exception\RuntimeException
142
     */
143
    private function bootPipes(InputInterface $input = null, OutputInterface $output = null): array
144
    {
145
        $input  = $input ?? new ArgvInput();
146
        $output = $output ?? new ConsoleOutput();
147
148
        return [$input, $output];
149
    }
150
151
    /**
152
     * @param \Throwable $e
153
     * @return int
154
     */
155
    private function exitCode(\Throwable $e): int
156
    {
157
        return $e->getCode() ?: 1;
158
    }
159
160
    /**
161
     * @param Command $command
162
     * @return Command
163
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
164
     */
165
    public function add(Command $command): Command
166
    {
167
        try {
168
            parent::add($command);
169
        } catch (\Throwable $e) {
170
            $this->throw($e);
171
        }
172
173
        return $command;
174
    }
175
176
    /**
177
     * @param \Throwable $e
178
     * @param ConsoleOutput|null $output
179
     * @return void
180
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
181
     */
182
    public function throw(\Throwable $e, ConsoleOutput $output = null): void
183
    {
184
        try {
185
            $result = $this->renderer->render($e);
186
        } catch (\Throwable $error) {
187
            $result = (string)new $error($error->getMessage(), $e->getCode(), $e);
188
        }
189
190
        ($output ?? new ConsoleOutput())->write($result);
191
192
        exit($this->exitCode($e));
193
    }
194
}
195