DefinitionConverter::convert()   D
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 35
rs 4.8196
cc 10
eloc 26
nc 10
nop 2

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
namespace TheCodingMachine\Yaco;
4
5
use Interop\Container\Definition\DefinitionInterface;
6
use Interop\Container\Definition\FactoryCallDefinitionInterface;
7
use Interop\Container\Definition\ObjectDefinitionInterface;
8
use Interop\Container\Definition\ParameterDefinitionInterface;
9
use Interop\Container\Definition\ReferenceDefinitionInterface;
10
use TheCodingMachine\Yaco\Definition\AliasDefinition;
11
use TheCodingMachine\Yaco\Definition\FactoryCallDefinition;
12
use TheCodingMachine\Yaco\Definition\ObjectDefinition;
13
use TheCodingMachine\Yaco\Definition\ParameterDefinition;
14
use TheCodingMachine\Yaco\Definition\Reference;
15
16
/**
17
 * This class is in charge of converting definitions from the Interop\Container\DefinitionInterface to the
18
 * internal DumpableInterface format.
19
 */
20
class DefinitionConverter implements DefinitionConverterInterface
21
{
22
    /**
23
     * @param string                    $identifier
24
     * @param DefinitionInterface|mixed $definition
25
     *
26
     * @return AliasDefinition|FactoryCallDefinition|ObjectDefinition|ParameterDefinition
27
     */
28
    public function convert($identifier, $definition)
29
    {
30
        if ($definition instanceof ObjectDefinitionInterface) {
31
            $yacoObjectDefinition = new ObjectDefinition($identifier,
32
                $definition->getClassName(),
33
                $this->convertArguments($definition->getConstructorArguments()));
34
35
            foreach ($definition->getPropertyAssignments() as $assignment) {
36
                $yacoObjectDefinition->setProperty($assignment->getPropertyName(), $this->convertValue($assignment->getValue()));
37
            }
38
39
            foreach ($definition->getMethodCalls() as $methodCall) {
40
                $yacoObjectDefinition->addMethodCall($methodCall->getMethodName(), $this->convertArguments($methodCall->getArguments()));
41
            }
42
43
            return $yacoObjectDefinition;
44
        } elseif ($definition instanceof FactoryCallDefinitionInterface) {
45
            return new FactoryCallDefinition($identifier,
46
                $this->convertValue($definition->getFactory()),
47
                $definition->getMethodName(),
48
                $this->convertArguments($definition->getArguments()));
49
        } elseif ($definition instanceof ParameterDefinitionInterface) {
50
            return new ParameterDefinition($identifier, $this->convertValue($definition->getValue()));
51
        } elseif ($definition instanceof ReferenceDefinitionInterface) {
52
            if ($identifier !== null) {
53
                return new AliasDefinition($identifier, $definition->getTarget());
54
            } else {
55
                return new Reference($definition->getTarget());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \TheCodingMac...finition->getTarget()); (TheCodingMachine\Yaco\Definition\Reference) is incompatible with the return type declared by the interface TheCodingMachine\Yaco\De...erterInterface::convert of type TheCodingMachine\Yaco\Definition\DumpableInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
            }
57
        } elseif (is_scalar($definition) || is_array($definition)) {
58
            return new ParameterDefinition($identifier, $this->convertValue($definition));
59
        } else {
60
            throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition)));
61
        }
62
    }
63
64
    /**
65
     * @param array $arguments
66
     *
67
     * @return array
68
     */
69
    private function convertArguments(array $arguments)
70
    {
71
        $yacoArguments = [];
72
        foreach ($arguments as $argument) {
73
            $yacoArguments[] = $this->convertValue($argument);
74
        }
75
76
        return $yacoArguments;
77
    }
78
79
    private function convertValue($value)
80
    {
81
        if (is_array($value)) {
82
            return array_map([$this, 'convertValue'], $value);
83
        } elseif ($value instanceof DefinitionInterface) {
84
            return $this->convert(null, $value);
85
        } elseif (is_object($value) || is_resource($value)) {
86
            throw new \RuntimeException('Unable to convert a definition. Parameters cannot be an object or a resource.');
87
        } else {
88
            return $value;
89
        }
90
    }
91
}
92