Test Setup Failed
Push — master ( a54369...c473ba )
by Delete
02:40
created

AdapterFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 97.65 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 83
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 13 13 3
A setAdapterClasses() 18 18 3
B getInstanceByClassName() 22 23 4

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 Crossjoin\Browscap\Parser\Sqlite\Adapter;
5
6
use Crossjoin\Browscap\Exception\BrowscapException;
7
use Crossjoin\Browscap\Exception\InvalidArgumentException;
8
use Crossjoin\Browscap\Exception\ParserConditionNotSatisfiedException;
9
use Crossjoin\Browscap\Exception\UnexpectedValueException;
10
11
/**
12
 * Class AdapterFactory
13
 *
14
 * @package Crossjoin\Browscap\Parser\Sqlite\Adapter
15
 * @author Christoph Ziegenberg <[email protected]>
16
 * @link https://github.com/crossjoin/browscap
17
 */
18 View Code Duplication
class AdapterFactory
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...
19
{
20
    const DEFAULT_CLASSES = [
21
        '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Pdo',
22
        '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Sqlite3',
23
    ];
24
25
    protected static $adapterClasses = self::DEFAULT_CLASSES;
26
27
    /**
28
     * @param string $fileName
29
     *
30
     * @return AdapterInterface
31
     * @throws ParserConditionNotSatisfiedException
32
     * @throws UnexpectedValueException
33
     */
34
    public static function getInstance(string $fileName) : AdapterInterface
35
    {
36
        foreach (static::$adapterClasses as $className) {
37
            $instance = static::getInstanceByClassName($className, $fileName);
38
            if ($instance !== null) {
39
                return $instance;
40
            }
41
        }
42
43
        throw new ParserConditionNotSatisfiedException(
44
            "No Sqlite extension found. Either 'pdo_sqlite' or 'sqlite3' extension required."
45
        );
46
    }
47
48
    /**
49
     * @param array $fullyQualifiedClassNames
50
     *
51
     * @throws InvalidArgumentException
52
     */
53
    public static function setAdapterClasses(array $fullyQualifiedClassNames)
54
    {
55
        $rememberClasses = self::$adapterClasses;
56
57
        self::$adapterClasses = [];
58
        foreach ($fullyQualifiedClassNames as $className) {
59
            if (is_string($className)) {
60
                self::$adapterClasses[] = $className;
61
            } else {
62
                // Reset to previous value on error
63
                self::$adapterClasses = $rememberClasses;
64
65
                throw new InvalidArgumentException(
66
                    "A value in the class name array is of type '" . gettype($className) . "'. String expected."
67
                );
68
            }
69
        }
70
    }
71
72
    /**
73
     * @param string $className
74
     * @param string $fileName
75
     *
76
     * @return AdapterInterface|null
77
     * @throws UnexpectedValueException
78
     */
79
    protected static function getInstanceByClassName(string $className, string $fileName)
80
    {
81
        if (class_exists($className)) {
82
            $interface = '\Crossjoin\Browscap\Parser\Sqlite\Adapter\AdapterFactoryInterface';
83
            $interfaces = class_implements($className);
84
            if (array_key_exists(ltrim($interface, '\\'), $interfaces)) {
85
                try {
86
                    return new $className($fileName);
87
                } catch (BrowscapException $e) {
88
                    // Ignore exception, because we just return NULL on failure
89
                }
90
            } else {
91
                throw new UnexpectedValueException(
92
                    "Class '$className' has to implement the interface '$interface'.",
93
                    1459000689
94
                );
95
            }
96
        } else {
97
            throw new UnexpectedValueException("Class '$className' doesn't exist.", 1459000690);
98
        }
99
100
        return null;
101
    }
102
}
103