Completed
Push — feature/php-7.1-partial ( 412847 )
by Tom
03:40
created

Shell::writeException()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 8
nop 1
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Exception;
6
use N98\Magento\Command\Developer\Console\Exception\NoModuleDefinedException;
7
use N98\Util\BinaryString;
8
use Psy\Exception\BreakException;
9
use Psy\Exception\ErrorException;
10
use Psy\Exception\FatalErrorException;
11
use Psy\Exception\ParseErrorException;
12
use Psy\Shell as PsyShell;
13
use Symfony\Component\Console\Formatter\OutputFormatter;
14
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
15
use Symfony\Component\Console\Helper\HelperInterface;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\ConsoleOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class Shell extends PsyShell
21
{
22
    /**
23
     * @var string
24
     */
25
    private $prompt = '';
26
27
    /**
28
     * @var ConsoleOutput
29
     */
30
    private $consoleOutput;
31
32
    /**
33
     * @param InputInterface|null $input
34
     * @param OutputInterface|null $output
35
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be null|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
36
     */
37
    public function run(InputInterface $input = null, OutputInterface $output = null)
38
    {
39
        $this->registerHelpersFromMainApplication();
40
41
        return parent::run($input, $output);
42
    }
43
44
    private function registerHelpersFromMainApplication()
45
    {
46
        $helperSetMagerun = $this->getScopeVariable('magerun')->getHelperSet();
47
        $helperSetPsy = $this->getHelperSet();
48
49
        foreach ($helperSetMagerun as $helper) {
50
            /** @var $helper HelperInterface */
51
            if (!$helperSetPsy->has($helper->getName())) {
52
                $helperSetPsy->set($helper);
53
            }
54
        }
55
    }
56
57
    /**
58
     * Renders a caught Exception.
59
     *
60
     * Exceptions are formatted according to severity. ErrorExceptions which were
61
     * warnings or Strict errors aren't rendered as harshly as real errors.
62
     *
63
     * Stores $e as the last Exception in the Shell Context.
64
     *
65
     * @param Exception $e An exception instance
66
     * @throws ErrorException
67
     * @throws FatalErrorException
68
     */
69
    public function writeException(Exception $e)
70
    {
71
        $this->resetCodeBuffer();
72
73
        if ($e instanceof BreakException) {
74
            return;
75
        }
76
77
        if ($e instanceof NoModuleDefinedException) {
0 ignored issues
show
Bug introduced by
The class N98\Magento\Command\Deve...oModuleDefinedException does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
78
            $this->getConsoleOutput()->writeln('<warning>' . $e->getMessage() . '</warning>');
79
80
            return;
81
        } elseif ($e instanceof ErrorException) {
82
            if (BinaryString::startsWith($e->getMessage(), 'PHP error:  Use of undefined constant')) {
83
                $this->getConsoleOutput()->writeln('<warning>Unknown command</warning>');
84
            }
85
86
            return;
87
        } elseif ($e instanceof FatalErrorException) {
88
            if (BinaryString::startsWith($e->getMessage(), 'PHP Fatal error:  Call to undefined function')) {
89
                $this->getConsoleOutput()->writeln('<warning>Unknown function</warning>');
90
            }
91
92
            return;
93
        } elseif ($e instanceof ParseErrorException) {
94
            $message = substr($e->getMessage(), 0, strpos($e->getMessage(), ' on line'));
95
            $this->getConsoleOutput()->writeln('<error>' . $message . '</error>');
96
97
            return;
98
        }
99
100
        parent::writeException($e);
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    protected function getPrompt()
107
    {
108
        if (!empty($this->prompt)) {
109
            return $this->prompt;
110
        }
111
112
        return parent::getPrompt();
113
    }
114
115
    /**
116
     * @param string $prompt
117
     */
118
    public function setPrompt($prompt)
119
    {
120
        $this->prompt = $prompt;
121
    }
122
123
    /**
124
     * Resets prompt to default
125
     */
126
    public function resetPrompt()
127
    {
128
        $this->prompt = '';
129
    }
130
131
    /**
132
     * @return ConsoleOutput
133
     */
134
    private function getConsoleOutput()
135
    {
136
        if (empty($this->consoleOutput)) {
137
            $formatter = new OutputFormatter();
138
            $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));
139
            $this->consoleOutput = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
140
        }
141
142
        return $this->consoleOutput;
143
    }
144
}
145