-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ext/spl: Throw TypeError when overloaded SplObjectStorage::getHash() …
…method does not return a string
- Loading branch information
Showing
2 changed files
with
48 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 36 additions & 32 deletions
68
ext/spl/tests/SplObjectStorage/SplObjectStorage_getHash.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,63 @@ | ||
--TEST-- | ||
SplObjectStorage::getHash implementation | ||
SplObjectStorage::getHash() implementation | ||
--FILE-- | ||
<?php | ||
$s = new SplObjectStorage(); | ||
$o1 = new Stdclass; | ||
$o2 = new Stdclass; | ||
$s[$o1] = "some_value\n"; | ||
echo $s->offsetGet($o1); | ||
|
||
class MySplObjectStorage extends SplObjectStorage { | ||
class MySplObjectStorage1 extends SplObjectStorage { | ||
#[ReturnTypeWillChange] | ||
public function getHash($obj) { | ||
return 2; | ||
} | ||
} | ||
|
||
try { | ||
$s1 = new MySplObjectStorage; | ||
$s1[$o1] = "foo"; | ||
} catch(Exception $e) { | ||
echo "caught 1\n"; | ||
} | ||
|
||
class MySplObjectStorage2 extends SplObjectStorage { | ||
public function getHash($obj): string { | ||
throw new Exception("foo"); | ||
return "asd"; | ||
} | ||
} | ||
|
||
try { | ||
$s2 = new MySplObjectStorage2; | ||
$s2[$o2] = "foo"; | ||
} catch(Exception $e) { | ||
echo "caught 2\n"; | ||
} | ||
|
||
class MySplObjectStorage3 extends SplObjectStorage { | ||
public function getHash($obj): string { | ||
return "asd"; | ||
} | ||
} | ||
|
||
$s3 = new MySplObjectStorage3; | ||
$s3[$o1] = $o1; | ||
var_dump($s3[$o1]); | ||
$s3[$o2] = $o2; | ||
|
||
var_dump($s3[$o1] === $s3[$o2]); | ||
$s = new SplObjectStorage(); | ||
$o1 = new stdClass(); | ||
$o2 = new stdClass(); | ||
|
||
$instances = [ | ||
new SplObjectStorage(), | ||
new MySplObjectStorage1(), | ||
new MySplObjectStorage2(), | ||
new MySplObjectStorage3(), | ||
]; | ||
|
||
foreach ($instances as $instance) { | ||
echo 'Instance as ', $instance::class, PHP_EOL; | ||
try { | ||
$instance[$o1] = 'foo'; | ||
var_dump($instance->offsetGet($o1)); | ||
var_dump($instance[$o1]); | ||
$instance[$o2] = $o2; | ||
var_dump($instance[$o1] === $instance[$o2]); | ||
} catch(Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
some_value | ||
caught 1 | ||
caught 2 | ||
object(stdClass)#2 (0) { | ||
} | ||
Instance as SplObjectStorage | ||
string(3) "foo" | ||
string(3) "foo" | ||
bool(false) | ||
Instance as MySplObjectStorage1 | ||
TypeError: MySplObjectStorage1::getHash(): Return value must be of type string, int returned | ||
Instance as MySplObjectStorage2 | ||
Exception: foo | ||
Instance as MySplObjectStorage3 | ||
string(3) "foo" | ||
string(3) "foo" | ||
bool(true) |