Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

Shell::writeException()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 25
rs 6.7272
cc 7
eloc 18
nc 7
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\Configuration;
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
    public function run(InputInterface $input = null, OutputInterface $output = null)
33
    {
34
        $this->registerHelpersFromMainApplication();
35
36
        return parent::run($input, $output);
37
    }
38
39
    private function registerHelpersFromMainApplication()
40
    {
41
        $helperSetMagerun = $this->getScopeVariable('magerun')->getHelperSet();
42
        $helperSetPsy = $this->getHelperSet();
43
44
        foreach ($helperSetMagerun as $helper) {
45
            /** @var $helper HelperInterface */
46
            if (!$helperSetPsy->has($helper->getName())) {
47
                $helperSetPsy->set($helper);
48
            }
49
        }
50
    }
51
52
    /**
53
     * Renders a caught Exception.
54
     *
55
     * Exceptions are formatted according to severity. ErrorExceptions which were
56
     * warnings or Strict errors aren't rendered as harshly as real errors.
57
     *
58
     * Stores $e as the last Exception in the Shell Context.
59
     *
60
     * @param Exception $e An exception instance
61
     * @throws ErrorException
62
     * @throws FatalErrorException
63
     */
64
    public function writeException(Exception $e)
65
    {
66
        $this->resetCodeBuffer();
67
68
        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...
69
            $this->getConsoleOutput()->writeln('<warning>' . $e->getMessage() . '</warning>');
70
            return;
71
        } elseif ($e instanceof ErrorException) {
72
            if (BinaryString::startsWith($e->getMessage(), 'PHP error:  Use of undefined constant')) {
73
                $this->getConsoleOutput()->writeln('<warning>Unknown command</warning>');
74
                return;
75
            }
76
        } elseif ($e instanceof FatalErrorException) {
77
            if (BinaryString::startsWith($e->getMessage(), 'PHP Fatal error:  Call to undefined function')) {
78
                $this->getConsoleOutput()->writeln('<warning>Unknown function</warning>');
79
                return;
80
            }
81
        } elseif ($e instanceof ParseErrorException) {
82
            $message = substr($e->getMessage(), 0, strpos($e->getMessage(), ' on line'));
83
            $this->getConsoleOutput()->writeln('<error>' . $message . '</error>');
84
            return;
85
        }
86
87
        throw $e;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    protected function getPrompt()
94
    {
95
        if (!empty($this->prompt)) {
96
            return $this->prompt;
97
        }
98
99
        return parent::getPrompt();
100
    }
101
102
    /**
103
     * @param string $prompt
104
     */
105
    public function setPrompt($prompt)
106
    {
107
        $this->prompt =  $prompt;
108
    }
109
110
    /**
111
     * Resets prompt to default
112
     */
113
    public function resetPrompt()
114
    {
115
        $this->prompt = '';
116
    }
117
118
    /**
119
     * @return ConsoleOutput
120
     */
121
    private function getConsoleOutput()
122
    {
123
        if (empty($this->consoleOutput)) {
124
            $formatter = new OutputFormatter();
125
            $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow'));
126
            $this->consoleOutput = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
127
        }
128
129
        return $this->consoleOutput;
130
    }
131
}
132