Passed
Push — stable ( 3da3e1...b1f606 )
by Nuno
03:57 queued 02:04
created

src/Writer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * This file is part of Collision.
5
 *
6
 * (c) Nuno Maduro <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace NunoMaduro\Collision;
13
14
use Whoops\Exception\Frame;
15
use Whoops\Exception\Inspector;
16
use Symfony\Component\Console\Output\ConsoleOutput;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use NunoMaduro\Collision\Contracts\Writer as WriterContract;
19
use NunoMaduro\Collision\Contracts\Highlighter as HighlighterContract;
20
use NunoMaduro\Collision\Contracts\ArgumentFormatter as ArgumentFormatterContract;
21
22
/**
23
 * This is an Collision Writer implementation.
24
 *
25
 * @author Nuno Maduro <[email protected]>
26
 */
27
class Writer implements WriterContract
28
{
29
    /**
30
     * The number of frames if no verbosity is specified.
31
     */
32
    const VERBOSITY_NORMAL_FRAMES = 1;
33
34
    /**
35
     * Holds an instance of the Output.
36
     *
37
     * @var \Symfony\Component\Console\Output\OutputInterface
38
     */
39
    protected $output;
40
41
    /**
42
     * Holds an instance of the Argument Formatter.
43
     *
44
     * @var \NunoMaduro\Collision\Contracts\ArgumentFormatter
45
     */
46
    protected $argumentFormatter;
47
48
    /**
49
     * Holds an instance of the Highlighter.
50
     *
51
     * @var \NunoMaduro\Collision\Contracts\Highlighter
52
     */
53
    protected $highlighter;
54
55
    /**
56
     * Ignores traces where the file string matches one
57
     * of the provided regex expressions.
58
     *
59
     * @var string[]
60
     */
61
    protected $ignore = [];
62
63
    /**
64
     * Declares whether or not the trace should appear.
65
     *
66
     * @var bool
67
     */
68
    protected $showTrace = true;
69
70
    /**
71
     * Declares whether or not the editor should appear.
72
     *
73
     * @var bool
74
     */
75
    protected $showEditor = true;
76
77
    /**
78
     * Creates an instance of the writer.
79
     *
80
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
81
     * @param \NunoMaduro\Collision\Contracts\ArgumentFormatter|null $argumentFormatter
82
     * @param \NunoMaduro\Collision\Contracts\Highlighter|null $highlighter
83
     */
84 14
    public function __construct(
85
        OutputInterface $output = null,
86
        ArgumentFormatterContract $argumentFormatter = null,
87
        HighlighterContract $highlighter = null
88
    ) {
89 14
        $this->output = $output ?: new ConsoleOutput;
0 ignored issues
show
Documentation Bug introduced by
It seems like $output ?: new \Symfony\...\Output\ConsoleOutput() can also be of type object<Symfony\Component...e\Output\ConsoleOutput>. However, the property $output is declared as type object<Symfony\Component...Output\OutputInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
90 14
        $this->argumentFormatter = $argumentFormatter ?: new ArgumentFormatter;
91 14
        $this->highlighter = $highlighter ?: new Highlighter;
92 14
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 5
    public function write(Inspector $inspector): void
98
    {
99 5
        $this->renderTitle($inspector);
100
101 5
        $frames = $this->getFrames($inspector);
102
103 5
        $editorFrame = array_shift($frames);
104 5
        if ($this->showEditor && $editorFrame !== null) {
105 4
            $this->renderEditor($editorFrame);
106
        }
107
108 5
        if ($this->showTrace && ! empty($frames)) {
109 4
            $this->renderTrace($frames);
110
        } else {
111 1
            $this->output->writeln('');
112
        }
113 5
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118 1
    public function ignoreFilesIn(array $ignore): WriterContract
119
    {
120 1
        $this->ignore = $ignore;
121
122 1
        return $this;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function showTrace(bool $show): WriterContract
129
    {
130 1
        $this->showTrace = $show;
131
132 1
        return $this;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function showEditor(bool $show): WriterContract
139
    {
140 1
        $this->showEditor = $show;
141
142 1
        return $this;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2
    public function setOutput(OutputInterface $output): WriterContract
149
    {
150 2
        $this->output = $output;
151
152 2
        return $this;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 7
    public function getOutput(): OutputInterface
159
    {
160 7
        return $this->output;
161
    }
162
163
    /**
164
     * Returns pertinent frames.
165
     *
166
     * @param  \Whoops\Exception\Inspector $inspector
167
     *
168
     * @return array
169
     */
170 5
    protected function getFrames(Inspector $inspector): array
171
    {
172 5
        return $inspector->getFrames()
173 5
            ->filter(
174
                function ($frame) {
175 5
                    foreach ($this->ignore as $ignore) {
176 1
                        if (preg_match($ignore, $frame->getFile())) {
177 1
                            return false;
178
                        }
179
                    }
180
181 5
                    return true;
182 5
                }
183
            )
184 5
            ->getArray();
185
    }
186
187
    /**
188
     * Renders the title of the exception.
189
     *
190
     * @param \Whoops\Exception\Inspector $inspector
191
     *
192
     * @return \NunoMaduro\Collision\Contracts\Writer
193
     */
194 5
    protected function renderTitle(Inspector $inspector): WriterContract
195
    {
196 5
        $exception = $inspector->getException();
197 5
        $message = $exception->getMessage();
198 5
        $class = $inspector->getExceptionName();
199
200 5
        $this->render("<bg=red;options=bold> $class </> : <comment>$message</>");
201
202 5
        return $this;
203
    }
204
205
    /**
206
     * Renders the editor containing the code that was the
207
     * origin of the exception.
208
     *
209
     * @param \Whoops\Exception\Frame $frame
210
     *
211
     * @return \NunoMaduro\Collision\Contracts\Writer
212
     */
213 4
    protected function renderEditor(Frame $frame): WriterContract
214
    {
215 4
        $this->render('at <fg=green>'.$frame->getFile().'</>'.':<fg=green>'.$frame->getLine().'</>');
216
217 4
        $content = $this->highlighter->highlight((string) $frame->getFileContents(), (int) $frame->getLine());
218
219 4
        $this->output->writeln($content);
220
221 4
        return $this;
222
    }
223
224
    /**
225
     * Renders the trace of the exception.
226
     *
227
     * @param  array $frames
228
     *
229
     * @return \NunoMaduro\Collision\Contracts\Writer
230
     */
231 4
    protected function renderTrace(array $frames): WriterContract
232
    {
233 4
        $this->render('<comment>Exception trace:</comment>');
234 4
        foreach ($frames as $i => $frame) {
235 4
            if ($i > static::VERBOSITY_NORMAL_FRAMES && $this->output->getVerbosity(
236 4
                ) < OutputInterface::VERBOSITY_VERBOSE) {
237 3
                $this->render('<info>Please use the argument <fg=red>-v</> to see more details.</info>');
238 3
                break;
239
            }
240
241 4
            $file = $frame->getFile();
242 4
            $line = $frame->getLine();
243 4
            $class = empty($frame->getClass()) ? '' : $frame->getClass().'::';
244 4
            $function = $frame->getFunction();
245 4
            $args = $this->argumentFormatter->format($frame->getArgs());
246 4
            $pos = str_pad($i + 1, 4, ' ');
247
248 4
            $this->render("<comment><fg=cyan>$pos</>$class$function($args)</comment>");
249 4
            $this->render("    <fg=green>$file</>:<fg=green>$line</>", false);
250
        }
251
252 4
        return $this;
253
    }
254
255
    /**
256
     * Renders an message into the console.
257
     *
258
     * @param  string $message
259
     * @param  bool $break
260
     *
261
     * @return $this
262
     */
263 5
    protected function render(string $message, bool $break = true): WriterContract
264
    {
265 5
        if ($break) {
266 5
            $this->output->writeln('');
267
        }
268
269 5
        $this->output->writeln("  $message");
270
271 5
        return $this;
272
    }
273
}
274