Completed
Push — master ( 1fe283...98fe5a )
by Alexey
02:01
created

UseStatements   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 0
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A hasStatement() 0 10 3
C getFullClassName() 0 22 7
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Reflection\ClassUseStatements;
6
7
use \ArrayObject;
8
9
/**
10
 * Class UseStatements
11
 * @package UsesReflection
12
 */
13
class UseStatements extends ArrayObject {
14
15
    /**
16
     * @param UseStatement $useStatement
17
     * @return UseStatements
18
     */
19 1
    public function add(UseStatement $useStatement): UseStatements {
20 1
        $this->append($useStatement);
21
22 1
        return $this;
23
    }
24
25
    /**
26
     * @param string $statement
27
     * @return bool
28
     */
29 1
    public function hasStatement(string $statement): bool {
30
        /** @var UseStatement $useStatement */
31 1
        foreach ($this as $useStatement) {
32 1
            if ($useStatement->isEqual($statement)) {
33 1
                return true;
34
            }
35
        }
36
37 1
        return false;
38
    }
39
40
    /**
41
     * @param string $class
42
     * @return string|null
43
     */
44 1
    public function getFullClassName(string $class): ?string {
45 1
        if ($class[0] === '\\') {
46 1
            return $class;
47
        }
48
49 1
        $classParts = explode('\\', $class);
50 1
        $passedNamespace = array_shift($classParts);
51
52
        /** @var UseStatement $useStatement */
53 1
        foreach ($this as $useStatement) {
54 1
            $useParts = explode('\\', $useStatement->getUse());
55 1
            $definedNamespace = array_pop($useParts);
56
57 1
            if ($definedNamespace === $class || $useStatement->getAlias() === $class) {
58 1
                return $useStatement->getUse();
59 1
            } elseif ($definedNamespace === $passedNamespace || $useStatement->getAlias() === $passedNamespace) {
60 1
                return $useStatement->getUse() . '\\' . implode('\\', $classParts);
61
            }
62
        }
63
64 1
        return null;
65
    }
66
67
}