Passed
Pull Request — develop (#82)
by Csaba
02:33
created

ObjectMapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 66
c 1
b 0
f 0
wmc 11
lcom 0
cbo 1
ccs 0
cts 38
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B map() 0 25 5
A setPropertyValue() 0 18 3
A getPropertyValue() 0 18 3
1
<?php
2
namespace Fathomminds\Rest\Helpers;
3
4
use Fathomminds\Rest\Exceptions\RestException;
5
6
abstract class ObjectMapper
7
{
8
    public static function map($object, $map)
9
    {
10
        if (gettype($object) !== 'object') {
11
            throw new RestException('ObjectMapper map method expects object as first parameter', [
12
                'parameter' => $object,
13
            ]);
14
        }
15
        if (!is_array($map)) {
16
            throw new RestException('ObjectMapper map method expects array as second parameter', [
17
                'parameter' => $map,
18
            ]);
19
        }
20
        $mappedObject = new \StdClass();
21
        foreach ($map as $targetFieldName => $sourceFieldName) {
22
            list($propertyExists, $propertyValue) = static::getPropertyValue($object, $sourceFieldName);
0 ignored issues
show
Bug introduced by
Since getPropertyValue() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPropertyValue() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
23
            if ($propertyExists) {
24
                $mappedObject = static::setPropertyValue(
0 ignored issues
show
Bug introduced by
Since setPropertyValue() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of setPropertyValue() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
25
                    $mappedObject,
26
                    $targetFieldName,
27
                    $propertyValue
28
                );
29
            }
30
        }
31
        return $mappedObject;
32
    }
33
34
    private static function setPropertyValue($mappedObject, $targetFieldName, $propertyValue)
35
    {
36
        $fieldNameArr = explode('.', $targetFieldName);
37
        $fieldName = array_shift($fieldNameArr);
38
        if (count($fieldNameArr) !== 0) {
39
            if (!property_exists($mappedObject, $fieldName)) {
40
                $mappedObject->{$fieldName} = new \StdClass();
41
            }
42
            $mappedObject->{$fieldName} = static::setPropertyValue(
0 ignored issues
show
Bug introduced by
Since setPropertyValue() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of setPropertyValue() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
43
                $mappedObject->{$fieldName},
44
                implode('.', $fieldNameArr),
45
                $propertyValue
46
            );
47
            return $mappedObject;
48
        }
49
        $mappedObject->{$fieldName} = $propertyValue;
50
        return $mappedObject;
51
    }
52
53
    private static function getPropertyValue($object, $sourceFieldName)
54
    {
55
        $fieldNameArr = explode('.', $sourceFieldName);
56
        $fieldName = array_shift($fieldNameArr);
57
        if (!property_exists($object, $fieldName)) {
58
            return [
59
                false,
60
                null
61
            ];
62
        }
63
        if (count($fieldNameArr) === 0) {
64
            return [
65
                true,
66
                json_decode(json_encode($object->{$fieldName}))
67
            ];
68
        }
69
        return static::getPropertyValue($object->{$fieldName}, implode('.', $fieldNameArr));
0 ignored issues
show
Bug introduced by
Since getPropertyValue() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPropertyValue() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
70
    }
71
}
72