Completed
Push — master ( 06117a...d54351 )
by Rafael
03:01
created

ObjectParser::parseValue()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 4.8196
cc 10
eloc 15
nc 24
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\Array2Object;
13
14
class ObjectParser implements ValueParserInterface
15
{
16
    /**
17
     * @inheritDoc
18
     */
19
    public function parseValue($value, $type, \ReflectionProperty $property, $object)
20
    {
21
        $className = null;
22
        $context = new \ReflectionClass($property->class);
23
24
        //use the type as class
25
        if (class_exists($type)) {
26
            $className = $type;
27
        }
28
29
        //try to get the class from use statements in the class file
30
        if ($className === null) {
31
            $classContent = file_get_contents($context->getFileName());
32
            preg_match("/use\s+([\w\\\]+$type);/", $classContent, $matches);
33
            if (isset($matches[1]) && class_exists($matches[1])) {
34
                $className = $matches[1];
35
            }
36
        }
37
38
        //use the same namespace as class container
39
        if ($className === null && class_exists($context->getNamespaceName() . "\\" . $type)) {
40
            $className = $context->getNamespaceName() . "\\" . $type;
41
        }
42
43
        if (is_array($value) && $className !== null && class_exists($className)) {
44
            return Array2Object::createObject($className, $value);
45
        }
46
47
        return $value;
48
    }
49
}