Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101306 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22127 invoked from network); 11 Dec 2017 07:46:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2017 07:46:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=ocramius@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ocramius@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.177 as permitted sender) X-PHP-List-Original-Sender: ocramius@gmail.com X-Host-Fingerprint: 209.85.128.177 mail-wr0-f177.google.com Received: from [209.85.128.177] ([209.85.128.177:37154] helo=mail-wr0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F0/37-53433-6C73E2A5 for ; Mon, 11 Dec 2017 02:46:14 -0500 Received: by mail-wr0-f177.google.com with SMTP id k61so16460415wrc.4 for ; Sun, 10 Dec 2017 23:46:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=yWpNBg/leBlDPP8hd5oUDhllpKgdw4V/4IAd1xtR48M=; b=YHNRaaaaibOgnK1IOrKnrVPlODgeOjOS44Bh0gtp/0EWuXwycDW2I5VqIYR3R5qd/E bC2n4iTYT74pUl4Th2+KJmCcn+Ll3UlNQxa1OTsXjKHtrvabuxVnUlTq7/H2YsGET61f sqr7d95GwCrvi9NutsLZ2Ge6UVfHC2rOMeTcMuVr3g+NrtYp86b68tF+hVXvmNcyxOpF ocVuU01lWhf2L8cK/p25gYY8ipW+px2YxGH3u8gOSR35b1jrTd81nPXcxYoTnxuln0fx GBL6FYXMtu7E51uX7BPQXLIpY55SA6agcyqGgu8Bp/phOddiy93BPTimkIwKTnHvsAII 0FGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=yWpNBg/leBlDPP8hd5oUDhllpKgdw4V/4IAd1xtR48M=; b=YgfSwOvkjypVxVpzkfWwhCENDEFpqyhsDdmIZc0son6o0qXP8PphZy3RzNwFXwhCox fBfB5X5h5YFJobbRkokNZHvf1j+gYxSN7/ozL9COZAD2Y9Ja3FQVQbzyhPEDO7Djmx3B eMs7v6g4leT2+24dtDDZPTy30smj3fTSrZZIARSQ6qyWB3K7kuX6EjUIqc6rjNBb7YBP tUX/P2d/4dE0azsslXcy6ibpC2nRxmT4nmu46BBqamC10a8jbunc9WXqxzOWxwLj1Mpz fiGmKntEU2UJPFX/xhnhwNeCv1/hcJ+bMRHD5INfjxU6mg9Fb+05Ety04Nc275QxyYLI DDoA== X-Gm-Message-State: AJaThX4e6H7zHCJhoHr8yAWpHuxX2kyEFhORWr0pPRoLDZDv7oZz4+Qr wrtcIsf2jKZhmfjJZHaNPRseQe7Jct2hzYemuSY= X-Google-Smtp-Source: AGs4zMbYjASI4NYroTIKKV1HeBXJc1IT5ifN+0RxRCyPoycZRJjtAhs4M8EuasiyiCAiNkczwwuEgDrddL0wiQZLtKg= X-Received: by 10.223.177.194 with SMTP id r2mr33817373wra.84.1512978371342; Sun, 10 Dec 2017 23:46:11 -0800 (PST) MIME-Version: 1.0 Received: by 10.223.184.205 with HTTP; Sun, 10 Dec 2017 23:46:09 -0800 (PST) Received: by 10.223.184.205 with HTTP; Sun, 10 Dec 2017 23:46:09 -0800 (PST) In-Reply-To: References: Date: Mon, 11 Dec 2017 08:46:09 +0100 Message-ID: To: Niklas Keller Cc: Andreas Hennings , PHP Internals List Content-Type: multipart/alternative; boundary="f403045e756660764305600bba7e" Subject: Re: [PHP-DEV] ReflectionContext for imports and namespace From: ocramius@gmail.com (Marco Pivetta) --f403045e756660764305600bba7e Content-Type: text/plain; charset="UTF-8" Indeed that already exists at https://github.com/Roave/BetterReflection/blob/2.0.1/docs/features.md#analysing-types-from-docblocks - relatively new lib, so it probably didn't get noticed upfront in here. It would probably be a good idea to address the fact that the current reflection API causes autoloading and evaluation of the loaded files before denying API improvement attempts upfront though: that limitation is unrelated with what Andreas is proposing, in my opinion. On 11 Dec 2017 08:36, "Niklas Keller" wrote: 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 > --f403045e756660764305600bba7e--