Completed
Push — master ( ac596a...35f4cd )
by Randy
03:49
created

ClassMapper::lookUpClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Dgame\Soap\Hydrator;
4
5
/**
6
 * Class ClassMapper
7
 * @package Dgame\Soap\Hydrator
8
 */
9
final class ClassMapper
10
{
11
    /**
12
     * @var array
13
     */
14
    private $classmap = [];
15
    /**
16
     * @var array
17
     */
18
    private $pattern = [];
19
20
    /**
21
     * AutoLoad constructor.
22
     *
23
     * @param array $classmap
24
     */
25 6
    public function __construct(array $classmap = [])
26
    {
27 6
        $this->classmap = $classmap;
28 6
    }
29
30
    /**
31
     * @param string $name
32
     * @param string $class
33
     */
34
    public function appendClass(string $name, string $class)
35
    {
36
        $this->classmap[$name] = $class;
37
    }
38
39
    /**
40
     * @param string $pattern
41
     * @param string $class
42
     */
43 2
    public function appendPattern(string $pattern, string $class)
44
    {
45 2
        $this->pattern[$pattern] = $class;
46 2
    }
47
48
    /**
49
     * @param string $class
50
     *
51
     * @return null|string
52
     */
53 6
    private function findClassName(string $class)
54
    {
55 6
        $name = $this->getClassName($class);
56 6
        if ($name !== null) {
57 6
            return $name;
58
        }
59
60 6
        foreach ($this->pattern as $pattern => $name) {
61 2
            if (preg_match($pattern, $class) === 1) {
62 2
                return $this->getClassName($name);
63
            }
64
        }
65
66 6
        return null;
67
    }
68
69
    /**
70
     * @param string $class
71
     *
72
     * @return string
73
     */
74 6
    private function lookUpClassName(string $class): string
75
    {
76 6
        return array_key_exists($class, $this->classmap) ? $this->classmap[$class] : $class;
77
    }
78
79
    /**
80
     * @param string $class
81
     *
82
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
83
     */
84 6
    private function getClassName(string $class)
85
    {
86 6
        $class = $this->lookUpClassName(ucfirst($class));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $class. This often makes code more readable.
Loading history...
87
88 6
        return class_exists($class) ? $class : null;
89
    }
90
91
    /**
92
     * @param string $class
93
     *
94
     * @return HydratableInterface|null
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
95
     */
96 6
    public function getInstanceOf(string $class)
97
    {
98 6
        if (($class = $this->findClassName($class)) !== null) {
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $class. This often makes code more readable.
Loading history...
99 6
            return new $class();
100
        }
101
102 6
        return null;
103
    }
104
}