Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101297 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95746 invoked from network); 11 Dec 2017 00:40:24 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2017 00:40:24 -0000 Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.54 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.54 mail-lf0-f54.google.com Received: from [209.85.215.54] ([209.85.215.54:40329] helo=mail-lf0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F1/04-53433-5F3DD2A5 for ; Sun, 10 Dec 2017 19:40:22 -0500 Received: by mail-lf0-f54.google.com with SMTP id c140so10492706lfg.7 for ; Sun, 10 Dec 2017 16:40:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=ZFIK5m7AcScD/kDHlU6jfRLeWxpluG/qtmoaF/sL7aQ=; b=GlXDPNhbfd2u4JaijCNt/ZOS/ijgXNkqSUU8fd6yhyjLj+42sx4TqnT/scsfrI0dxj VJiZ+UB/UQH7B9VFEazIOmIHydAN4H91xX9mpZuRjNfeoMpYQiuVF0XeuCEe/ad7yjvA m02+KZ7VNTNajv7dQFRX3zRoB1NAg6F1QC5M6fc6lcQCYsdnLrm+MSINbPvLozc8IDLf gIcrjb1bOQrz1LEms0tmpXMgHOfkR/Xa6usenR2u7dX6kJTqFDIJszVNGCK9jK9NTtTE XncbkTXmZwGJP5LZOHUUoZ7/QFQC28mKIyy8wQ5QXsHyBPAbCBgfL6kR6dHB0nKu3ctt L1gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ZFIK5m7AcScD/kDHlU6jfRLeWxpluG/qtmoaF/sL7aQ=; b=oA7PPSnxm+dPSKzkSMbD17nN5jsWiGECw8YVUcixOk9fIsPpu5zxQAUHcI7opO7oV8 6Z3Atwsgr2kvz8YdWoR2CorjIT+gEJD8JokoM8SToBUqoxSxOXIWzvqavB1d1rOsaCxd bpIdLXJ3u04YBETMh/pcIsT3PTy2G1nazVOvXyGfhyKU/ymi6g8x6e4AHNVf+f8GdO+3 7SVszTSGX8iqj5oZArnohzr6VRQGoLAnrjxcVXV/psV2U1BEWKPiU7OUiCGydnLd2w7v zNBwsTDesSy//mp9hHpO/LPAIee/gAhNj5ViLxcXpyC2VN2TkIdY1y1ZmqzuKvCr097/ jwLA== X-Gm-Message-State: AJaThX6CTIlig2lcWys5jbRR3ENLyutA3NHcvJ4Dn+Xs+A/RfOmi1ogN 7nGtoliMUzkOx1ayRSuNTqoNGgm5 X-Google-Smtp-Source: AGs4zMa7RJc756GCGje2YI71YHwuTMX2QzloQ2GkTQr3tsCR2nVLA40+3Uc33HaX2gNZokTPfMsbJg== X-Received: by 10.46.57.20 with SMTP id g20mr18659925lja.150.1512952818220; Sun, 10 Dec 2017 16:40:18 -0800 (PST) Received: from mail-lf0-f41.google.com (mail-lf0-f41.google.com. [209.85.215.41]) by smtp.googlemail.com with ESMTPSA id 91sm2466247lfu.83.2017.12.10.16.40.16 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Dec 2017 16:40:17 -0800 (PST) Received: by mail-lf0-f41.google.com with SMTP id x204so17288869lfa.11 for ; Sun, 10 Dec 2017 16:40:16 -0800 (PST) X-Received: by 10.46.101.74 with SMTP id z71mr19923405ljb.35.1512952816079; Sun, 10 Dec 2017 16:40:16 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.170.16 with HTTP; Sun, 10 Dec 2017 16:39:55 -0800 (PST) Date: Mon, 11 Dec 2017 01:39:55 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: ReflectionContext for imports and namespace From: andreas@dqxtech.net (Andreas Hennings) 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.