Test Failed
Pull Request — master (#3)
by
unknown
18:49
created

FormatterTrait   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 91
ccs 0
cts 41
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 6 1
B prenormalizeData() 0 24 9
A getScalarRepresentation() 0 12 4
A normalizeObject() 0 15 3
A normalizeProxy() 0 12 3
A toJson() 0 8 1
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) {
0 ignored issues
show
Bug introduced by
The class Doctrine\Common\Persistence\Proxy does not exist. Did you forget a USE statement, or did you not list all dependencies?

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 the composer.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 or require-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 ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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()];
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Doctrine\Persistence\Proxy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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