GuardFactory::create()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 50
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 45
nc 13
nop 3
dl 0
loc 50
ccs 33
cts 33
cp 1
crap 13
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace InputGuard\Guards;
5
6
use InputGuard\Configuration;
7
8
/**
9
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
 */
11
class GuardFactory
12
{
13
    /**
14
     * @var Configuration
15
     */
16
    private $configuration;
17
18 43
    public function __construct(Configuration $configuration)
19
    {
20 43
        $this->configuration = $configuration;
21 43
    }
22
23
    /**
24
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
25
     *
26
     * @param string $class
27
     * @param mixed  $input
28
     * @param array  $extra
29
     *
30
     * @return Guard
31
     */
32 38
    public function create(string $class, $input, array $extra): Guard
33
    {
34 38
        switch ($class) {
35
            case BoolGuard::class:
36 3
                $guard = new BoolGuard($input, $this->defaultValue(BoolGuard::class));
37 3
                break;
38
            case IntGuard::class:
39 8
                $guard = new IntGuard($input, $this->defaultValue(IntGuard::class));
40 8
                break;
41
            case FloatGuard::class:
42 4
                $guard = new FloatGuard($input, $this->defaultValue(FloatGuard::class));
43 4
                break;
44
            case InstanceOfGuard::class:
45 3
                $guard = new InstanceOfGuard($input, $extra[0], $this->defaultValue(InstanceOfGuard::class));
46 3
                break;
47
            case StringGuard::class:
48 4
                $guard = new StringGuard($input, $this->defaultValue(StringGuard::class));
49 4
                break;
50
            case StringableGuard::class:
51 3
                $guard = new StringableGuard($input, $this->defaultValue(StringableGuard::class));
52 3
                break;
53
            case IterableGuard::class:
54 4
                $guard = new IterableGuard($input, $this->defaultValue(IterableGuard::class));
55 4
                break;
56
            case IterableIntGuard::class:
57 3
                $guard = new IterableIntGuard($input, $this->defaultValue(IterableIntGuard::class));
58 3
                break;
59
            case IterableFloatGuard::class:
60 2
                $guard = new IterableFloatGuard($input, $this->defaultValue(IterableFloatGuard::class));
61 2
                break;
62
            case IterableStringGuard::class:
63 2
                $guard = new IterableStringGuard($input, $this->defaultValue(IterableStringGuard::class));
64 2
                break;
65
            case IterableStringableGuard::class:
66 2
                $guard = new IterableStringableGuard($input, $this->defaultValue(IterableStringableGuard::class));
67 2
                break;
68
            case InListGuard::class:
69 2
                $guard = new InListGuard(
70 2
                    $input,
71 2
                    $extra[0],
72 2
                    $this->defaultValue(InListGuard::class),
73 2
                    $this->defaultStrict(InListGuard::class)
74
                );
75 2
                break;
76
            default:
77 1
                $guard = new FailAlwaysGuard($this->defaultValue(FailAlwaysGuard::class));
78 1
                break;
79
        }
80
81 38
        return $guard;
82
    }
83
84 38
    private function defaultValue(string $class)
85
    {
86 38
        return $this->configuration->defaultValue($class);
87
    }
88
89 2
    private function defaultStrict(string $class): bool
90
    {
91 2
        return $this->configuration->defaultStrict($class);
92
    }
93
}
94