ArraySetType::setInnerType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Infrastructure\Collections\Doctrine\ODM\MongoDB\Types;
12
13
use Cubiche\Core\Collections\ArrayCollection\ArraySet;
14
use Cubiche\Core\Collections\ArrayCollection\ArraySetInterface;
15
use Doctrine\ODM\MongoDB\Types\CollectionType;
16
use Doctrine\ODM\MongoDB\Types\Type;
17
18
/**
19
 * Array Set Type Class.
20
 *
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 * @author Ivannis Suárez Jerez <[email protected]>
23
 */
24 View Code Duplication
class ArraySetType extends CollectionType
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $innerType = '';
30
31
    /**
32
     * @return string
33
     */
34
    protected function innerType()
35
    {
36
        return $this->innerType;
37
    }
38
39
    /**
40
     * @param string $innerType
41
     */
42
    public function setInnerType($innerType)
43
    {
44
        $this->innerType = $innerType;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function convertToDatabaseValue($value)
51
    {
52
        if ($value !== null && $value instanceof ArraySetInterface) {
53
            $items = array();
54
            $type = Type::getType($this->innerType);
55
            foreach ($value as $item) {
56
                $items[] = $type->convertToDatabaseValue($item);
57
            }
58
            $value = $items;
59
        }
60
61
        return parent::convertToDatabaseValue($value);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function convertToPHPValue($value)
68
    {
69
        if ($value === null) {
70
            return new ArraySet();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cubiche\Core...yCollection\ArraySet(); (Cubiche\Core\Collections\ArrayCollection\ArraySet) is incompatible with the return type of the parent method Doctrine\ODM\MongoDB\Typ...Type::convertToPHPValue of type array|null.

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...
71
        }
72
        if (is_array($value) || $value instanceof \Traversable) {
73
            $items = array();
74
            $type = Type::getType($this->innerType);
75
            foreach ($value as $item) {
76
                $items[] = $type->convertToPHPValue($item);
77
            }
78
79
            return new ArraySet($items);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cubiche\Core...ction\ArraySet($items); (Cubiche\Core\Collections\ArrayCollection\ArraySet) is incompatible with the return type of the parent method Doctrine\ODM\MongoDB\Typ...Type::convertToPHPValue of type array|null.

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...
80
        }
81
82
        return parent::convertToPHPValue($value);
83
    }
84
}
85