ConnectorFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 3
loc 54
c 0
b 0
f 0
wmc 8
lcom 0
cbo 2
ccs 16
cts 20
cp 0.8
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 3
B validate() 3 20 5

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 AsyncPHP\Icicle\Database;
4
5
use AsyncPHP\Icicle\Database\Connector\BlockingConnector;
6
use AsyncPHP\Icicle\Database\Connector\DoormanConnector;
7
use InvalidArgumentException;
8
9
final class ConnectorFactory
10
{
11
    /**
12
     * @param array $config
13
     *
14
     * @return Connector
15
     *
16
     * @throws InvalidArgumentException
17
     */
18 2
    public function create(array $config)
19
    {
20 2
        $config = $this->validate($config);
21
22 2
        if ($config["connector"] === "doorman") {
23 1
            $connector = new DoormanConnector();
24 1
        }
25
26 2
        if ($config["connector"] === "blocking") {
27 1
            $connector = new BlockingConnector();
28 1
        }
29
30 2
        $connector->connect($config);
0 ignored issues
show
Bug introduced by
The variable $connector does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
31
32 2
        return $connector;
33
    }
34
35
    /**
36
     * @param array $config
37
     *
38
     * @return array
39
     *
40
     * @throws InvalidArgumentException
41
     */
42 2
    private function validate(array $config)
43
    {
44 2
        if (!isset($config["driver"])) {
45
            throw new InvalidArgumentException("Undefined driver");
46
        }
47
48 2 View Code Duplication
        if (!in_array($config["driver"], ["sqlsrv", "mysql", "pgsql", "sqlite"])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
            throw new InvalidArgumentException("Unrecognised driver");
50
        }
51
52 2
        if (!isset($config["connector"])) {
53
            throw new InvalidArgumentException("Undefined connector");
54
        }
55
56 2
        if (!in_array($config["connector"], ["doorman", "blocking"])) {
57
            throw new InvalidArgumentException("Unrecognised connector");
58
        }
59
60 2
        return $config;
61
    }
62
}
63