Larastan: Cannot cast Ramsey\Uuid\UuidInterface to string.

Created on 23 Jul 2018  Â·  8Comments  Â·  Source: nunomaduro/larastan

Got a message saying

Cannot cast Ramsey\Uuid\UuidInterface to string.

for this line:

$instance->uuid = (string) Str::uuid();

This works just fine, also see Laravel docs.

Thanks!

Most helpful comment

You're right. The interface has a toString (non-magic) method that should be used instead. Thanks for teaching me something today.

All 8 comments

The method should return Ramsey\Uuid\Uuid, not the interface.

On Mon, 23 Jul 2018 at 14:37, Michał Karnicki notifications@github.com
wrote:

Got a message saying

Cannot cast Ramsey\Uuid\UuidInterface to string.

for this line:

$instance->uuid = (string) Str::uuid();

This works just fine, also see Laravel docs
https://laravel.com/docs/5.6/helpers#method-str-uuid.

Thanks!

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/83, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuG1Gc7udIurk9J2DGLAzwzhyVlbHks5uJcN2gaJpZM4Va6gW
.

>

Ondřej Mirtes

Well, the method may return Uuid instance, one that confirms to UuidInterface, but if it declared Uuid as return type, Ramsey could never change the underlying class without breaking other's code.

If he now decided to change the underlying class, yet conforming to the UuidInterface, the Laravel's Str::uuid() wouldn't break. Whereas if Ramsey decided to return some new 'UuidValue' class instance, if Str::uuid() declared returning an Uuid instance, it would break.

PHPStan can (through a dynamic return type extension) say that the method always returns Uuid, without changing code of the library.

Thanks for replying Ondrej. I'm not sure we understand each other.

What I'm saying is:

  • Uuid implements UuidInterface
  • UuidInterface has a toString method with a string return type hint.
  • Laravel's Str::uuid() correctly returns an instance of a class that implements the UuidInterface contract, instead of the concrete class implementation Uuid.

I titled the issue report:

Cannot cast Ramsey\Uuid\UuidInterface to string.

which, I believe, is not true, because if the returned instance implements a toString method (and it does), it can be cast to string. Returning a contract (an interface), and not a concrete class, is a well known and widely used programming technique. There's ton of blog posts explaining why you should code to an interface.

What are you saying exactly?

It’s a difference between having toString and __toString (which isn’t on
the interface).

On Mon, 23 Jul 2018 at 15:23, Michał Karnicki notifications@github.com
wrote:

Thanks for replying Ondrej. I'm not sure we understand each other.

What I'm saying is:

I titled the issue report:

Cannot cast RamseyUuidUuidInterface to string.

which, I believe, is not true, because if the returned instance implements
a toString method (and it does), it can be cast to string. Returning a
contract (an interface), and not a concrete class, is a well known and
widely used programming technique. There's ton of blog posts explaining why
you should code to an interface.

What are you saying exactly?

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/nunomaduro/larastan/issues/83#issuecomment-407056076,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGZuH2oexSeEL_jTDUnvNxNurb_RFOWks5uJc40gaJpZM4Va6gW
.

>

Ondřej Mirtes

You're right. In that case, I'd say that's a bug in RamseyUuid package then, not larastan, just a different king of bug than what you initially suggested. I'll leave it up to Nuno to close this or keep around.

I don't think it's a bug in Uuid library itself - you just can't cast to string UuidInterface, it doesn't promise you that.

It's just a wrong assumption that everything that get's returned from Str::uuid can be cast to string which isn't right but isn't anybody's fault, really.

You're right. The interface has a toString (non-magic) method that should be used instead. Thanks for teaching me something today.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fico7489 picture fico7489  Â·  3Comments

gitetsu picture gitetsu  Â·  4Comments

mariomka picture mariomka  Â·  4Comments

bogdankharchenko picture bogdankharchenko  Â·  4Comments

danielcosta picture danielcosta  Â·  4Comments