Skip to content

Commit e954007

Browse files
committed
add getName method
1 parent b1521b0 commit e954007

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/Fqsen.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ final class Fqsen
2323
*/
2424
private $fqsen;
2525

26+
/**
27+
* @var string name of the element without path.
28+
*/
29+
private $name;
30+
2631
/**
2732
* Initializes the object.
2833
*
@@ -32,14 +37,23 @@ final class Fqsen
3237
*/
3338
public function __construct($fqsen)
3439
{
35-
$result = preg_match('/^\\\\([A-Za-z_\\\\]+)(::\\$?[a-zA-Z_]+)?(\\(\\))?$/', $fqsen);
40+
$matches = array();
41+
$result = preg_match('/^\\\\([A-Za-z_\\\\]+)(?:[:]{2}\\$?([a-zA-Z_]+))?(?:\\(\\))?$/', $fqsen, $matches);
42+
3643
if ($result === 0) {
3744
throw new \InvalidArgumentException(
3845
sprintf('"%s" is not a valid Fqsen.', $fqsen)
3946
);
4047
}
4148

4249
$this->fqsen = $fqsen;
50+
51+
if (isset($matches[2])) {
52+
$this->name = $matches[2];
53+
} else {
54+
$matches = explode('\\', $fqsen);
55+
$this->name = trim(end($matches), '()');
56+
}
4357
}
4458

4559
/**
@@ -51,4 +65,14 @@ public function __toString()
5165
{
5266
return $this->fqsen;
5367
}
68+
69+
/**
70+
* Returns the name of the element without path.
71+
*
72+
* @return string
73+
*/
74+
public function getName()
75+
{
76+
return $this->name;
77+
}
5478
}

tests/unit/FqsenTest.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ class FqsenTest extends \PHPUnit_Framework_TestCase
2222
* @covers ::__construct
2323
* @dataProvider validFqsenProvider
2424
*/
25-
public function testValidFormats($fqsen)
25+
public function testValidFormats($fqsen, $name)
2626
{
27-
new Fqsen($fqsen);
27+
$instance = new Fqsen($fqsen);
28+
$this->assertEquals($name, $instance->getName());
2829
}
2930

3031
/**
@@ -35,15 +36,15 @@ public function testValidFormats($fqsen)
3536
public function validFqsenProvider()
3637
{
3738
return [
38-
['\My\Space'],
39-
['\My\Space\myFunction()'],
40-
['\My\Space\MY_CONSTANT'],
41-
['\My\Space\MyClass'],
42-
['\My\Space\MyInterface'],
43-
['\My\Space\MyTrait'],
44-
['\My\Space\MyClass::myMethod()'],
45-
['\My\Space\MyClass::$my_property'],
46-
['\My\Space\MyClass::MY_CONSTANT'],
39+
['\My\Space', 'Space'],
40+
['\My\Space\myFunction()', 'myFunction'],
41+
['\My\Space\MY_CONSTANT', 'MY_CONSTANT'],
42+
['\My\Space\MyClass', 'MyClass'],
43+
['\My\Space\MyInterface', 'MyInterface'],
44+
['\My\Space\MyTrait', 'MyTrait'],
45+
['\My\Space\MyClass::myMethod()', 'myMethod'],
46+
['\My\Space\MyClass::$my_property', 'my_property'],
47+
['\My\Space\MyClass::MY_CONSTANT', 'MY_CONSTANT'],
4748
];
4849
}
4950

0 commit comments

Comments
 (0)