CompositeSearch   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 26.98 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 34
loc 126
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A search() 17 17 3
A searchAndDecorate() 17 17 3
A searchFully() 0 13 2
A getSearchers() 0 4 1
A addSearcher() 0 14 4
A addSearchers() 0 8 2

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
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @author     Nico Schneider <[email protected]>
16
 * @copyright  2015 Christian Schiffler <[email protected]>
17
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
18
 * @link       https://github.com/tenside/core
19
 * @filesource
20
 */
21
22
namespace Tenside\Core\Composer\Search;
23
24
/**
25
 * Class CompositeSearch
26
 *
27
 * @package Tenside\Composer
28
 */
29
class CompositeSearch extends AbstractSearch
30
{
31
    /**
32
     * The list of search providers.
33
     *
34
     * @var SearchInterface[]
35
     */
36
    protected $searchers;
37
38
    /**
39
     * Create a new instance.
40
     *
41
     * @param array $searchers The list of search providers.
42
     */
43
    public function __construct(array $searchers)
44
    {
45
        $this->addSearchers($searchers);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function search($keywords, $filters = [])
0 ignored issues
show
Duplication introduced by
This method 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...
52
    {
53
        $results = [];
54
55
        foreach ($this->getSearchers() as $searcher) {
56
            $results = array_merge(
57
                $results,
58
                $searcher->search($keywords, $filters)
59
            );
60
61
            if (count($results) >= $this->getSatisfactionThreshold()) {
62
                return array_slice($results, 0, $this->getSatisfactionThreshold());
63
            }
64
        }
65
66
        return $results;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 View Code Duplication
    public function searchAndDecorate($keywords, $filters = [])
0 ignored issues
show
Duplication introduced by
This method 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...
73
    {
74
        $results = [];
75
76
        foreach ($this->getSearchers() as $searcher) {
77
            $results = array_merge(
78
                $results,
79
                $searcher->searchAndDecorate($keywords, $filters)
80
            );
81
82
            if (count($results) >= $this->getSatisfactionThreshold()) {
83
                return array_slice($results, 0, $this->getSatisfactionThreshold());
84
            }
85
        }
86
87
        return $results;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function searchFully($keywords, $filters = [])
94
    {
95
        $results = [];
96
97
        foreach ($this->getSearchers() as $searcher) {
98
            $results = array_merge(
99
                $results,
100
                $searcher->search($keywords, $filters)
101
            );
102
        }
103
104
        return $results;
105
    }
106
107
    /**
108
     * Retrieve the list of search providers.
109
     *
110
     * @return SearchInterface[]
111
     */
112
    public function getSearchers()
113
    {
114
        return $this->searchers;
115
    }
116
117
    /**
118
     * Add a search providers,
119
     *
120
     * @param SearchInterface $searcher The provider to add.
121
     *
122
     * @return $this
123
     */
124
    public function addSearcher(SearchInterface $searcher)
125
    {
126
        if ($searcher instanceof self && count($searcher->getSearchers())) {
127
            foreach ($searcher->getSearchers() as $compositeSearcher) {
128
                $this->addSearcher($compositeSearcher);
129
            }
130
131
            return $this;
132
        }
133
134
        $this->searchers[] = $searcher;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Add the passed search providers to the own list.
141
     *
142
     * @param array $searchers The providers to add.
143
     *
144
     * @return $this
145
     */
146
    public function addSearchers(array $searchers)
147
    {
148
        foreach ($searchers as $searcher) {
149
            $this->addSearcher($searcher);
150
        }
151
152
        return $this;
153
    }
154
}
155