Skip to content

[7.2] Add polyfill for spl_object_id() #97

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

Merged
merged 1 commit into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Polyfills are provided for:
- the `is_iterable` function introduced in PHP 7.1;
- a `Binary` utility class to be used when compatibility with
`mbstring.func_overload` is required;
- the `stream_isatty` function introduced in PHP 7.2;
- the `spl_object_id` and `stream_isatty` functions introduced in PHP 7.2;
- the `sapi_windows_vt100_support` function (Windows only) introduced in PHP 7.2;
- the `PHP_OS_FAMILY` constant introduced in PHP 7.2.

Expand Down
36 changes: 36 additions & 0 deletions src/Php72/Php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
final class Php72
{
private static $hashMask;

public static function utf8_encode($s)
{
$s .= $s;
Expand Down Expand Up @@ -80,4 +82,38 @@ public static function php_os_family()

return isset($map[PHP_OS]) ? $map[PHP_OS] : 'Unknown';
}

public static function spl_object_id($object)
{
if (null === self::$hashMask) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use an early return here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure it would help, L108 and L111 are not the same, so that would require duplicating L111, is that what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually hashMask is calculated without usage of input param here$object. maybe extract it to separated function and then

public static function spl_object_id($object)
{
    return self::getHashMask() ^ hexdec(substr(spl_object_hash($object), self::$hashOffset, PHP_INT_SIZE));
}

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer raw perf, thus prevent an extra call here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we are, init logic split in separate function, called only once

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

self::initHashMask();
}
if (null === $hash = spl_object_hash($object)) {
return;
}

return self::$hashMask ^ hexdec(substr($hash, 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
}

private static function initHashMask()
{
$obj = (object) array();
self::$hashMask = -1;

// check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
$obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
foreach (debug_backtrace(\PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS : false) as $frame) {
if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
$frame['line'] = 0;
break;
}
}
if (!empty($frame['line'])) {
ob_start();
debug_zval_dump($obj);
self::$hashMask = (int) substr(ob_get_clean(), 17);
}

self::$hashMask ^= hexdec(substr(spl_object_hash($obj), 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
}
}
1 change: 1 addition & 0 deletions src/Php72/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Symfony Polyfill / Php72

This component provides functions added to PHP 7.2 core:

- [`spl_object_id`](https://php.net/spl_object_id)
- [`stream_isatty`](https://php.net/stream_isatty)

On Windows only:
Expand Down
3 changes: 3 additions & 0 deletions src/Php72/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function stream_isatty($stream) { return function_exists('posix_isatty') && @pos
function utf8_encode($s) { return p\Php72::utf8_encode($s); }
function utf8_decode($s) { return p\Php72::utf8_decode($s); }
}
if (!function_exists('spl_object_id')) {
function spl_object_id($s) { return p\Php72::spl_object_id($s); }
}
if (!defined('PHP_OS_FAMILY')) {
define('PHP_OS_FAMILY', p\Php72::php_os_family());
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Php72/Php72Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ public function testPhpOsFamily()
$this->assertTrue(defined('PHP_OS_FAMILY'));
$this->assertSame(PHP_OS_FAMILY, p::php_os_family());
}

/**
* @covers Symfony\Polyfill\Php72\Php72::spl_object_id
*/
public function testSplObjectId()
{
$obj = new \stdClass();
$id = spl_object_id($obj);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please test that calling spl_object_id for non-object works the same as native function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ob_start();
var_dump($obj);
$dump = ob_get_clean();

$this->assertStringStartsWith("object(stdClass)#$id ", $dump);

$this->assertNull(@spl_object_id(123));
}
}