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

TreeDelegate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 31.11 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 14
loc 45
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAncestors() 7 7 1
A getSiblings() 0 12 1
A getChildren() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace eZ\Publish\Core\Repository\TreeAccessor;
6
7
use eZ\Publish\API\Repository\SearchService;
8
use eZ\Publish\API\Repository\Values\Content\Language;
9
use eZ\Publish\API\Repository\Values\Content\Location;
10
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
11
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Ancestor;
12
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LocationId;
13
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd;
14
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalNot;
15
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ParentLocationId;
16
use eZ\Publish\Core\Repository\LocationListFactory\LazySearchResult;
17
18
/**
19
 * @internal
20
 */
21
final class TreeDelegate
22
{
23
    /** @var \eZ\Publish\API\Repository\SearchService */
24
    private $searchService;
25
26
    /** @var array */
27
    private $languageFilter;
28
29
    public function __construct(SearchService $searchService, array $prioritizedLanguages = Language::ALL)
30
    {
31
        $this->searchService = $searchService;
32
        $this->languageFilter =             [
33
            'prioritizedLanguages' => $prioritizedLanguages
34
        ];
35
    }
36
37 View Code Duplication
    public function getAncestors(Location $location): iterable
38
    {
39
        $query = new LocationQuery();
40
        $query->filter = new Ancestor($location->pathString);
41
42
        return new LazySearchResult($this->searchService, $query, $this->languageFilter);
43
    }
44
45
    public function getSiblings(Location $location): iterable
46
    {
47
        $query = new LocationQuery();
48
        $query->filter = new LogicalAnd([
49
            new ParentLocationId($location->parentLocationId),
50
            new LogicalNot(
51
                new LocationId($location->id)
52
            ),
53
        ]);
54
55
        return new LazySearchResult($this->searchService, $query, $this->languageFilter);
56
    }
57
58 View Code Duplication
    public function getChildren(Location $location): iterable
59
    {
60
        $query = new LocationQuery();
61
        $query->filter = new ParentLocationId($location->id);
62
63
        return new LazySearchResult($this->searchService, $query, $this->languageFilter);
64
    }
65
}
66