1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Domain\Project; |
4
|
|
|
|
5
|
|
|
class FQN { |
6
|
|
|
|
7
|
|
|
public function __construct($namespace = "") { |
8
|
|
|
if ($namespace) { |
9
|
|
|
if ($namespace instanceof FQN) { |
10
|
|
|
$this->parts = $namespace->getParts(); |
11
|
|
|
} |
12
|
|
|
elseif (!is_array($namespace)) { |
13
|
|
|
$this->parts = explode("\\", $namespace); |
14
|
|
|
} |
15
|
|
|
else { |
16
|
|
|
$this->parts = $namespace; |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
else { |
20
|
|
|
$this->parts = []; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Joins FQN to the clone of the current FQN and returns it |
26
|
|
|
* |
27
|
|
|
* @return FQN |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function join(FQN $join) { |
|
|
|
|
30
|
|
|
$result = new self(); |
31
|
|
|
$resultParts = $this->getParts(); |
32
|
|
|
$joiningParts = $join->getParts(); |
33
|
|
|
if ($this->getLast() === $join->getFirst()) { |
34
|
|
|
array_shift($joiningParts); |
35
|
|
|
} |
36
|
|
|
$result->setParts(array_merge($resultParts, $joiningParts)); |
37
|
|
|
return $result; |
38
|
|
|
} |
39
|
|
|
public function getFirst() { |
40
|
|
|
$parts = $this->getParts(); |
41
|
|
|
return array_shift($parts); |
42
|
|
|
} |
43
|
|
|
public function getLast() { |
44
|
|
|
$parts = $this->getParts(); |
45
|
|
|
return array_pop($parts); |
46
|
|
|
} |
47
|
|
|
public function getTail() { |
48
|
|
|
$parts = $this->getParts(); |
49
|
|
|
array_pop($parts); |
50
|
|
|
return $parts; |
51
|
|
|
} |
52
|
|
|
public function getParts() { |
53
|
|
|
if (is_array($this->parts)) { |
54
|
|
|
return $this->parts; |
55
|
|
|
} |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
public function setParts(array $parts) { |
59
|
|
|
$this->parts = $parts; |
60
|
|
|
} |
61
|
|
|
public function addPart($part) { |
62
|
|
|
$this->parts[] = $part; |
63
|
|
|
} |
64
|
|
|
public function toString() { |
65
|
|
|
return implode("\\", $this->getParts()); |
66
|
|
|
} |
67
|
|
|
public function __toString() { |
68
|
|
|
return $this->toString(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static $test; |
72
|
|
|
private $parts; |
73
|
|
|
} |
74
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.