Skip to main content
replaced http://us3.php.net with https://www.php.net
Source Link

is not correct. offsetSet does not return a valueoffsetSet does not return a value, so this won't work. It's not clear what you should do instead. Throwing an exception is one possibility.

is not correct. offsetSet does not return a value, so this won't work. It's not clear what you should do instead. Throwing an exception is one possibility.

is not correct. offsetSet does not return a value, so this won't work. It's not clear what you should do instead. Throwing an exception is one possibility.

Updates based on comments; plus minor tweaks noticed as I was reading through.
Source Link
Brythan
  • 7k
  • 3
  • 22
  • 37

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length$maximum_length as an integer. A comment would be helpful, but I wouldn't put it here. Instead use a helper function here.

    $added_count = 0;
    foreach ( $args as &$value )
    {
        if ( $this->count < $this->maximum_length )
        {
            $this->data[$this->count++] = sanitize$this->sanitize($value);
            $added_count++;
        }
        else
        {
            // when out of space, stop processing the arguments
            break;
        }
    }

    // returns the number of elements addedin the array (replicates array_push behaviour)
    return $added_count;$this->count;

I changed the return because the comment was lying to us. It was not in fact doing the same thing as array_push. With the changes, it does.

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length as an integer. A comment would be helpful, but I wouldn't put it here. Instead use a helper function here.

    $added_count = 0;
    foreach ( $args as &$value )
    {
        if ( $this->count < $this->maximum_length )
        {
            $this->data[$this->count++] = sanitize($value);
            $added_count++;
        }
        else
        {
            // when out of space, stop processing the arguments
            break;
        }
    }

    //returns the number of elements added (replicates array_push behaviour)
    return $added_count;

I changed the return because the comment was lying to us. It was not in fact doing the same thing as array_push. With the changes, it does.

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length as an integer. A comment would be helpful, but I wouldn't put it here. Instead use a helper function here.

    foreach ( $args as &$value )
    {
        if ( $this->count < $this->maximum_length )
        {
            $this->data[$this->count++] = $this->sanitize($value);
        }
        else
        {
            // when out of space, stop processing arguments
            break;
        }
    }

    // returns the number of elements in the array (replicates array_push behaviour)
    return $this->count;
Bounty Awarded with 50 reputation awarded by Ismael Miguel
Update to reflect explanation of why >>0 is better than (int).
Source Link
Brythan
  • 7k
  • 3
  • 22
  • 37

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length as an integer. If that's A comment would be helpful, but I wouldn't put it then why nothere. Instead use a helper function here.

    $this->maximum_length = UIntArray::make_int32(int)$maximum_length > 0 ? (int)$maximum_length >> 0 : 1;1);

which says what it is doing explicitly rather than relying on a side effect?. Also, I put it all the way around, as you want to force it to be a 32 bit integer all the time, not just when the value is not the default.

private static function make_int32($value)
{
    // Doing a shift forces PHP to treat the value as a 32 bit integer.
    // Shifting by 0 means that this is otherwise a no-op.  
    return $value >> 0;
}
    if ( 0 > $offset || ( UIntArray::make_int32(int)$offset) >= $this->maximum_length ) )
    // $uint_array[] = <value>; will set the $offset to null
    if ( is_null($offset) )
    {
        if ( $this->count >= $this->maximum_length )
        {
            // panic somehow and do not proceed
        }

        $offset = $this->count++;
    }

    $this->data[>data[UIntArray::make_int32(int$offset)$offset]] = $this->sanitize($value);
    $offset = UIntArray::make_int32($offset);
    $dummy = isset( $this->data[(int)$offset]>data[$offset])
        ? $this->data[(int)$offset]>data[$offset]
        : null;
    return $dummy;

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length as an integer. If that's it then why not

    $this->maximum_length = (int)$maximum_length > 0 ? (int)$maximum_length >> 0 : 1;

which says what it is doing explicitly rather than relying on a side effect?

    if ( 0 > $offset || ( (int)$offset >= $this->maximum_length ) )
    // $uint_array[] = <value>; will set the $offset to null
    if ( is_null($offset) )
    {
        if ( $this->count >= $this->maximum_length )
        {
            // panic somehow and do not proceed
        }

        $offset = $this->count++;
    }

    $this->data[(int)$offset] = $this->sanitize($value);
    $dummy = isset( $this->data[(int)$offset])
        ? $this->data[(int)$offset]
        : null;
    return $dummy;

I removed your comment, as that just restates what I can read from the code. I'd still like a comment here though. It's unclear to me why you are shifting the bits right by zero places. The only reason that I can see is that it forces PHP to treat $maximum_length as an integer. A comment would be helpful, but I wouldn't put it here. Instead use a helper function here.

    $this->maximum_length = UIntArray::make_int32($maximum_length > 0 ? $maximum_length : 1);

which says what it is doing explicitly rather than relying on a side effect. Also, I put it all the way around, as you want to force it to be a 32 bit integer all the time, not just when the value is not the default.

private static function make_int32($value)
{
    // Doing a shift forces PHP to treat the value as a 32 bit integer.
    // Shifting by 0 means that this is otherwise a no-op.  
    return $value >> 0;
}
    if ( 0 > $offset || ( UIntArray::make_int32($offset) >= $this->maximum_length ) )
    // $uint_array[] = <value>; will set the $offset to null
    if ( is_null($offset) )
    {
        if ( $this->count >= $this->maximum_length )
        {
            // panic somehow and do not proceed
        }

        $offset = $this->count++;
    }

    $this->data[UIntArray::make_int32($offset)] = $this->sanitize($value);
    $offset = UIntArray::make_int32($offset);
    $dummy = isset( $this->data[$offset])
        ? $this->data[$offset]
        : null;
    return $dummy;
Source Link
Brythan
  • 7k
  • 3
  • 22
  • 37
Loading