Completed
Push — master ( bdaaad...4f4372 )
by personal
04:35 queued 45s
created

Issuer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Issue;
11
12
use PhpParser\Node;
13
use PhpParser\PrettyPrinter\Standard;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * Class Issuer
18
 * @package Hal\Component\Issue
19
 */
20
class Issuer
21
{
22
23
    /**
24
     * @var array
25
     */
26
    private $debug = [];
27
28
    /**
29
     * @var OutputInterface
30
     */
31
    private $output;
32
33
    /**
34
     * Issuer constructor.
35
     * @param OutputInterface $output
36
     */
37
    public function __construct(OutputInterface $output)
38
    {
39
        $this->output = $output;
40
    }
41
42
    /**
43
     * @param $errno
44
     * @param $errstr
45
     * @param $errfile
46
     * @param $errline
47
     * @throws \ErrorException
48
     */
49
    public function onError($errno, $errstr, $errfile, $errline)
50
    {
51
        if (error_reporting() == 0) {
52
            return;
53
        }
54
        $php = PHP_VERSION;
55
        $os = php_uname();
56
        $phpmetrics = getVersion();
57
        $traces = debug_backtrace(0, 10);
58
        $trace = '';
59
        foreach ($traces as $c) {
60
            if (isset($c['file'])) {
61
                $trace .= sprintf("+ %s (line %d)\n", $c['file'], $c['line']);
62
            }
63
        }
64
65
        $debug = '';
66
        foreach ($this->debug as $key => $value) {
67
            if ($value instanceof Node || is_array($value)) {
68
                $value = (new Standard())->prettyPrint($value);
0 ignored issues
show
Documentation introduced by
$value is of type object<PhpParser\Node>|array, but the function expects a array<integer,object<PhpParser\Node>>.

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...
69
            }
70
71
            $debug .= sprintf("%s: %s\n", $key, $value);
72
        }
73
74
        $logfile = './phpmetrics-error.log';
75
76
        $message = <<<EOT
77
78
<error>We're sorry : an unexpected error occured.</error>
79
 
80
<question>Can you help us ?</question> Please open a new issue at https://github.com/phpmetrics/PhpMetrics/issues/new, and copy-paste the content 
81
of this file: $logfile ?
82
83
Thanks for your help :)
84
85
EOT;
86
87
        $log = <<<EOT
88
## Title: $errstr
89
90
## Message:
91
92
Hi,
93
94
This issue occured:
95
96
$errstr
97
98
**Environment**
99
100
+ PHP: $php
101
+ PhpMetrics: $phpmetrics
102
+ Operating System: $os
103
+ File: $errfile (line $errline)
104
105
<details>
106
  <summary>Details</summary>
107
  ```
108
$trace
109
110
111
$debug
112
```
113
</details>
114
115
EOT;
116
117
        $this->output->write($message);
118
119
        $this->log($logfile, $log);
120
        $this->terminate(1);
121
    }
122
123
    /**
124
     * @return $this
125
     */
126
    public function enable()
127
    {
128
        set_error_handler([$this, 'onError']);
129
        return $this;
130
    }
131
132
    /**
133
     * @param $status
134
     */
135
    protected function terminate($status)
136
    {
137
        exit($status);
0 ignored issues
show
Coding Style Compatibility introduced by
The method terminate() 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...
138
    }
139
140
    /**
141
     * @param $log
142
     * @return $this
143
     */
144
    protected function log($logfile, $log)
145
    {
146
        if (is_writable(getcwd())) {
147
            file_put_contents($logfile, $log);
148
        } else {
149
            $this->output->write($log);
150
        }
151
        return $this;
152
    }
153
154
    /**
155
     * @param $debugKey
156
     * @param $value
157
     * @return $this
158
     */
159
    public function set($debugKey, $value)
160
    {
161
        $this->debug[$debugKey] = $value;
162
        return $this;
163
    }
164
165
    /**
166
     * @param $debugKey
167
     * @return $this
168
     */
169
    public function clear($debugKey)
170
    {
171
        unset($this->debug[$debugKey]);
172
        return $this;
173
    }
174
}
175
176