Completed
Pull Request — master (#35)
by Aleh
03:05
created

Uses   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 67
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A find() 0 6 2
A findAlias() 0 7 2
A getFQCN() 0 3 1
A setFQCN() 0 3 1
A add() 0 7 2
A all() 0 3 1
A getReversed() 0 9 3
1
<?php
2
3
namespace Padawan\Domain\Project\Node;
4
5
use Padawan\Domain\Project\FQN;
6
use Padawan\Domain\Project\FQCN;
7
8
class Uses {
9
    private $map = [];
10
    private $reversed;
11
    private $fqcn;
12
13
    public function __construct(FQN $fqcn = null) {
14
        $this->fqcn = $fqcn;
15
    }
16
17
    /**
18
     * Finds FQCN in file uses. Returns false if there are no such class in uses
19
     *
20
     * @return FQCN|bool
21
     */
22
    public function find($alias) {
23
        if (array_key_exists($alias, $this->map)) {
24
            return $this->map[$alias];
25
        }
26
        return false;
27
    }
28
29
    /**
30
     * Finds alias for given FQCN.
31
     *
32
     * @return string
33
     */
34
    public function findAlias(FQCN $fqcn) {
35
        $rev = $this->getReversed();
36
        if (array_key_exists($fqcn->toString(), $rev)) {
37
            return $rev[$fqcn->toString()];
38
        }
39
        return $fqcn->toString();
40
    }
41
42
    public function getFQCN() {
43
        return $this->fqcn;
44
    }
45
46
    public function setFQCN(FQN $fqcn) {
47
        $this->fqcn = $fqcn;
48
    }
49
50
    /**
51
     * Adds FQCN to uses map
52
     */
53
    public function add(FQCN $fqcn, $alias = null) {
54
        if (!$alias) {
55
            $alias = $fqcn->getClassName();
56
        }
57
        $this->map[$alias] = $fqcn;
58
        $this->reversed = null;
59
    }
60
61
    public function all() {
62
        return $this->map;
63
    }
64
65
    protected function getReversed() {
66
        if ($this->reversed === null) {
67
            $this->reversed = [];
68
            foreach ($this->map AS $alias=>$fqcn) {
69
                $this->reversed[$fqcn->toString()] = $alias;
70
            }
71
        }
72
        return $this->reversed;
73
    }
74
}
75