Skip to content
This repository

SplObjectStorage #327

Closed
wants to merge 5 commits into from

2 participants

Artur Kotyrba Paul Tarjan
Artur Kotyrba
udat commented April 25, 2011

This PR contains the implementations of several new SplObjectStorage methods and fixes SplObjectStorage::rewind method.

DocBlock comments should be regenerated using idl/sysdoc.php file.

###Details

SplObjectStorage::rewind used rewind instead of reset function.

Commit fdf99c7 implements addAll, removeAll, removeAllExcept, offsetGet, offsetExists, offsetSet, offsetUnset methods of SplObjectStorage class and adds optional $data parameter to attach method.

added some commits April 25, 2011
Artur Kotyrba Fix SplObjectStorage implementation.
Test case:
  <?php

    $storage = new SplObjectStorage();
    $objects = array();

    for ($i=0; $i<5; ++$i) {
      $objects[] = new StdClass;
    }
    for ($i=0; $i<5; ++$i) {
      $storage->attach($objects[$i]);
    }

    $storage->next();

    for ($i=0; $i<4; ++$i) {
      $storage->detach($objects[$i]);
    }

    echo $storage->key();
    ?>

PHP result:
    0

HPHP result (before patch):
    HipHop Notice:  In PHP, mixing up foreach() and functional style array iteration by calling current(), each(), key(), value(), prev(), next() may lead to undefined behavior. In HipHop, this problem is
    less severe, but to avoid inconsistency between PHP and HipHop, please call reset() or end() after foreach and before calling any of those array iteration functions.
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    1

HPHP result (after patch):
    HipHop Notice:  In PHP, mixing up foreach() and functional style array iteration by calling current(), each(), key(), value(), prev(), next() may lead to undefined behavior. In HipHop, this problem is
    less severe, but to avoid inconsistency between PHP and HipHop, please call reset() or end() after foreach and before calling any of those array iteration functions.
    1
ceaabec
Artur Kotyrba Implement addAll, removeAll, removeAllExcept, offsetGet, offsetExists…
…, offsetSet, offsetUnset methods of SplObjectStorage class.

Test case:

  <?php
    namespace test;

    class D extends \SplObjectStorage {
      public function clear() {
       $this->removeAll($this);
      }
    }

    $o1 = new \StdClass;
    $o2 = new \StdClass;
    $a = new D();

    $a[$o1] = "foo";
    var_dump(count($a));

    $a->clear();
    var_dump(count($a));

    $a[$o1] = "foo";

    $b = new D();
    $b[$o1] = "bar";
    $b[$o2] = "gee";

    var_dump(count($b));
    $b->removeAll($a);
    var_dump(count($b));

    var_dump($b[$o2]);
    $b->removeAllExcept($a);
    var_dump(count($b));
fdf99c7
Artur Kotyrba Simplify SplObjectStorage implementation. 112756b
Artur Kotyrba Revert accidental change in iterator.cpp f3faf4c
Artur Kotyrba Fix four bugs in SplObjectStorage and regenerate system.
1. SplObjectStorage::valid() - behaviour difference

  In PHP after call to SplObjectStorage::next(), which advance internal pointer after array last element, any subsequent call to SplObjectStorage::valid() should return false, even if
SplObjectStorage::attach was called. Also SplObjectStorage::valid() returns false until SplObjectStorage::rewind() is called on non-empty storage.

  For example:
   <?php
      $s = new SplObjectStorage();
      $s->rewind();
      $s->attach(new StdClass());
      var_dump($s->valid()); //should return false
      $s->rewind();
      var_dump($s->valid()); //should return true
      $s->next();
      var_dump($s->valid()); //should return false
      $s->attach(new StdClass());
      var_dump($s->valid()); //should return false

2. SplObjectStorage->index property wasn't reset to 0 after SplObjectStorage::rewind() is called
3. Call to SplObjectStorage::setInfo() added new element to $storage array if iterator wasn't pointing to valid object.
4. SplObjectStorage->valid method was comparing `current($this->storage)` return value to false instead of null.
5454e33
Paul Tarjan
Collaborator
ptarjan commented May 13, 2013

We're closing out all bugs older than 2 months. http://www.hiphop-php.com/wp/?p=575

If this is still an issue, please re-open it, and in order of goodness:

  1. Give detailed repro steps
  2. Write a test case in hphp/tests/quick (run it with hphp/tests/run) and send the pull request
  3. Fix it in a pull request
Paul Tarjan ptarjan closed this May 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 5 unique commits by 1 author.

Apr 25, 2011
Artur Kotyrba Fix SplObjectStorage implementation.
Test case:
  <?php

    $storage = new SplObjectStorage();
    $objects = array();

    for ($i=0; $i<5; ++$i) {
      $objects[] = new StdClass;
    }
    for ($i=0; $i<5; ++$i) {
      $storage->attach($objects[$i]);
    }

    $storage->next();

    for ($i=0; $i<4; ++$i) {
      $storage->detach($objects[$i]);
    }

    echo $storage->key();
    ?>

PHP result:
    0

HPHP result (before patch):
    HipHop Notice:  In PHP, mixing up foreach() and functional style array iteration by calling current(), each(), key(), value(), prev(), next() may lead to undefined behavior. In HipHop, this problem is
    less severe, but to avoid inconsistency between PHP and HipHop, please call reset() or end() after foreach and before calling any of those array iteration functions.
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    HipHop Warning:  Not a valid stream resource
    1

HPHP result (after patch):
    HipHop Notice:  In PHP, mixing up foreach() and functional style array iteration by calling current(), each(), key(), value(), prev(), next() may lead to undefined behavior. In HipHop, this problem is
    less severe, but to avoid inconsistency between PHP and HipHop, please call reset() or end() after foreach and before calling any of those array iteration functions.
    1
ceaabec
Artur Kotyrba Implement addAll, removeAll, removeAllExcept, offsetGet, offsetExists…
…, offsetSet, offsetUnset methods of SplObjectStorage class.

Test case:

  <?php
    namespace test;

    class D extends \SplObjectStorage {
      public function clear() {
       $this->removeAll($this);
      }
    }

    $o1 = new \StdClass;
    $o2 = new \StdClass;
    $a = new D();

    $a[$o1] = "foo";
    var_dump(count($a));

    $a->clear();
    var_dump(count($a));

    $a[$o1] = "foo";

    $b = new D();
    $b[$o1] = "bar";
    $b[$o2] = "gee";

    var_dump(count($b));
    $b->removeAll($a);
    var_dump(count($b));

    var_dump($b[$o2]);
    $b->removeAllExcept($a);
    var_dump(count($b));
fdf99c7
May 01, 2011
Artur Kotyrba Simplify SplObjectStorage implementation. 112756b
Artur Kotyrba Revert accidental change in iterator.cpp f3faf4c
Artur Kotyrba Fix four bugs in SplObjectStorage and regenerate system.
1. SplObjectStorage::valid() - behaviour difference

  In PHP after call to SplObjectStorage::next(), which advance internal pointer after array last element, any subsequent call to SplObjectStorage::valid() should return false, even if
SplObjectStorage::attach was called. Also SplObjectStorage::valid() returns false until SplObjectStorage::rewind() is called on non-empty storage.

  For example:
   <?php
      $s = new SplObjectStorage();
      $s->rewind();
      $s->attach(new StdClass());
      var_dump($s->valid()); //should return false
      $s->rewind();
      var_dump($s->valid()); //should return true
      $s->next();
      var_dump($s->valid()); //should return false
      $s->attach(new StdClass());
      var_dump($s->valid()); //should return false

2. SplObjectStorage->index property wasn't reset to 0 after SplObjectStorage::rewind() is called
3. Call to SplObjectStorage::setInfo() added new element to $storage array if iterator wasn't pointing to valid object.
4. SplObjectStorage->valid method was comparing `current($this->storage)` return value to false instead of null.
5454e33
Something went wrong with that request. Please try again.