php-pipe-operator icon indicating copy to clipboard operation
php-pipe-operator copied to clipboard

strict_types=1 makes pipe-operator less agile [question]

Open PawelSuwinski opened this issue 1 year ago • 3 comments

Hi

It is very nice piece of work, I am using pipe-operator in my projects. It really rocks, especially in combination with symfony expression language.

When I upgraded to 5.x within php8 migration I noticed that some of my expressions need to be fix because of strict_types=1.

I am wondering is declaring strict_types=1 is good for pipe-operator? What were the arguments for doing this?

As I understand the purpose of the pipe-operator it to make the code flow :), get it more compact and agile and so does the php coercive mode which is good thing in specific cases, changing it to strict_types in my opinion takes pipe-operator one step back in terms of agile.

For example:

class A 
{
  public function __toString(): string
  {
    return '[]';
  }
}
take(new A())->json_decode()->get();

On version 3-4.x it executes with no errors, on version 5.x it ends with:

PHP Fatal error:  Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, A given in /home/psuw/xtm/projekty/portal/portal/vendor/sebastiaanluca/php-pipe-operator/src/Pipe.php:37

Of course it will work but it is not so compact:

take((string) new A())->json_decode()->get();

symfony expression language:

take(a.__toString()).json_decode().get()

PawelSuwinski avatar Oct 26 '22 08:10 PawelSuwinski