ClassName   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 16
dl 0
loc 47
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A mustImplement() 0 4 2
A equals() 0 3 1
A __toString() 0 3 1
A getParents() 0 9 2
A mustHaveMethod() 0 4 2
1
<?php declare(strict_types=1);
2
3
namespace Comquer\Reflection\ClassName;
4
5
use ReflectionClass;
6
7
class ClassName
8
{
9
    /** @var ReflectionClass e*/
10
    private $classReflection;
11
12
    public function __construct(string $className)
13
    {
14
        try {
15
            $this->classReflection = new ReflectionClass($className);
16
        } catch (\ReflectionException $exception) {
17
            throw ClassNameException::classDoesNotExist($className);
18
        }
19
    }
20
21
    public function getParents() : ClassNameCollection
22
    {
23
        $parents = new ClassNameCollection();
24
25
        foreach (class_parents((string) $this) as $parent) {
26
            $parents->add(new self($parent));
27
        }
28
29
        return $parents;
30
    }
31
32
    public function mustHaveMethod(string $methodName) : void
33
    {
34
        if ($this->classReflection->hasMethod($methodName) === false) {
35
            throw ClassNameException::missingMethod((string) $this, $methodName);
36
        }
37
    }
38
39
    public function mustImplement(string $interfaceName) : void
40
    {
41
        if ($this->classReflection->implementsInterface($interfaceName) === false) {
42
            throw ClassNameException::missingImplementation((string) $this->classReflection, $interfaceName);
43
        }
44
    }
45
46
    public function equals(self $namespace) : bool
47
    {
48
        return (string) $this === (string) $namespace;
49
    }
50
51
    public function __toString() : string
52
    {
53
        return $this->classReflection->getName();
54
    }
55
}
56