railt /
console
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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
|
|||
| 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); |
||
| 128 | } catch (\Throwable $e) { |
||
| 129 | $code = $this->exitCode($e); |
||
| 130 | $this->throw($e); |
||
| 131 | } finally { |
||
| 132 | exit($code); |
||
|
0 ignored issues
–
show
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
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 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: