Skip to content

Commit

Permalink
UniqueExtractor: extracting from array logic fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Jan 14, 2023
1 parent 92b8803 commit 30e03d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/UniqueExtractor.php
Expand Up @@ -36,7 +36,7 @@ public static function getString($var, bool $strict): string
{
switch (true) {
case is_array($var):
return 'array_'.md5(serialize($var));
return 'array_'.serialize($var);
case is_resource($var):
preg_match('/#([0-9]+)$/', (string)$var, $matches);
return 'resource_'.$matches[1];
Expand All @@ -45,7 +45,7 @@ public static function getString($var, bool $strict): string
case $var instanceof Closure:
return 'closure_'.spl_object_id($var);
case is_object($var):
return 'object_'.($strict ? spl_object_id($var) : md5(serialize($var)));
return 'object_'.($strict ? spl_object_id($var) : serialize($var));
case gettype($var) === 'boolean':
return 'boolean_'.(int)$var;
case $strict:
Expand Down
52 changes: 26 additions & 26 deletions tests/unit/UniqueExtractor/NonStrictTest.php
Expand Up @@ -68,23 +68,23 @@ public function dataProviderForStringNonStrict(): array
['Abc', 'scalar_Abc'],

// arrays
[[], 'array_'.md5(serialize([]))],
[[0], 'array_'.md5(serialize([0]))],
[[1], 'array_'.md5(serialize([1]))],
[[1, 2, 3], 'array_'.md5(serialize([1, 2, 3]))],
[[1, 2, '3'], 'array_'.md5(serialize([1, 2, '3']))],
[['a', 'b', 'c'], 'array_'.md5(serialize(['a', 'b', 'c']))],
[['a' => [1, 2, 3], [4, 5, 6]], 'array_'.md5(serialize(['a' => [1, 2, 3], [4, 5, 6]]))],
[[], 'array_'.serialize([])],
[[0], 'array_'.serialize([0])],
[[1], 'array_'.serialize([1])],
[[1, 2, 3], 'array_'.serialize([1, 2, 3])],
[[1, 2, '3'], 'array_'.serialize([1, 2, '3'])],
[['a', 'b', 'c'], 'array_'.serialize(['a', 'b', 'c'])],
[['a' => [1, 2, 3], [4, 5, 6]], 'array_'.serialize(['a' => [1, 2, 3], [4, 5, 6]])],

// stdClass objects
[(object)[], 'object_'.md5(serialize((object)[]))],
[(object)[1, 2, 3], 'object_'.md5(serialize((object)[1, 2, 3]))],
[(object)['a' => 1], 'object_'.md5(serialize((object)['a' => 1]))],
[(object)['a' => [1, 2, 3]], 'object_'.md5(serialize((object)['a' => [1, 2, 3]]))],
[(object)[], 'object_'.serialize((object)[])],
[(object)[1, 2, 3], 'object_'.serialize((object)[1, 2, 3])],
[(object)['a' => 1], 'object_'.serialize((object)['a' => 1])],
[(object)['a' => [1, 2, 3]], 'object_'.serialize((object)['a' => [1, 2, 3]])],

// another objects
[new SerializableFixture(1), 'object_'.md5(serialize(new SerializableFixture(1)))],
[new SerializableFixture(2), 'object_'.md5(serialize(new SerializableFixture(2)))],
[new SerializableFixture(1), 'object_'.serialize(new SerializableFixture(1))],
[new SerializableFixture(2), 'object_'.serialize(new SerializableFixture(2))],

// closures
[$func = function() {}, 'closure_'.spl_object_id($func)],
Expand Down Expand Up @@ -158,23 +158,23 @@ public function dataProviderForHashNonStrict(): array
['Abc', md5('scalar_Abc')],

// arrays
[[], md5('array_'.md5(serialize([])))],
[[0], md5('array_'.md5(serialize([0])))],
[[1], md5('array_'.md5(serialize([1])))],
[[1, 2, 3], md5('array_'.md5(serialize([1, 2, 3])))],
[[1, 2, '3'], md5('array_'.md5(serialize([1, 2, '3'])))],
[['a', 'b', 'c'], md5('array_'.md5(serialize(['a', 'b', 'c'])))],
[['a' => [1, 2, 3], [4, 5, 6]], md5('array_'.md5(serialize(['a' => [1, 2, 3], [4, 5, 6]])))],
[[], md5('array_'.serialize([]))],
[[0], md5('array_'.serialize([0]))],
[[1], md5('array_'.serialize([1]))],
[[1, 2, 3], md5('array_'.serialize([1, 2, 3]))],
[[1, 2, '3'], md5('array_'.serialize([1, 2, '3']))],
[['a', 'b', 'c'], md5('array_'.serialize(['a', 'b', 'c']))],
[['a' => [1, 2, 3], [4, 5, 6]], md5('array_'.serialize(['a' => [1, 2, 3], [4, 5, 6]]))],

// stdClass objects
[(object)[], md5('object_'.md5(serialize((object)[])))],
[(object)[1, 2, 3], md5('object_'.md5(serialize((object)[1, 2, 3])))],
[(object)['a' => 1], md5('object_'.md5(serialize((object)['a' => 1])))],
[(object)['a' => [1, 2, 3]], md5('object_'.md5(serialize((object)['a' => [1, 2, 3]])))],
[(object)[], md5('object_'.serialize((object)[]))],
[(object)[1, 2, 3], md5('object_'.serialize((object)[1, 2, 3]))],
[(object)['a' => 1], md5('object_'.serialize((object)['a' => 1]))],
[(object)['a' => [1, 2, 3]], md5('object_'.serialize((object)['a' => [1, 2, 3]]))],

// another objects
[new SerializableFixture(1), md5('object_'.md5(serialize(new SerializableFixture(1))))],
[new SerializableFixture(2), md5('object_'.md5(serialize(new SerializableFixture(2))))],
[new SerializableFixture(1), md5('object_'.serialize(new SerializableFixture(1)))],
[new SerializableFixture(2), md5('object_'.serialize(new SerializableFixture(2)))],

// closures
[$func = function() {}, md5('closure_'.spl_object_id($func))],
Expand Down
28 changes: 14 additions & 14 deletions tests/unit/UniqueExtractor/StrictTest.php
Expand Up @@ -68,13 +68,13 @@ public function dataProviderForString(): array
['Abc', 'string_Abc'],

// arrays
[[], 'array_'.md5(serialize([]))],
[[0], 'array_'.md5(serialize([0]))],
[[1], 'array_'.md5(serialize([1]))],
[[1, 2, 3], 'array_'.md5(serialize([1, 2, 3]))],
[[1, 2, '3'], 'array_'.md5(serialize([1, 2, '3']))],
[['a', 'b', 'c'], 'array_'.md5(serialize(['a', 'b', 'c']))],
[['a' => [1, 2, 3], [4, 5, 6]], 'array_'.md5(serialize(['a' => [1, 2, 3], [4, 5, 6]]))],
[[], 'array_'.serialize([])],
[[0], 'array_'.serialize([0])],
[[1], 'array_'.serialize([1])],
[[1, 2, 3], 'array_'.serialize([1, 2, 3])],
[[1, 2, '3'], 'array_'.serialize([1, 2, '3'])],
[['a', 'b', 'c'], 'array_'.serialize(['a', 'b', 'c'])],
[['a' => [1, 2, 3], [4, 5, 6]], 'array_'.serialize(['a' => [1, 2, 3], [4, 5, 6]])],

// stdClass objects
[$obj = (object)[], 'object_'.spl_object_id($obj)],
Expand Down Expand Up @@ -158,13 +158,13 @@ public function dataProviderForHash(): array
['Abc', md5('string_Abc')],

// arrays
[[], md5('array_'.md5(serialize([])))],
[[0], md5('array_'.md5(serialize([0])))],
[[1], md5('array_'.md5(serialize([1])))],
[[1, 2, 3], md5('array_'.md5(serialize([1, 2, 3])))],
[[1, 2, '3'], md5('array_'.md5(serialize([1, 2, '3'])))],
[['a', 'b', 'c'], md5('array_'.md5(serialize(['a', 'b', 'c'])))],
[['a' => [1, 2, 3], [4, 5, 6]], md5('array_'.md5(serialize(['a' => [1, 2, 3], [4, 5, 6]])))],
[[], md5('array_'.serialize([]))],
[[0], md5('array_'.serialize([0]))],
[[1], md5('array_'.serialize([1]))],
[[1, 2, 3], md5('array_'.serialize([1, 2, 3]))],
[[1, 2, '3'], md5('array_'.serialize([1, 2, '3']))],
[['a', 'b', 'c'], md5('array_'.serialize(['a', 'b', 'c']))],
[['a' => [1, 2, 3], [4, 5, 6]], md5('array_'.serialize(['a' => [1, 2, 3], [4, 5, 6]]))],

// stdClass objects
[$obj = (object)[], md5('object_'.spl_object_id($obj))],
Expand Down

0 comments on commit 30e03d0

Please sign in to comment.