Completed
Push — master ( f42a76...e2cf37 )
by Andreas
05:39
created

TypeConverter::convertToModernType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4286
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
namespace Alcaeus\MongoDbAdapter;
17
18
/**
19
 * @internal
20
 */
21
class TypeConverter
22
{
23 7
    public static function convertLegacyArrayToObject($array)
24
    {
25
        // TODO: provide actual class
26 7
        $result = new \stdClass();
27
28 7
        foreach ($array as $key => $value) {
29 7
            $result->$key = (is_array($value)) ? static::convertLegacyArrayToObject($value) : static::convertToBSONType($value);
30 7
        }
31
32 7
        return $result;
33
    }
34
35 2
    public static function convertObjectToLegacyArray($object)
36
    {
37 2
        $result = [];
38
39 2
        foreach ($object as $key => $value) {
40
            // TODO: maybe add a more meaningful check instead of stdClass?
41 2
            $result[$key] = ($value instanceof \stdClass) ? static::convertObjectToLegacyArray($value) : static::convertToLegacyType($value);
42 2
        }
43
44 2
        return $result;
45
    }
46
47 2
    public static function convertToLegacyType($value)
48
    {
49 2
        switch (true) {
50 2
            case $value instanceof \MongoDB\BSON\ObjectID:
1 ignored issue
show
Bug introduced by
The class MongoDB\BSON\ObjectID 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...
51 2
                return new \MongoId($value);
52
53 2
            default:
54 2
                return $value;
55 2
        }
56
    }
57
58 7
    public static function convertToBSONType($value)
59
    {
60 7
        switch (true) {
61 7
            case $value instanceof TypeInterface:
62 2
                return $value->toBSONType();
63
64 7
            default:
65 7
                return $value;
66 7
        }
67
    }
68
}
69