Completed
Pull Request — master (#13)
by
unknown
01:15
created

Whoops_Run_Composite::silenceErrorsInPaths()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace Rarst\wps;
3
4
use Whoops\Exception\ErrorException;
5
use Whoops\Exception\Inspector;
6
use Whoops\Run;
7
use Whoops\Util\SystemFacade;
8
9
/**
10
 * This class will show Errors and Warnings only if they are coming from
11
 * Specific Plugin
12
 */
13
class Whoops_Run_Composite {
14
15
    /**
16
     * @var SystemFacade
17
     */
18
    private $system;
19
    
20
    /**
21
     * @var Run
22
     */
23
    private $run;
24
    
25
    private $skipAllNoticesAndWarnings = false;
26
27
    private $pathPatterns;
28
29
    public function __construct(SystemFacade $system = null)
30
    {
31
        $this->system = $system ?: new SystemFacade;
32
        $this->run = new Run($this->system);
33
    }
34
35
    public function register() {
36
        class_exists("\\Whoops\\Exception\\ErrorException");
37
        class_exists("\\Whoops\\Exception\\FrameCollection");
38
        class_exists("\\Whoops\\Exception\\Frame");
39
        class_exists("\\Whoops\\Exception\\Inspector");
40
41
        $this->system->setErrorHandler([$this, Run::ERROR_HANDLER]);
42
        $this->system->setExceptionHandler([$this->run, Run::EXCEPTION_HANDLER]);
43
        $this->system->registerShutdownFunction([$this->run, Run::SHUTDOWN_HANDLER]);
44
    }
45
46
    public function unregister() {
47
        $this->system->restoreExceptionHandler();
48
        $this->system->restoreErrorHandler();
49
    }
50
51
    public function pushHandler($handler) {
52
        $this->run->pushHandler($handler);
53
    }
54
55
    public function skipAllNoticesAndWarnings() {
56
        $this->skipAllNoticesAndWarnings = true;
57
    }
58
59
    public function watchFilesWithPatterns($pathPatterns) {
60
        $this->pathPatterns = $pathPatterns;
61
    }
62
63
    /**
64
     * Silence particular errors in particular files
65
     * @param  array|string $patterns List or a single regex pattern to match
66
     * @param  int          $levels   Defaults to E_STRICT | E_DEPRECATED
67
     * @return Run
68
     * @see https://maximivanov.github.io/php-error-reporting-calculator/
69
     */
70
    public function silenceErrorsInPaths($patterns, $levels = 10240) {
71
        $this->run->silenceErrorsInPaths($patterns, $levels);
72
    }
73
74
    /**
75
     * Converts generic PHP errors to \ErrorException
76
     * instances, before passing them off to be handled.
77
     *
78
     * This method MUST be compatible with set_error_handler.
79
     *
80
     * @param int    $level
81
     * @param string $message
82
     * @param string $file
83
     * @param int    $line
84
     *
85
     * @return bool
86
     * @throws ErrorException
87
     */
88
    public function handleError($level, $message, $file = null, $line = null) {
89
90
        // Handle all fatals, exceptions etc.
91
        if(in_array(
92
            $level, 
93
            [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR]
94
        )) {
95
            $this->run->handleError($level, $message, $file, $line);
96
            return false;
97
        }
98
99
        // If All Notices and Warnings should be skipped, then short-circuit
100
        // error handler.
101
        if($this->skipAllNoticesAndWarnings) {
102
            return false;
103
        }
104
105
        // Watch for everything
106
        if( empty($this->pathPatterns) ) {
107
            $this->run->handleError($level, $message, $file, $line);
108
            return false;
109
        }
110
111
        $pathPatternsToWatchFor = (array) $this->pathPatterns;
112
        $errorBelongsToWatchCriteria = false;
113
        
114
        // We are creating an exception object because Inspector Class needs it.
115
        $exception = new ErrorException($message, /*code*/ $level, /*severity*/ $level, $file, $line);
116
        $frames = (new Inspector($exception))->getFrames();
0 ignored issues
show
Documentation introduced by
$exception is of type object<Whoops\Exception\ErrorException>, but the function expects a object<Throwable>.

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...
117
        foreach( $frames as $frame ) {
118
            foreach($pathPatternsToWatchFor as $pathPattern) {
119
                $pathMatches = (bool) preg_match($pathPattern, $frame->getFile());
120
                if ($pathMatches) {
121
                    $errorBelongsToWatchCriteria = true;
122
                    break 2;
123
                }
124
            }
125
        }
126
127
        if($errorBelongsToWatchCriteria) {
128
            $this->run->handleError($level, $message, $file, $line);
129
        }
130
131
        return false;
132
    }
133
}