function Name::slice
Gets a slice of a name (similar to array_slice).
This method returns a new instance of the same type as the original and with the same attributes.
If the slice is empty, null is returned. The null value will be correctly handled in concatenations using concat().
Offset and length have the same meaning as in array_slice().
Parameters
int $offset Offset to start the slice at (may be negative):
int|null $length Length of the slice (may be negative):
Return value
static|null Sliced name
File
-
vendor/
nikic/ php-parser/ lib/ PhpParser/ Node/ Name.php, line 175
Class
Namespace
PhpParser\NodeCode
public function slice(int $offset, ?int $length = null) {
if ($offset === 1 && $length === null) {
// Short-circuit the common case.
if (false !== ($pos = \strpos($this->name, '\\'))) {
return new static(\substr($this->name, $pos + 1));
}
return null;
}
$parts = \explode('\\', $this->name);
$numParts = \count($parts);
$realOffset = $offset < 0 ? $offset + $numParts : $offset;
if ($realOffset < 0 || $realOffset > $numParts) {
throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
}
if (null === $length) {
$realLength = $numParts - $realOffset;
}
else {
$realLength = $length < 0 ? $length + $numParts - $realOffset : $length;
if ($realLength < 0 || $realLength > $numParts - $realOffset) {
throw new \OutOfBoundsException(sprintf('Length %d is out of bounds', $length));
}
}
if ($realLength === 0) {
// Empty slice is represented as null
return null;
}
return new static(array_slice($parts, $realOffset, $realLength), $this->attributes);
}