Completed
Push — master ( c6f91f...0eb918 )
by Alessandro
05:12
created

MongoQuerySerializer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 0
cbo 1
dl 0
loc 65
ccs 24
cts 26
cp 0.9231
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 6 1
A prepareUnserializableData() 0 13 3
C prepareItemData() 0 28 8
1
<?php declare(strict_types = 1);
2
3
namespace Facile\MongoDbBundle\DataCollector;
4
5
use Facile\MongoDbBundle\Models\Query;
6
use MongoDB\BSON\Serializable;
7
8
/**
9
 * Class MongoQuerySerializer
10
 * @internal
11
 */
12
final class MongoQuerySerializer
13
{
14
    /**
15
     * @param Query $query
16
     */
17 6
    public static function serialize(Query $query)
18
    {
19 6
        $query->setFilters(self::prepareUnserializableData($query->getFilters()));
20 6
        $query->setData(self::prepareUnserializableData($query->getData()));
21 6
        $query->setOptions(self::prepareUnserializableData($query->getOptions()));
22 6
    }
23
24
    /**
25
     * @param array|object $data
26
     *
27
     * @return array|object
28
     */
29 7
    private static function prepareUnserializableData($data)
30
    {
31 7
        if ($data instanceof Serializable) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\Serializable 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...
32 1
            $data = $data->bsonSerialize();
33
        }
34
35 7
        $newData = [];
36 7
        foreach ($data as $key => $item) {
37 7
            $newData[$key] = self::prepareItemData($item);
38
        }
39
40 7
        return $newData;
41
    }
42
43
    /**
44
     * @param mixed $item
45
     *
46
     * @return mixed
47
     */
48 7
    public static function prepareItemData($item)
49
    {
50 7
        if (\is_string($item)) {
51 4
            return $item;
52
        }
53
54 6
        if (method_exists($item, 'getArrayCopy')) {
55 3
            return self::prepareUnserializableData($item->getArrayCopy());
56
        }
57
58 4
        if (method_exists($item, 'toDateTime')) {
59 1
            return 'ISODate("'.$item->toDateTime()->format('c').'")';
60
        }
61
62 3
        if (method_exists($item, '__toString')) {
63
            return $item->__toString();
64
        }
65
66 3
        if ($item instanceof Serializable) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\Serializable 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...
67
            return $item->bsonSerialize();
68
        }
69
70 3
        if (is_array($item) || is_object($item)) {
71 2
            return self::prepareUnserializableData((array) $item);
72
        }
73
74 2
        return $item;
75
    }
76
}
77