Passed
Push — master ( 66351d...335aef )
by Hirofumi
20:01
created

GatewayRegistry   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 1
dl 60
loc 60
ccs 11
cts 18
cp 0.6111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A instance() 8 8 2
A set() 4 4 1
A setAll() 4 4 1
A get() 14 14 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
declare(strict_types=1);
3
4
namespace Shippinno\Notification\Domain\Model;
5
6
use InvalidArgumentException;
7
8 View Code Duplication
class GatewayRegistry
1 ignored issue
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...
9
{
10
    /**
11
     * @var null|GatewayRegistry
12
     */
13
    private static $instance = null;
14
15
    /**
16
     * @var Gateway[]
17
     */
18
    protected $gateways = [];
19
20
    /**
21
     * @return GatewayRegistry
22
     */
23
    public static function instance(): GatewayRegistry
24
    {
25
        if (null === static::$instance) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
26
            static::$instance = new GatewayRegistry;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
27
        }
28
29
        return static::$instance;
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $instance to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
30
    }
31
32
    /**
33
     * @param string $destinationType
34
     * @param Gateway $gateway
35
     */
36 4
    public function set(string $destinationType, Gateway $gateway): void
37
    {
38 4
        $this->gateways[$destinationType] = $gateway;
39 4
    }
40
41
    /**
42
     * @param array $gateways
43
     */
44
    public function setAll(array $gateways): void
45
    {
46
        $this->gateways = $gateways;
47
    }
48
49
    /**
50
     * @param Destination $destination
51
     * @return Gateway
52
     */
53 5
    public function get(Destination $destination): Gateway
54
    {
55 5
        $destinationType = $destination::type();
56 5
        if (!isset($this->gateways[$destinationType])) {
57 1
            throw new InvalidArgumentException(
58 1
                sprintf(
59 1
                    'No gateway for destination type (%s) is supported.',
60 1
                    $destinationType
61
                )
62
            );
63
        }
64
65 4
        return $this->gateways[$destinationType];
66
    }
67
}
68