Re: [PHP-DEV] [VOTE] Object-based token_get_all() alternative

This is only part of a thread. view whole thread
  108972
March 11, 2020 09:57 nikita.ppv@gmail.com (Nikita Popov)
On Tue, Mar 10, 2020 at 5:19 PM Côme Chilliet <
come.chilliet@fusiondirectory.org> wrote:

> Le mardi 10 mars 2020, 15:53:41 CET Nikita Popov a écrit : > > Would __toString() return $token->text? I'm not strictly opposed to this, > > but I'd be hard pressed to think of a situation where I would prefer > > writing (string) $token over $token->text. Generally, the less magic the > > better :) > > My idea was that since tokens are parsed from a string it should be easy > to write a string back with them. > I do not have an obvious usecase but the idea is doing stuff like > > if ($token.is('function')) { > echo "public $token"; > } else { > echo $token; > } > > Am I right to assume that with __toString the same as ->text we’d have > implode('', PhpToken::getAll($code)) == $code ? > > This would allow: > $tokens = PhpToken::getAll($code); > // Modifications on $tokens > $code = implode('', $tokens); >
Yes, that would work. It's not much harder without __toString though: $code = implode(array_column($tokens, 'text')); Does anyone else have thoughts on whether PhpToken should implement __toString() or not? Nikita
  108982
March 11, 2020 15:59 larry@garfieldtech.com ("Larry Garfield")
On Wed, Mar 11, 2020, at 4:57 AM, Nikita Popov wrote:
> On Tue, Mar 10, 2020 at 5:19 PM Côme Chilliet < > come.chilliet@fusiondirectory.org> wrote: > > > Le mardi 10 mars 2020, 15:53:41 CET Nikita Popov a écrit : > > > Would __toString() return $token->text? I'm not strictly opposed to this, > > > but I'd be hard pressed to think of a situation where I would prefer > > > writing (string) $token over $token->text. Generally, the less magic the > > > better :) > > > > My idea was that since tokens are parsed from a string it should be easy > > to write a string back with them. > > I do not have an obvious usecase but the idea is doing stuff like > > > > if ($token.is('function')) { > > echo "public $token"; > > } else { > > echo $token; > > } > > > > Am I right to assume that with __toString the same as ->text we’d have > > implode('', PhpToken::getAll($code)) == $code ? > > > > This would allow: > > $tokens = PhpToken::getAll($code); > > // Modifications on $tokens > > $code = implode('', $tokens); > > > > Yes, that would work. It's not much harder without __toString though: > > $code = implode(array_column($tokens, 'text')); > > Does anyone else have thoughts on whether PhpToken should implement > __toString() or not? > > Nikita
I forget who it was but someone on the list a while back offered a heuristic of "__toString should be used iff there is one and only one logical string representation of a value, because the value is conceptually just a string with extra metadata on it". That sounds like a really solid heuristic to me. In this case, I think yes, ->text is the only reasonable thing to produce for __toString(). Or perhaps this is a good use case for "you can use a subclass, do that" if there's any good reason to not have it just be ->text. --Larry Garfield
  108994
March 12, 2020 11:07 come.chilliet@fusiondirectory.org (=?ISO-8859-1?Q?C=F4me?= Chilliet)
Le mercredi 11 mars 2020, 10:59:19 CET Larry Garfield a écrit :
> I forget who it was but someone on the list a while back offered a heuristic of "__toString should be used iff there is one and only one logical string representation of a value, because the value is conceptually just a string with extra metadata on it". > > That sounds like a really solid heuristic to me. In this case, I think yes, ->text is the only reasonable thing to produce for __toString().
Yes that was my reasoning, a token is essentially a substring, so I was expecting __toString to reflect this and allow me to use it as a string. -- Côme Chilliet FusionDirectory - https://www.fusiondirectory.org