genkgo / mail

Library to send e-mails over different transports and protocols (like SMTP and IMAP) using immutable messages and streams. Also includes SMTP server.
https://mail.readthedocs.io/
Other
402 stars 21 forks source link

Possible error in EmailAddress #6

Closed Zolli closed 7 years ago

Zolli commented 7 years ago

While I looking at the code I ran into the EmailAddress::equals() method. The method expect an EmailAddress as first parameter and use this to read the property called address ($address->address), but this property has private visibility. Probably submit a PR for that.

frederikbosch commented 7 years ago

@Zolli In PHP instances of the same class can access their private properties. This is a feature of PHP, not a bug.

So.

<?php
class MyObject {
  private $property = 'x';
  public function equals(MyObject $object) {
    // because $object and $this are of the same class (MyObject)
    // the property can be accessed even though it is private
    // this is because php only prohibits other *classes* - and thus not instances - to access private properties
    return $this->property === $object->property;
  }
}
Zolli commented 7 years ago

Oh, sorry about that. I dont use this feature very often.

frederikbosch commented 7 years ago

No problem. You might also have a look at the tests. This functionality is tested here.

Otherwise, I might also recommend reading upon value objects, or specific for PHP. In this case EmailAddresss is a value object. One of the characteristics of a value object is that their equality is based on the value, not on identity.

So.

$address1 = new EmailAddress('test@test.com');
$address2 = new EmailAddress('test@test.com');
$address3 = new EmailAddress('xxx@test.com');

$address1->equals($address2); // true
$address1->equals($address3); // false

This is different from entities and models. For models and entities the equality is based on the id of the object.

$user1 = new User(['id' => 1]);
$user2 = new User(['id' => 2]);
$user3 = new User(['id' => 3]);

$user1->equals($user2); // true
$user1->equals($user3); // false

Hope it helps understanding the concept! It is really valuable (!)

Zolli commented 7 years ago

@frederikbosch, I understand the concept, only those private access from the same type behavior not. Thanks for write this down. This could be very useful.