public static function fromStringInContext(string $fqsen_string, Context $context) : FullyQualifiedFunctionName { // Check to see if we're fully qualified if (0 === strpos($fqsen_string, '\\')) { return static::fromFullyQualifiedString($fqsen_string); } // Split off the alternate ID $parts = explode(',', $fqsen_string); $fqsen_string = $parts[0]; $alternate_id = (int) ($parts[1] ?? 0); assert(is_int($alternate_id), "Alternate must be an integer"); $parts = explode('\\', $fqsen_string); $name = array_pop($parts); assert(!empty($name), "The name cannot be empty"); // Check for a name map if ($context->hasNamespaceMapFor(static::getNamespaceMapType(), $name)) { return $context->getNamespaceMapFor(static::getNamespaceMapType(), $name); } // For functions we don't use the context's namespace if // there is no NS on the call. $namespace = implode('\\', array_filter($parts)); return static::make($namespace, $name, $alternate_id); }
public function testFullyQualifiedFunctionName() { $this->assertFQSENEqual(FullyQualifiedFunctionName::make('\\Name\\Space', 'g'), '\\Name\\Space\\g'); $this->assertFQSENEqual(FullyQualifiedFunctionName::make('', 'g'), '\\g'); $this->assertFQSENEqual(FullyQualifiedGlobalConstantName::make('', 'g'), '\\g'); $this->assertFQSENEqual(FullyQualifiedFunctionName::fromFullyQualifiedString('\\g'), '\\g'); $this->assertFQSENEqual(FullyQualifiedFunctionName::fromStringInContext('g', $this->context), '\\g'); }