You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I guess the question is, how we treat the token names in regard to backwards compatbility and whether we need/want to align the token names here with the token names in php-src
My assumption is - and I did not verify that in any way yet - this is caused by the switch to PHPToken::tokenize() and it behaving differently than the old token_get_all. I believe that actually was one reason to introduce the ::tokenize()-method into PHP in the first place, so we have a reliable object only result set?
If that is correct, we might not need the internal Map at all anymore.
So, if we stick with ::tokenize() I guess that's just a change we have to accept, maybe allowing us to clean up the mapping logic as the (use) case should no longer exist.
This class provides an alternative to token_get_all(). While the function returns tokens either as a single-character string, or an array with a token ID, token text and line number, PhpToken::tokenize() normalizes all tokens into PhpToken objects, which makes code operating on tokens more memory efficient and readable.
Okay, a quick test shows the Object is equally useless when it comes to names, e.g. the ::name() method returns ) for example instead of an actual token name like T_CLOSE_BRACKET.
it looks like with 2.0.x we are now using T_AMPERSAND more often as we did with the 1.x counterpart.
To me, that would only make sense when the ::tokenize() result uses & as a name more frequently. Otherwise we did not change anything I can see that should be relevant to this.
I'm still confused as to why that should be happening more frequently and would like to understand it better before I commit to that change.
Could it be that token_get_all actually returned a TOKEN for those cases already? And now that we look at ->text value only, we screw it up?
So if we indeed remove the & from the map, we would be fine: There is no reason to assume we still get a single char & as ->text - right? Even something like $x="&"; would tokenize differently..
Okay, token_get_all returns a token array since PHP 8.1 as to be expected, a single char string for 8.0 and 7.4 (didn't test older versions). That means indeed using a ->text lookup is a BC break for &.
That means with PHP 8.1+, we shouldn't have seen T_AMPERSAND at all in the XML output with 1.x, but with PHP 8.0 and earlier. This at least qualifies as an unintended side effect and an inconsistency between PHP versions.
So now we have to decide:
Do we go for the consistency? Than we should keep the code as is, as it also normalizes the two potential cases the 8.1+ tokenizing has into a single token, same as it was before PHP 8.1.
We stick closer to the PHP tokenizing, and remove the "custom" T_AMPERSAND from 2.x.
I'm leaning to removing it as with 2.x we require PHP 8.1+ and there might be some sort of value in having the two explicit tokens represented. Even if I fail to see a use case ;-)
Do we go for the consistency? Than we should keep the code as is, as it also normalizes the two potential cases the 8.1+ tokenizing has into a single token, same as it was before PHP 8.1.
I don't think the job of this lib is to interpret tokens differently than php-src and change meaning/abstract details away
We stick closer to the PHP tokenizing, and remove the "custom" T_AMPERSAND from 2.x.
Do we go for the consistency? Than we should keep the code as is, as it also normalizes the two potential cases the 8.1+ tokenizing has into a single token, same as it was before PHP 8.1.
I don't think the job of this lib is to interpret tokens differently than php-src and change meaning/abstract details away
I disagree with that statement.
If you'd want a library behaving like this statement suggests, we'd need to remove the map completely.
But back when I started this library, I added a bunch of custom tokens for the very reason: To get actual Tokens for things where PHP does not.
Regardless, given that PHP 8.1+ now does have Tokens for &, i removed my custom one with Commit 024008b
Activity
staabm commentedon Dec 8, 2025
dropping this line will make the phpunit/code-coverage regression go away:
tokenizer/src/Tokenizer.php
Line 35 in d1dd771
I guess the question is, how we treat the token names in regard to backwards compatbility and whether we need/want to align the token names here with the token names in php-src
theseer commentedon Dec 8, 2025
My assumption is - and I did not verify that in any way yet - this is caused by the switch to
PHPToken::tokenize()and it behaving differently than the oldtoken_get_all. I believe that actually was one reason to introduce the::tokenize()-method into PHP in the first place, so we have a reliable object only result set?If that is correct, we might not need the internal Map at all anymore.
So, if we stick with
::tokenize()I guess that's just a change we have to accept, maybe allowing us to clean up the mapping logic as the (use) case should no longer exist.theseer commentedon Dec 8, 2025
Reading the documentation helps :-)
So at least my memory was right.
theseer commentedon Dec 8, 2025
Okay, a quick test shows the Object is equally useless when it comes to names, e.g. the
::name()method returns)for example instead of an actual token name likeT_CLOSE_BRACKET.So I'm not sure as to what to do here :)
theseer commentedon Dec 8, 2025
To me, that would only make sense when the
::tokenize()result uses&as a name more frequently. Otherwise we did not change anything I can see that should be relevant to this.staabm commentedon Dec 8, 2025
I think easiest way forward would be to only keep the Tokenizer map for those single-characters which are still returned by
getTokenName()theseer commentedon Dec 8, 2025
The leaves the question of which ones are that? ;-)
And, I guess we should change the map access to use
::getTokenName()then, rather than using->text, right?staabm commentedon Dec 8, 2025
see e.g. https://3v4l.org/bfRMm#veol
staabm commentedon Dec 8, 2025
yes
theseer commentedon Dec 8, 2025
Funny.
So from PHP 8.1 onwards, it appears that only the
&has been removed. At least that's how I interprete the diff view on 3v4l.Given we, for 2.0.0 require 8.1+, I guess that means we can indeed remove that line from our map.
staabm commentedon Dec 8, 2025
oohh I think we can just do:
if (isset(self::MAP[$tok->getTokenName()])) {because of
theseer commentedon Dec 8, 2025
I'll update the code.
theseer commentedon Dec 8, 2025
I took the output for the 8.1+ from 3v4l and ran it through sort and uniq:
So that's 22 chars.
Our Map array still contains more entries after I removed the
&:That implies we'd loose more of "our" Tokens.
Are we sure we want that?
theseer commentedon Dec 8, 2025
The map lookup of course only would work as before if we stick to
->textI guess..staabm commentedon Dec 8, 2025
so that means we should stick with
if (isset(self::MAP[$tok->text])) {and remove only&because?
theseer commentedon Dec 8, 2025
There's a button in the 3v4l output to get a visual diff:
So I guess, ... yes?
theseer commentedon Dec 8, 2025
I'm still confused as to why that should be happening more frequently and would like to understand it better before I commit to that change.
Could it be that
token_get_allactually returned a TOKEN for those cases already? And now that we look at->textvalue only, we screw it up?So if we indeed remove the
&from the map, we would be fine: There is no reason to assume we still get a single char&as->text- right? Even something like$x="&";would tokenize differently..theseer commentedon Dec 8, 2025
Okay,
token_get_allreturns a token array since PHP 8.1 as to be expected, a single char string for 8.0 and 7.4 (didn't test older versions). That means indeed using a->textlookup is a BC break for&.theseer commentedon Dec 8, 2025
That means with PHP 8.1+, we shouldn't have seen
T_AMPERSANDat all in the XML output with 1.x, but with PHP 8.0 and earlier. This at least qualifies as an unintended side effect and an inconsistency between PHP versions.So now we have to decide:
Do we go for the consistency? Than we should keep the code as is, as it also normalizes the two potential cases the 8.1+ tokenizing has into a single token, same as it was before PHP 8.1.
We stick closer to the PHP tokenizing, and remove the "custom"
T_AMPERSANDfrom 2.x.theseer commentedon Dec 8, 2025
I'm leaning to removing it as with 2.x we require PHP 8.1+ and there might be some sort of value in having the two explicit tokens represented. Even if I fail to see a use case ;-)
Optionions?
staabm commentedon Dec 8, 2025
I don't think the job of this lib is to interpret tokens differently than php-src and change meaning/abstract details away
👍
theseer commentedon Dec 8, 2025
I disagree with that statement.
If you'd want a library behaving like this statement suggests, we'd need to remove the map completely.
But back when I started this library, I added a bunch of custom tokens for the very reason: To get actual Tokens for things where PHP does not.
Regardless, given that PHP 8.1+ now does have Tokens for
&, i removed my custom one with Commit 024008bI'll roll a new release.