Completed
Push — feature/merge-build-stop-step ( d59daf )
by Tom
8s
created

Shell::writeException()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
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 N98\Magento\Command\Developer\Console\Exception\NoModuleDefinedException;
6
use N98\Util\BinaryString;
7
use Psy\Configuration;
8
use Psy\Exception\ErrorException;
9
use Psy\Exception\FatalErrorException;
10
use Psy\Exception\ParseErrorException;
11
use Psy\Shell as PsyShell;
12
use Symfony\Component\Console\Formatter\OutputFormatter;
13
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
14
use Symfony\Component\Console\Helper\HelperInterface;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\ConsoleOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Shell extends PsyShell
20
{
21
    /**
22
     * @var string
23
     */
24
    private $prompt = '';
25
26
    /**
27
     * @var ConsoleOutput
28
     */
29
    private $consoleOutput;
30
31
    public function run(InputInterface $input = null, OutputInterface $output = null)
32
    {
33
        $this->registerHelpersFromMainApplication();
34
35
        return parent::run($input, $output);
36
    }
37
38
    private function registerHelpersFromMainApplication()
39
    {
40
        $helperSetMagerun = $this->getScopeVariable('magerun')->getHelperSet();
41
        $helperSetPsy = $this->getHelperSet();
42
43
        foreach ($helperSetMagerun as $helper) {
44
            /** @var $helper HelperInterface */
45
            if (!$helperSetPsy->has($helper->getName())) {
46
                $helperSetPsy->set($helper);
47
            }
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
     * @param OutputInterface $output An OutputInterface instance
0 ignored issues
show
Bug introduced by
There is no parameter named $output. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
62
     */
63
    public function writeException(\Exception $e)
64
    {
65
        $this->resetCodeBuffer();
66
67
        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...
68
            $this->getConsoleOutput()->writeln('<warning>' . $e->getMessage() . '</warning>');
69
            return;
70
        } elseif ($e instanceof ErrorException) {
71
            if (BinaryString::startsWith($e->getMessage(), 'PHP error:  Use of undefined constant')) {
72
                $this->getConsoleOutput()->writeln('<warning>Unknown command</warning>');
73
                return;
74
            }
75
        } elseif ($e instanceof FatalErrorException) {
76
            if (BinaryString::startsWith($e->getMessage(), 'PHP Fatal error:  Call to undefined function')) {
77
                $this->getConsoleOutput()->writeln('<warning>Unknown function</warning>');
78
                return;
79
            }
80
        } elseif ($e instanceof ParseErrorException) {
81
            $message = substr($e->getMessage(), 0, strpos($e->getMessage(), ' on line'));
82
            $this->getConsoleOutput()->writeln('<error>' . $message . '</error>');
83
            return;
84
        }
85
86
        throw $e;
87
    }
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
}