Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

ObjectParser::toObjectValue()   C

Complexity

Conditions 11
Paths 36

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 5.2653
cc 11
eloc 16
nc 36
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object\Parser;
11
12
use Rafrsr\LibArray2Object\AbstractContext;
13
use Rafrsr\LibArray2Object\Array2Object;
14
use Rafrsr\LibArray2Object\Array2ObjectContext;
15
use Rafrsr\LibArray2Object\Object2Array;
16
use Rafrsr\LibArray2Object\Object2ArrayContext;
17
18
class ObjectParser implements ValueParserInterface
19
{
20
    const NAME = 'object';
21
22
    /** @var Array2Object */
23
    protected $array2Object;
24
    /** @var Object2Array */
25
    protected $object2Array;
26
27
    /**
28
     * ObjectParser constructor.
29
     *
30
     * @param AbstractContext $context
31
     */
32
    public function __construct(AbstractContext $context)
33
    {
34
        if ($context instanceof Array2ObjectContext) {
35
            $this->array2Object = new Array2Object($context);
36
        } elseif ($context instanceof Object2ArrayContext) {
37
            $this->object2Array = new Object2Array($context);
38
        }
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function getName()
45
    {
46
        return self::NAME;
47
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52
    public function toObjectValue($value, $type, \ReflectionProperty $property, $object)
53
    {
54
        $className = null;
55
        $context = new \ReflectionClass($property->class);
56
57
        //use the type as class
58
        if (class_exists($type)) {
59
            $className = $type;
60
        }
61
62
        //try to get the class from use statements in the class file
63
        if ($className === null) {
64
            $classContent = file_get_contents($context->getFileName());
65
            preg_match("/use\s+([\w\\\]+$type);/", $classContent, $matches);
66
            if (isset($matches[1]) && class_exists($matches[1])) {
67
                $className = $matches[1];
68
            }
69
        }
70
71
        //use the same namespace as class container
72
        if ($className === null && class_exists($context->getNamespaceName() . "\\" . $type)) {
73
            $className = $context->getNamespaceName() . "\\" . $type;
74
        }
75
76
        if (is_array($value) && $className !== null && class_exists($className)) {
77
            if ($this->array2Object) {
78
                return $this->array2Object->createObject($className, $value);
79
            }
80
        }
81
82
        return $value;
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function toArrayValue($value, $type, \ReflectionProperty $property, $object)
89
    {
90
        if (is_object($value)) {
91
            return $this->object2Array->createArray($value);
92
        }
93
94
        return $value;
95
    }
96
}