Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interesting bug #42

Open
ghost opened this issue Aug 28, 2017 · 7 comments
Open

Interesting bug #42

ghost opened this issue Aug 28, 2017 · 7 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Aug 28, 2017

I just came across an interesting bug I thought you might like.

I have a Set that is being created using an array like this pseudo code

foreach ($users as &$userId) {
    $userId = UserId::fromString($userId->user_uuid);
}

$result = new Set($users);

UserId implements Hashable.

When I come to add another UserId to the Set, even if the hash functions return the same value, and the equals functions return true, the new UserId is added to the Set.

Set after loop and before add:

class Ds\Set#41 (3) {
  public ${0} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#40 (1) {
    private $identity =>
    string(36) "4f220a85-1b2f-40f6-91c8-5879a8331c9f"
  }
  public ${1} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#42 (1) {
    private $identity =>
    string(36) "5e006610-5ed4-4393-a6e7-57857ac5deec"
  }
  public ${2} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#43 (1) {
    private $identity =>
    string(36) "b6f23619-dfca-423f-b598-3878a30abbc1"
  }
}

Set after loop and after add

class Ds\Set#41 (4) {
  public ${0} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#40 (1) {
    private $identity =>
    string(36) "4f220a85-1b2f-40f6-91c8-5879a8331c9f"
  }
  public ${1} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#42 (1) {
    private $identity =>
    string(36) "5e006610-5ed4-4393-a6e7-57857ac5deec"
  }
  public ${2} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#43 (1) {
    private $identity =>
    string(36) "b6f23619-dfca-423f-b598-3878a30abbc1"
  }
  public ${3} =>
  class ThoughtRiver\Review\Domain\Model\User\UserId#32 (1) {
    private $identity =>
    string(36) "5e006610-5ed4-4393-a6e7-57857ac5deec"
  }
}

Notice indexes 1 and 3

If I change the loop to the following:

$result = new Set;

foreach ($users as $userId) {
    $result->add(UserId::fromString($userId->user_uuid));
}

Then I cannot add a new UserId to the Set when the Set already contains an object that equals considers to be true.

How weird?

@rtheunissen
Copy link
Member

So strange. 🤔

I'll take a look asap. Thanks for the report. :)

@rtheunissen
Copy link
Member

I have a feeling it's because we're not dereferencing consistently. I don't think it's add vs constructor, but the fact that you're using references in the first case.

What's your code for hash and equals?

@ghost
Copy link
Author

ghost commented Aug 29, 2017

It will definitely be because of the references.

    public function equals($obj): bool
    {
        return get_class($this) === get_class($obj)
            && $this->hash() === $obj->hash();
    }

    public function hash()
    {
        return (string) $this;
    }

    public function __toString(): string
    {
        return (string) $this->identity;
    }

Just a simple set of functions as it's a uuid.

@rtheunissen
Copy link
Member

I think this has now been fixed in the extension?

@ghost
Copy link
Author

ghost commented Dec 21, 2017

Great if it has! I will try to reproduce this (the code has moved on since I logged it).

@designermonkey
Copy link

I know it's been quite a while, sorry I didn't get to this earlier.

I've just done an overhaul of our tests:

  • Run on a system using the polyfill: Everything is ok.
  • Run on a system using the extension: Same result as above.

To save on work, I've changed the code to not use references as described above. I thought you would want to know that there is still a references issue lurking in there somewhere.

@rtheunissen
Copy link
Member

Thanks @designermonkey, I'll try to add a failing test for this.

@rtheunissen rtheunissen self-assigned this Sep 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants