Completed
Push — master ( f18951...94bfb9 )
by Randy
03:34
created

ClassMapper::new()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
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 Hydrate|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \self|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...
52
     */
53 6
    public function new(string $class)
54
    {
55 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...
56 6
            return Hydrate::new($class);
57
        }
58
59 6
        return null;
60
    }
61
62
    /**
63
     * @param string $class
64
     *
65
     * @return null|string
66
     */
67 6
    private function findClassName(string $class)
68
    {
69 6
        $name = $this->getClassName($class);
70 6
        if ($name !== null) {
71 6
            return $name;
72
        }
73
74 6
        foreach ($this->pattern as $pattern => $name) {
75 2
            if (preg_match($pattern, $class) === 1) {
76 2
                return $this->getClassName($name);
77
            }
78
        }
79
80 6
        return null;
81
    }
82
83
    /**
84
     * @param string $class
85
     *
86
     * @return null|string
87
     */
88 6
    private function getClassName(string $class)
89
    {
90 6
        $class = 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...
91 6
        $class = array_key_exists($class, $this->classmap) ? $this->classmap[$class] : $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...
92
93 6
        return class_exists($class) ? $class : null;
94
    }
95
}