Completed
Push — master ( c6e1e1...d4cc5d )
by personal
02:20
created

Issuer::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
rs 9.6666
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 Hal\Component\Output\Output;
13
use PhpParser\Node;
14
use PhpParser\PrettyPrinter\Standard;
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(Output $output)
38
    {
39
        $this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
It seems like $output of type object<Hal\Component\Output\Output> is incompatible with the declared type object<Hal\Component\Issue\OutputInterface> of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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
     * @return $this
134
     */
135
    public function disable()
136
    {
137
        restore_error_handler();
138
        return $this;
139
    }
140
141
    /**
142
     * @param $status
143
     */
144
    protected function terminate($status)
145
    {
146
        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...
147
    }
148
149
    /**
150
     * @param $log
151
     * @return $this
152
     */
153
    protected function log($logfile, $log)
154
    {
155
        if (is_writable(getcwd())) {
156
            file_put_contents($logfile, $log);
157
        } else {
158
            $this->output->write($log);
159
        }
160
        return $this;
161
    }
162
163
    /**
164
     * @param $debugKey
165
     * @param $value
166
     * @return $this
167
     */
168
    public function set($debugKey, $value)
169
    {
170
        $this->debug[$debugKey] = $value;
171
        return $this;
172
    }
173
174
    /**
175
     * @param $debugKey
176
     * @return $this
177
     */
178
    public function clear($debugKey)
179
    {
180
        unset($this->debug[$debugKey]);
181
        return $this;
182
    }
183
}
184
185