1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paysera\LoggingExtraBundle\Service\Formatter; |
6
|
|
|
|
7
|
|
|
use DateTimeInterface; |
8
|
|
|
use Doctrine\Common\Persistence\Proxy as LegacyProxy; |
9
|
|
|
use Doctrine\Persistence\Proxy; |
10
|
|
|
use Doctrine\ORM\PersistentCollection; |
11
|
|
|
use Monolog\Utils; |
12
|
|
|
use Throwable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* To be used on classes extending NormalizerFormatter |
16
|
|
|
*/ |
17
|
|
|
trait FormatterTrait |
18
|
|
|
{ |
19
|
|
|
protected function normalize($data, $depth = 0) |
20
|
|
|
{ |
21
|
|
|
$prenormalizedData = $this->prenormalizeData($data, $depth); |
22
|
|
|
|
23
|
|
|
return parent::normalize($prenormalizedData, $depth); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function prenormalizeData($data, $depth) |
27
|
|
|
{ |
28
|
|
|
if ($depth > 2) { |
29
|
|
|
return $this->getScalarRepresentation($data); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($data instanceof PersistentCollection) { |
33
|
|
|
return $data->isInitialized() ? iterator_to_array($data) : get_class($data); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($data instanceof Proxy || $data instanceof LegacyProxy) { |
|
|
|
|
37
|
|
|
return $this->normalizeProxy($data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ( |
41
|
|
|
is_object($data) |
42
|
|
|
&& !$data instanceof DateTimeInterface |
43
|
|
|
&& !$data instanceof Throwable |
44
|
|
|
) { |
45
|
|
|
return $this->normalizeObject($data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getScalarRepresentation($data) |
52
|
|
|
{ |
53
|
|
|
if (is_scalar($data) || $data === null) { |
54
|
|
|
return $data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (is_object($data)) { |
58
|
|
|
return get_class($data); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return gettype($data); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function normalizeObject($data) |
65
|
|
|
{ |
66
|
|
|
$result = []; |
67
|
|
|
foreach ((array)$data as $key => $value) { |
68
|
|
|
$parts = explode("\0", $key); |
69
|
|
|
$fixedKey = end($parts); |
70
|
|
|
if (substr($fixedKey, 0, 2) === '__') { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$result[$fixedKey] = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $result; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function normalizeProxy(Proxy $data) |
81
|
|
|
{ |
82
|
|
|
if ($data->__isInitialized()) { |
83
|
|
|
return $this->normalizeObject($data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (method_exists($data, 'getId')) { |
87
|
|
|
return ['id' => $data->getId()]; |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return '[Uninitialized]'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $data |
95
|
|
|
* @param bool $ignoreErrors |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function toJson($data, $ignoreErrors = false): string |
100
|
|
|
{ |
101
|
|
|
return Utils::jsonEncode( |
102
|
|
|
$data, |
103
|
|
|
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, |
104
|
|
|
$ignoreErrors |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.