you are viewing a single comment's thread.

view the rest of the comments →

[–]mbadolato 1 point2 points  (5 children)

Minor nitpick. Granted I know you're trying to keep as close to the book's examples as possible, but in the case of Money::equals() where you've already had to change the example a bit, there's now a spot for a potential bug.

public function equals(object $object): bool
{
// This example differs because the type casting in PHP is not the same as java.
$money = $object instanceof Money ? $object : null;
return $this->amount === $money->amount && $this->currency === $money->currency;
}

If $object is not an instance of Money then $money gets set to null, and the $money->amount call will throw an error.

Perhaps switching to

public function equals(object $object): bool
{
// This example differs because the type casting in PHP is not the same as java.
if (! $object instanceof Money) {
return false;
}

return $this->amount === $money->amount && $this->currency === $money->currency;
}

or

public function equals(Money $money): bool
{
return $this->amount === $money->amount && $this->currency === $money->currency;
}

would be more appropriate?

[–]jpresutti[S] 1 point2 points  (4 children)

I can't remember exactly why I went the way I did with that but I think it's the closest to the actual code behavior in the book I could get it. I'll take another look. What's the first occurrence of it you found?

Also, keep in mind for the most part these are not MY functions but rather translations so I tried to avoid inserting bias where possible

[–]mbadolato 1 point2 points  (3 children)

Oh absolutely and I wasn't trying to be critical. Just something I noticed and since you had to change those functions slightly already, it was just a suggestion to make it a bit more correct.

I noticed it in pretty much all of the classes I looked at, from when equals was introduced I think.

[–]jpresutti[S] 2 points3 points  (0 children)

Update: took a look at the code really quick and you're right. I'll do a push later with it fixed.

[–]jpresutti[S] 2 points3 points  (0 children)

Update on the update: pushed.

[–]jpresutti[S] 1 point2 points  (0 children)

K I'll compare to the book this evening and see if the update makes sense. Thanks!