Completed
Push — master ( 1f3fa8...7d4b61 )
by Mike
03:08
created

ProviderChain   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 56
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 16
loc 56
rs 10
c 2
b 1
f 1
wmc 10
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B addProviders() 16 16 6
A __call() 0 10 3

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
namespace Sugarcrm\UpgradeSpec\Data;
4
5
use Sugarcrm\UpgradeSpec\Data\Provider\ProviderInterface;
6
7
class ProviderChain
8
{
9
    /**
10
     * @var array
11
     */
12
    private $providers = [];
13
14
    /**
15
     * ProviderChain constructor.
16
     *
17
     * @param mixed $providers
18
     */
19
    public function __construct($providers)
20
    {
21
        $this->addProviders($providers);
22
    }
23
24
    /**
25
     * Adds providers to the chain
26
     *
27
     * @param mixed $providers
28
     */
29 View Code Duplication
    public function addProviders($providers)
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...
30
    {
31
        if (!is_array($providers) && !$providers instanceof \Traversable) {
32
            throw new \InvalidArgumentException(sprintf('Argument is not traversable: %s', $providers));
33
        }
34
35
        $providers = is_array($providers) ? $providers : iterator_to_array($providers);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $providers. This often makes code more readable.
Loading history...
36
37
        foreach ($providers as $provider) {
38
            if (!is_a($provider, ProviderInterface::class)) {
39
                throw new \InvalidArgumentException('ProviderChain expects ProviderInterface[]');
40
            }
41
        }
42
43
        $this->providers = array_merge($this->providers, $providers);
44
    }
45
46
    /**
47
     * @param $name
48
     * @param $arguments
49
     *
50
     * @return mixed
51
     */
52
    public function __call($name, $arguments)
53
    {
54
        foreach ($this->providers as $provider) {
55
            if (method_exists($provider, $name)) {
56
                return call_user_func_array([$provider, $name], $arguments);
57
            }
58
        }
59
60
        throw new \RuntimeException(sprintf('There is no provider with method: %s', $name));
61
    }
62
}
63