1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* phpDocumentor |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @link http://phpdoc.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use function assert; |
18
|
|
|
use function end; |
19
|
|
|
use function explode; |
20
|
|
|
use function is_string; |
21
|
|
|
use function preg_match; |
22
|
|
|
use function sprintf; |
23
|
|
|
use function trim; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Value Object for Fqsen. |
27
|
|
|
* |
28
|
|
|
* @link https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-meta.md |
29
|
|
|
* |
30
|
|
|
* @psalm-immutable |
31
|
|
|
*/ |
32
|
|
|
final class Fqsen |
33
|
|
|
{ |
34
|
|
|
/** @var string fully qualified structural element name */ |
35
|
|
|
private $fqsen; |
36
|
|
|
|
37
|
|
|
/** @var string name of the element without path. */ |
38
|
|
|
private $name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
17 |
|
* Initializes the object. |
42
|
|
|
* |
43
|
17 |
|
* @throws InvalidArgumentException when $fqsen is not matching the format. |
44
|
17 |
|
*/ |
45
|
17 |
|
public function __construct(string $fqsen) |
46
|
17 |
|
{ |
47
|
17 |
|
$matches = []; |
48
|
|
|
|
49
|
|
|
$result = preg_match( |
50
|
17 |
|
//phpcs:ignore Generic.Files.LineLength.TooLong |
51
|
4 |
|
'/^\\\\([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff\\\\]*)?(?:[:]{2}\\$?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*))?(?:\\(\\))?$/', |
52
|
4 |
|
$fqsen, |
53
|
|
|
$matches |
54
|
|
|
); |
55
|
|
|
|
56
|
13 |
|
if ($result === 0) { |
57
|
|
|
throw new InvalidArgumentException( |
58
|
13 |
|
sprintf('"%s" is not a valid Fqsen.', $fqsen) |
59
|
3 |
|
); |
60
|
|
|
} |
61
|
10 |
|
|
62
|
10 |
|
$this->fqsen = $fqsen; |
63
|
|
|
|
64
|
13 |
|
if (isset($matches[2])) { |
65
|
|
|
$this->name = $matches[2]; |
66
|
|
|
} else { |
67
|
|
|
$matches = explode('\\', $fqsen); |
68
|
|
|
$name = end($matches); |
69
|
1 |
|
assert(is_string($name)); |
70
|
|
|
$this->name = trim($name, '()'); |
71
|
1 |
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* converts this class to string. |
76
|
|
|
*/ |
77
|
|
|
public function __toString() : string |
78
|
|
|
{ |
79
|
|
|
return $this->fqsen; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns the name of the element without path. |
84
|
|
|
*/ |
85
|
|
|
public function getName() : string |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|