Completed
Pull Request — 1.0 (#6)
by David
04:47
created

DefinitionConverter::convertReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TheCodingMachine\Yaco;
4
5
use Interop\Container\Definition\AliasDefinitionInterface;
6
use Interop\Container\Definition\DefinitionInterface;
7
use Interop\Container\Definition\FactoryCallDefinitionInterface;
8
use Interop\Container\Definition\ObjectDefinitionInterface;
9
use Interop\Container\Definition\ParameterDefinitionInterface;
10
use Interop\Container\Definition\ReferenceDefinitionInterface;
11
use TheCodingMachine\Yaco\Definition\AliasDefinition;
12
use TheCodingMachine\Yaco\Definition\FactoryCallDefinition;
13
use TheCodingMachine\Yaco\Definition\ObjectDefinition;
14
use TheCodingMachine\Yaco\Definition\ParameterDefinition;
15
use TheCodingMachine\Yaco\Definition\Reference;
16
17
/**
18
 * This class is in charge of converting definitions from the Interop\Container\DefinitionInterface to the
19
 * internal DumpableInterface format.
20
 */
21
class DefinitionConverter implements DefinitionConverterInterface
22
{
23
    /**
24
     * @param string $identifier
25
     * @param DefinitionInterface $definition
26
     * @return AliasDefinition|FactoryCallDefinition|ObjectDefinition|ParameterDefinition
27
     */
28
    public function convert($identifier, DefinitionInterface $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, $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
        } else {
58
            throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition)));
59
        }
60
    }
61
62
    /**
63
     * @param array $arguments
64
     *
65
     * @return array
66
     */
67
    private function convertArguments(array $arguments)
68
    {
69
        $yacoArguments = [];
70
        foreach ($arguments as $argument) {
71
            $yacoArguments[] = $this->convertValue($argument);
72
        }
73
74
        return $yacoArguments;
75
    }
76
77
    private function convertValue($value)
78
    {
79
        if (is_array($value)) {
80
            return array_map([$this, 'convertValue'], $value);
81
        } elseif ($value instanceof DefinitionInterface) {
82
            return $this->convert(null, $value);
83
        } elseif (is_object($value) || is_resource($value)) {
84
            throw new \RuntimeException('Unable to convert a definition. Parameters cannot be an object or a resource.');
85
        } else {
86
            return $value;
87
        }
88
    }
89
}
90