ExceptionHandler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Geekish\Crap;
4
5
use Exception;
6
use Throwable;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class ExceptionHandler
11
 * @package Geekish\Crap
12
 */
13
class ExceptionHandler
14
{
15
    /**
16
     * @var OutputInterface
17
     */
18
    protected $output;
19
20
    /**
21
     * Create ExceptionHandler
22
     *
23
     * @param OutputInterface $output
24
     */
25
    public function __construct(OutputInterface $output)
26
    {
27
        $this->output = $output;
28
    }
29
30
    /**
31
     * Handle the Exception/Throwable
32
     * We avoid type hinting to support both 5.6 & 7.x
33
     *
34
     * @param Exception|Throwable $e
35
     *
36
     * @param bool $exit
37
     * @return int|void
38
     */
39
    public function __invoke($e, $exit = true)
40
    {
41
        $this->output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
42
43
        return $exit ? exit(1) : 1;
0 ignored issues
show
Coding Style Compatibility introduced by
The method __invoke() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
44
    }
45
}
46