Skip to content

Commit

Permalink
Add Name::getParts(), deprecate Name::$parts
Browse files Browse the repository at this point in the history
In preparation for switching this to a plain string in
PHP-Parser 5, deprecate direct access to the property and
provide an API that will work on both versions.

(cherry picked from commit c9e5a13)
  • Loading branch information
nikic committed May 21, 2023
1 parent d43edfb commit df3a705
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/PhpParser/Node/Name.php
Expand Up @@ -5,7 +5,10 @@
use PhpParser\NodeAbstract;

class Name extends NodeAbstract {
/** @var string[] Parts of the name */
/**
* @var string[] Parts of the name
* @deprecated Use getParts() instead
*/
public $parts;

/** @var array<string, bool> */
Expand All @@ -30,6 +33,15 @@ public function getSubNodeNames(): array {
return ['parts'];
}

/**
* Get parts of name (split by the namespace separator).
*
* @return string[] Parts of name
*/
public function getParts(): array {
return $this->parts;
}

/**
* Gets the first part of the name, i.e. everything before the first namespace separator.
*
Expand Down
2 changes: 2 additions & 0 deletions test/PhpParser/Node/NameTest.php
Expand Up @@ -18,10 +18,12 @@ public function testGet() {
$name = new Name('foo');
$this->assertSame('foo', $name->getFirst());
$this->assertSame('foo', $name->getLast());
$this->assertSame(['foo'], $name->getParts());

$name = new Name('foo\bar');
$this->assertSame('foo', $name->getFirst());
$this->assertSame('bar', $name->getLast());
$this->assertSame(['foo', 'bar'], $name->getParts());
}

public function testToString() {
Expand Down

0 comments on commit df3a705

Please sign in to comment.