Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101305 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20307 invoked from network); 11 Dec 2017 07:36:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2017 07:36:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.160 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.160 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.160] ([81.169.146.160:14630] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 93/D6-53433-6653E2A5 for ; Mon, 11 Dec 2017 02:36:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1512977764; s=domk; d=kelunik.com; h=Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject; bh=vdgzTiuq2fM9jceLfU/AlPJejmwNo3uXDULysL66qKk=; b=P8zVN8ZZo/zh68MABS+OBTUAjpgGd6AJqMqOGxKVE5f+dq0UsyciYvkDm5PIhhpdCj c9/LbMMeX3iRNyDVuKyfor00KNBtheOJEATLjYP2Rd6qb6ZfuBSy1o9QLPnx7IwIQu9/ 8hGhwozxNvJa44Sl5csN9u96hXC9dkB1/ezEw= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mlsGbEv0XHBzMIJSS+jKTzde5mDb8AaBUcZiAqcA== X-RZG-CLASS-ID: mo00 Received: by mail-yb0-f176.google.com with SMTP id j7so4983080ybl.3 for ; Sun, 10 Dec 2017 23:36:04 -0800 (PST) X-Gm-Message-State: AJaThX6shvROLZxk//YAtfkKFJ/BQZsCod9JAXrmmKg9VJ1C8iNQLJ9G cYQ61l6meqV/8ARsWZVJzQBfyDxbrN14J3eDxNs= X-Google-Smtp-Source: AGs4zMYz1kClwa9ZMIxPun4uwZOrGMJ8C3piNbSbi+bF+tYWbHB3c+wyl2u65h2YZHTj2Ed0u6Fv/8l4X/Lj5x5edOI= X-Received: by 10.129.49.84 with SMTP id x81mr25494838ywx.191.1512977763473; Sun, 10 Dec 2017 23:36:03 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 11 Dec 2017 07:35:52 +0000 X-Gmail-Original-Message-ID: Message-ID: To: Andreas Hennings Cc: PHP Internals Content-Type: multipart/alternative; boundary="001a11407d68251f7905600b96f5" Subject: Re: [PHP-DEV] ReflectionContext for imports and namespace From: me@kelunik.com (Niklas Keller) --001a11407d68251f7905600b96f5 Content-Type: text/plain; charset="UTF-8" Andreas Hennings schrieb am Mo., 11. Dez. 2017, 01:39: > TLDR: > I propose to introduce a new class \ReflectionContext (or perhaps > \ReflectionScope?), to give information about imported aliases and the > namespace. > > > ## Background / motivation > > Some libraries that parse doc comment annotations, e.g. phpDocumentor, > need to know the class aliases and the namespace that are active in > the place (scope) where the class, function or method is declared. > > E.g. phpDocumentor has this class: > > https://github.com/phpDocumentor/TypeResolver/blob/552bf401d264a443819a66233932be6a23f59d78/src/Types/Context.php > > To get this information, they parse the PHP file where the class is > defined: > > https://github.com/phpDocumentor/TypeResolver/blob/552bf401d264a443819a66233932be6a23f59d78/src/Types/ContextFactory.php > > It would be much more pleasant if PHP would provide this information > through the reflection API. > A custom library should not need to do an expensive and fragile > parsing job for information that is already known. > > Also, this external parsing might not cover all cases, depending how > it is implemented. E.g. local namespace scopes in curly brackets. (Who > uses those, btw?) > > > ## Proposal > > Make information about class aliases (imported through use statements) > and the active namespace available through the reflection API. > > $reflClass = new \ReflectionClass(C::class); > $reflContext = $reflClass->getContext(); > $aliases = $reflContext->getAliases(); > > > ## Details > > class ReflectionContext { > > function getNamespace() : string {..} > > /** > * @return string[] > * Format: $[$alias] = $qcn > */ > function getAliases() : string[] {..} > > /** > * @return string|null > * The qcn of the class, or null if it is a built-in type like > "string". > */ > function resolveAlias($alias) : ?string {..} > } > > The $reflClass->getContext()->getNamespace() is the same as > $reflClass->getNamespace(). > > But having it all in the $context object allows this object to be > passed around to components that need it. > > > ## Open questions > > In a method doc comment, we also want to resolve the "self" keyword. > For this, the class name is needed as well, not just external imports > and namespace. > > Maybe also introduce \RefllectionMethod::getContext()? Not sure. > > Should it be "context" or "scope"? It is not the same. Maybe "scope" > leads to the wrong expectations. > Documentation tools shouldn't run the code IMO, that means they won't have access to that feature. Nikic's PHP parser already contains a tool that resolves all namespaces, you can probably write a similar visitor for the Ast that also works for PHPdoc blocks. Regards, Niklas > --001a11407d68251f7905600b96f5--