ErrorHandler::__invoke()   C
last analyzed

Complexity

Conditions 17
Paths 17

Size

Total Lines 49
Code Lines 34

Duplication

Lines 10
Ratio 20.41 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
dl 10
loc 49
ccs 0
cts 45
cp 0
rs 5.1117
c 0
b 0
f 0
cc 17
eloc 34
nc 17
nop 2
crap 306

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Tool;
8
9
use Symfony\Component\Console\Helper\DialogHelper;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Zicht\Tool\Container\ExecutionAbortedException;
13
14
/**
15
 * Wrapper class for handling errors
16
 */
17
class ErrorHandler
18
{
19
    private $input;
20
    private $output;
21
    private $dialog;
22
    private $repeating = array();
23
    private $continueAlways = false;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     */
31
    public function __construct($input, $output)
32
    {
33
        $this->input = $input;
34
        $this->output = $output;
35
        $this->dialog = new DialogHelper();
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Console\Helper\DialogHelper has been deprecated with message: since version 2.5, to be removed in 3.0. Use {@link \Symfony\Component\Console\Helper\QuestionHelper} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
36
    }
37
38
    /**
39
     * Handler implementation which allows for some user interaction based on emitted user errors.
40
     *
41
     * @param int $err
42
     * @param string $errstr
43
     * @return void
44
     *
45
     * @throws Container\ExecutionAbortedException
46
     */
47
    public function __invoke($err, $errstr)
48
    {
49
        if (in_array($errstr, $this->repeating)) {
50
            return;
51
        }
52
        $this->repeating[] = $errstr;
53
        if (
54
            error_reporting() & E_USER_DEPRECATED
55
            || error_reporting() & E_USER_NOTICE
56
            || error_reporting() & E_USER_WARNING
57
            || error_reporting() & E_RECOVERABLE_ERROR
58
        ) {
59
            switch ($err) {
60
                case E_USER_WARNING:
61
                    fprintf(STDERR, $this->output->getFormatter()->format("<comment>[WARNING]</comment>   $errstr\n"));
62
63
                    if (!$this->continueAlways) {
64
                        do {
65
                            if ($this->input->isInteractive()) {
66
                                $answer = $this->dialog->ask($this->output, "Continue anyway? (y)es, (n)o, (a)lways ", false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                            } else {
68
                                $answer = 'n';
69
                            }
70
                        } while (!in_array(strtolower($answer), array('y', 'n', 'a')));
71
72
                        if ($answer === 'n') {
73
                            throw new ExecutionAbortedException("Aborted by user request");
74
                        } elseif ($answer === 'a') {
75
                            $this->continueAlways = true;
76
                        }
77
                    }
78
79
80
                    break;
81 View Code Duplication
                case E_USER_NOTICE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                    if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
83
                        fprintf(STDERR, $this->output->getFormatter()->format("<comment>[NOTICE]</comment>   $errstr\n"));
84
                    }
85
                    break;
86 View Code Duplication
                case E_USER_DEPRECATED:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
                    if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
88
                        fprintf(STDERR, $this->output->getFormatter()->format("<comment>[DEPRECATED]</comment>   $errstr\n"));
89
                    }
90
                    break;
91
                case E_RECOVERABLE_ERROR:
92
                    throw new \ErrorException($errstr);
93
            }
94
        }
95
    }
96
}
97