Passed
Push — master ( 4ed61c...14fd45 )
by Jakub
57s queued 13s
created

ClassName::getParents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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