XrefResolverFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 26.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 51.85%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 20
loc 75
ccs 14
cts 27
cp 0.5185
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerKnownTypes() 0 9 2
A getByType() 8 8 2
A isRegistered() 0 4 1
A internalRegister() 0 4 1
A register() 12 12 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
namespace ConfigToken\TreeCompiler\XrefResolver;
4
5
use ConfigToken\Exception\AlreadyRegisteredException;
6
use ConfigToken\TreeCompiler\XrefResolver\Types\InlineXrefResolver;
7
use ConfigToken\TreeCompiler\XrefResolver\Types\UrlXrefResolver;
8
use ConfigToken\TreeCompiler\XrefResolver\Types\LocalFileXrefResolver;
9
use ConfigToken\TreeCompiler\XrefResolver\Exception\UnknownXrefTypeException;
10
11
12
class XrefResolverFactory
13
{
14
    /** @var XrefResolverInterface[] */
15
    protected static $registeredByType = array();
16
17
    /**
18
     * Register all known resolvers.
19
     */
20 7
    protected static function registerKnownTypes()
21
    {
22 7
        if (!empty(static::$registeredByType)) {
23 7
            return;
24
        }
25 1
        static::internalRegister(new UrlXrefResolver());
26 1
        static::internalRegister(new LocalFileXrefResolver());
27 1
        static::internalRegister(new InlineXrefResolver());
28 1
    }
29
30
    /**
31
     * Get a registered resolver for the given type.
32
     *
33
     * @param string $xrefType
34
     * @return XrefResolverInterface
35
     *
36
     * @throws UnknownXrefTypeException
37
     */
38 7 View Code Duplication
    public static function getByType($xrefType)
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...
39
    {
40 7
        static::registerKnownTypes();
41 7
        if (isset(static::$registeredByType[$xrefType])) {
42 7
            return static::$registeredByType[$xrefType];
43
        }
44
        throw new UnknownXrefTypeException($xrefType);
45
    }
46
47
    /**
48
     * Check if the given resolver is registerd.
49
     *
50
     * @param XrefResolverInterface $xrefResolver
51
     * @return boolean
52
     */
53
    public static function isRegistered(XrefResolverInterface $xrefResolver)
54
    {
55
        return (isset(static::$registeredByType[$xrefResolver::getType()]));
56
    }
57
58
    /**
59
     * Used internally to register a new resolver.
60
     *
61
     * @param XrefResolverInterface $xrefResolver
62
     */
63 1
    protected static function internalRegister(XrefResolverInterface $xrefResolver)
64
    {
65 1
        static::$registeredByType[$xrefResolver::getType()] = $xrefResolver;
66 1
    }
67
68
    /**
69
     * Register a new resolver.
70
     *
71
     * @param XrefResolverInterface $xrefResolver
72
     * @throws AlreadyRegisteredException
73
     */
74 View Code Duplication
    public static function register(XrefResolverInterface $xrefResolver)
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...
75
    {
76
        if (static::isRegistered($xrefResolver)) {
77
            throw new AlreadyRegisteredException(
78
                sprintf(
79
                    'Xref resolver for Xref type %s is already registered.',
80
                    $xrefResolver::getType()
81
                )
82
            );
83
        }
84
        static::internalRegister($xrefResolver);
85
    }
86
}