Completed
Pull Request — master (#7)
by Thomas Mauro
05:04
created

SanitizeDataProcessor::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\SentryModule\Processor;
6
7
use Raven_Client;
8
use Raven_Processor;
9
10
/**
11
 * Class SanitizeDataProcessor.
12
 */
13
final class SanitizeDataProcessor extends Raven_Processor
14
{
15
    /**
16
     * @var Raven_Processor
17
     */
18
    protected $previous;
19
20
    /**
21
     * SanitizeDataProcessor constructor.
22
     *
23
     * @param Raven_Client $client
24
     * @param Raven_Processor|null $previous
25
     */
26 5
    public function __construct(Raven_Client $client, Raven_Processor $previous = null)
27
    {
28 5
        parent::__construct($client);
29
30 5
        $this->previous = $previous ?: new \Raven_Processor_SanitizeDataProcessor($client);
31 5
    }
32
33
    /**
34
     * @param mixed  $item Associative array value
35
     *
36
     * @return mixed
37
     */
38 3
    public function sanitize(&$item)
39
    {
40 3
        if (null === $item
41 3
            || is_scalar($item)
42 3
            || (is_object($item) && method_exists($item, '__toString'))
43
        ) {
44 3
            $item = is_object($item) ? '[Object: "'.addslashes((string) $item).'"]' : $item;
45 3
        } elseif (is_object($item)) {
46 3
            $item = '[object '.get_class($item).']';
47
        } else {
48 3
            $item = '['.gettype($item).']';
49
        }
50
51 3
        return $item;
52
    }
53
54
    /**
55
     * Process and sanitize data, modifying the existing value if necessary.
56
     *
57
     * @param array $data Array of log data
58
     */
59 3
    public function process(&$data)
60
    {
61 3
        $this->previous->process($data);
62
63 3
        if (! empty($data['exception'])) {
64 1
            $data['exception'] = $this->sanitizeException($data['exception']);
65
        }
66 3
        if (! empty($data['stacktrace'])) {
67 1
            $data['stacktrace'] = $this->sanitizeStacktrace($data['stacktrace']);
68
        }
69 3
        if (! empty($data['extra'])) {
70 1
            array_walk_recursive($data['extra'], [$this, 'sanitize']);
71
        }
72 3
    }
73
74 1
    public function sanitizeException(array $data): array
75
    {
76 1
        if (! array_key_exists('values', $data) || ! is_array($data['values'])) {
77
            return $data;
78
        }
79
80 1
        $values = $data['values'] ?? [];
81
82 1
        if (! count($values)) {
83
            return $data;
84
        }
85
86 1
        foreach ($values as $key => $value) {
87 1
            $values[$key] = $this->sanitizeStacktrace($value);
88
        }
89
90 1
        $data['values'] = $values;
91
92 1
        return $data;
93
    }
94
95 2
    public function sanitizeStacktrace(array $data): array
96
    {
97 2
        if (! array_key_exists('frames', $data) || ! is_array($data['frames'])) {
98
            return $data;
99
        }
100
101 2
        $frames = $data['frames'] ?? [];
102
103 2
        if (! count($frames)) {
104
            return $data;
105
        }
106
107 2
        foreach ($frames as $key => $frame) {
108 2
            if (! is_array($frame)) {
109
                continue;
110
            }
111
112 2
            if (! array_key_exists('vars', $frame) || ! is_array($frame['vars'])) {
113
                continue;
114
            }
115
116 2
            array_walk_recursive($frame['vars'], [$this, 'sanitize']);
117
118 2
            $frames[$key] = $frame;
119
        }
120
121 2
        $data['frames'] = $frames;
122
123 2
        return $data;
124
    }
125
}
126