Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:95668 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28705 invoked from network); 5 Sep 2016 18:42:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Sep 2016 18:42:06 -0000 Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wm0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:37047] helo=mail-wm0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 35/B7-45301-C7CBDC75 for ; Mon, 05 Sep 2016 14:42:05 -0400 Received: by mail-wm0-f54.google.com with SMTP id w12so54957569wmf.0 for ; Mon, 05 Sep 2016 11:42:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-transfer-encoding; bh=XlPcZywZGiU0f+DbFWw4xIfEXi0CiQxe6WSTll2h4TU=; b=aZipce6npUfy/M/REi+dA4mNL+WduspbPm66vDWbvsQ9n3xyYzIaCs6rQwKAwVqz9J ns7DABnmJPi+Qhb+KEP04BsrFgLiaQbPrhqB0znr4MMCnntT3hdnA7QpdKf5Lff3u3rB l2f04pH24cl4VBfq9j1MnUPNYFQytIgg013GyJWZa8z4fUXvTy0QvYdHVAYew5fEbv+k LFAcMedboUO6CpRj83xsnds3W3D+Or+4S/R4vve0cGHyAPsDnR81AEJinV/ty9j94Fgh V/XT0ULLkqWBjemUt9jt4fRuNXBQFR9AySiumepJ1xDrNeL2j+5aMursCJA/438IAWIL Ye9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-transfer-encoding; bh=XlPcZywZGiU0f+DbFWw4xIfEXi0CiQxe6WSTll2h4TU=; b=iUEStuh5ewWmMGVE03bLGe9zNVi3bT2YyqHfOD223YSXGIXrUSTd0LbQGjeDJ3YWNo ShEO0Zi/Mr1FWEdClUO2+8MyCukKkiMK/1lfAoQl2hbgBv3xxDowVrz9250tu1bqMOzb L+XoiH+c+KJp01lvZJTBCc3j8fb9dc93hAn8pMhF8zYF+nnI4YjLUJUKSx7mO9zwzEjL V8q44SbRLd/qXSF/995keojfe8WV2pW/8nFrP+YJLrfY/mgbGeXYVNFEhuisXYsWHObI lfTpLiT1sRmr11McEKX4c/amhxylLs0mn+3Uew24Li4wKxs1ObczsCeT6YaMFLtLpSA4 Abxg== X-Gm-Message-State: AE9vXwM/LixB/1WJ6Q1RBQG73rZajXDBZ8CDmqXp4g2JVD3Og0YJ3WVEy7a5fvTPoEdMxnVlAm1D6qHkjn+iVw== X-Received: by 10.28.39.133 with SMTP id n127mr17213297wmn.6.1473100921753; Mon, 05 Sep 2016 11:42:01 -0700 (PDT) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.194.45.4 with HTTP; Mon, 5 Sep 2016 11:41:21 -0700 (PDT) In-Reply-To: References: Date: Mon, 5 Sep 2016 20:41:21 +0200 X-Google-Sender-Auth: SFDJp5pWCWNrC3An0VCEWrE_BaU Message-ID: To: Andrea Faulds Cc: PHP Internals Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Missing reflection info about strict types? From: jpauli@php.net (Julien Pauli) On Mon, Sep 5, 2016 at 7:38 PM, Andrea Faulds wrote: > Hi, > > Nicolas Grekas wrote: >> >> Thanks for trying Julien >> >> I guess what you miss for what you want to do, is to detect if strict >>> >>> types are activated into the current scope, at runtime. >>> >> >> From the inside, the author of the code knows if they added the declare >> or >> not. >> I'd need to know from the outside, before concatenating it, if some file >> has strict types. >> This could be exposed on the reflection, since a function/method/class >> could have a flag that tells if it has been compiled with strict types >> enabled or not. >> The current alternative is to parse the source to check if it starts wit= h >> the declare directive (but not trivial because of non semantic tokens). >> >> Here is my current regex to do so: >>> >>> $c =3D '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?>> $strictTypesRegex =3D str_replace('.', $c, "'^<\?php\s.declare.\(.stric= t_ >> >> types.=3D.1.\).;'is"); > > > Reflection deals with classes, functions, and so on, but it doesn't deal > with files. Files are, for the most part, a detail that is only represent= ed > when code is compiled (e.g. when an include or require statement is run, = or > when a script is executed by a request or from the command line). After t= hat > compilation stage, however, there's no structure for a =E2=80=9Cfile=E2= =80=9D in memory > (beyond maybe in OPcache). The constants, functions, classes and variable= s > it defines are kept track of by the PHP interpreter, but the file itself > isn't directly. > > Because of this, implementing a reflection method to tell you whether a f= ile > uses the strict_types directive isn't currently possible. To the best of = my > knowledge, PHP doesn't maintain a registry of files in memory which track= s > whether they use strict typing or not. Instead it marks any code originat= ing > from strictly-typed files as using strict mode. > > I suppose the PHP interpreter could be modified so it kept track of this > information. Though, if you're concatenating arbitrary source files, I fe= el > like reflection might be the wrong tool to use. I think reflection is mor= e > intended for inspecting your own codebase, rather than analysing arbitrar= y > PHP files. In order to use reflection on some PHP file, you have to inclu= de > it first, and you can't un-include it later. You also can't prevent any > top-level PHP code in that file from executing when you include it. > > If you don't need to know whether a /file/ specifically uses strict typin= g, > and only need to know which classes do, that might be possible, given PHP > does know at runtime which methods use strict typing. Again, though, I'm = not > sure if reflection is the best tool. > > Your best hope is probably to try and parse the file using PHP-Parser or = the > Tokenizer extension. > > I hope this is helpful. I have to both, confirm and agree. Julien