Completed
Push — ezp_30981_tree_walking ( b1bcb5 )
by
unknown
13:48
created

Location::getAncestors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\Core\Repository\Values\Content\Location class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Values\Content;
10
11
use Closure;
12
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
13
use eZ\Publish\API\Repository\Values\Content\LocationList;
14
use eZ\Publish\Core\Repository\LocationListFactory\ChildrenLazySearchQuery;
15
use eZ\Publish\Core\Repository\TreeAccessor\TreeDelegate;
16
17
/**
18
 * This class represents a location in the repository.
19
 *
20
 * @internal Meant for internal use by Repository, type hint against API object instead.
21
 */
22
class Location extends APILocation
23
{
24
    /**
25
     * Content info of the content object of this location.
26
     *
27
     * @var \eZ\Publish\API\Repository\Values\Content\ContentInfo
28
     */
29
    protected $contentInfo;
30
31
    /** @var array */
32
    protected $path;
33
34
    /** @var \eZ\Publish\Core\Repository\TreeAccessor\TreeDelegate */
35
    private $treeDelegate;
36
37
    /**
38
     * Returns the content info of the content object of this location.
39
     *
40
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
41
     */
42
    public function getContentInfo()
43
    {
44
        return $this->contentInfo;
45
    }
46
47
    /**
48
     * Function where list of properties are returned.
49
     *
50
     * Override to add dynamic properties
51
     *
52
     * @uses \parent::getProperties()
53
     *
54
     * @param array $dynamicProperties
55
     *
56
     * @return array
57
     */
58
    protected function getProperties($dynamicProperties = ['contentId'])
59
    {
60
        return parent::getProperties($dynamicProperties);
61
    }
62
63
    /**
64
     * Magic getter for retrieving convenience properties.
65
     *
66
     * @param string $property The name of the property to retrieve
67
     *
68
     * @return mixed
69
     */
70
    public function __get($property)
71
    {
72
        switch ($property) {
73
            case 'contentId':
74
                return $this->contentInfo->id;
75
            case 'path':
76
                if ($this->path !== null) {
77
                    return $this->path;
78
                }
79
                if (isset($this->pathString[1]) && $this->pathString[0] === '/') {
80
                    return $this->path = explode('/', trim($this->pathString, '/'));
81
                }
82
83
                return $this->path = [];
84
        }
85
86
        return parent::__get($property);
87
    }
88
89
    /**
90
     * Magic isset for signaling existence of convenience properties.
91
     *
92
     * @param string $property
93
     *
94
     * @return bool
95
     */
96
    public function __isset($property)
97
    {
98
        if ($property === 'contentId' || $property === 'path') {
99
            return true;
100
        }
101
102
        return parent::__isset($property);
103
    }
104
105
    public function getChildren(): iterable
106
    {
107
        return $this->treeDelegate->getChildren($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->treeDelegate->getChildren($this); (eZ\Publish\Core\Reposito...actory\LazySearchResult) is incompatible with the return type of the parent method eZ\Publish\API\Repositor...t\Location::getChildren of type array.

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...
108
    }
109
110
    public function getSiblings(): iterable
111
    {
112
        return $this->treeDelegate->getSiblings($this);
113
    }
114
115
    public function getAncestors(): iterable
116
    {
117
        return $this->treeDelegate->getAncestors($this);
118
    }
119
}
120