On Mon, Sep 5, 2016 at 7:38 PM, Andrea Faulds <a...@ajf.me> 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 with
>> the declare directive (but not trivial because of non semantic tokens).
>>
>> Here is my current regex to do so:
>>>
>>> $c = '(?:\s*+(?:(?:#|//)[^\n]*+\n|/\*(?:(?<!\*/).)++)?+)*+';
>>> $strictTypesRegex = str_replace('.', $c, "'^<\?php\s.declare.\(.strict_
>>
>> types.=.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 represented
> 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 that
> compilation stage, however, there's no structure for a “file” in memory
> (beyond maybe in OPcache). The constants, functions, classes and variables
> 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 file
> 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 tracks
> whether they use strict typing or not. Instead it marks any code originating
> 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 feel
> like reflection might be the wrong tool to use. I think reflection is more
> intended for inspecting your own codebase, rather than analysing arbitrary
> PHP files. In order to use reflection on some PHP file, you have to include
> 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 typing,
> 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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to