Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 2f86f48

Browse files
committed
Turn lock into a sharing mechanism
1 parent 0b3d95d commit 2f86f48

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

infection.json.dist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@
1717
"@default": true,
1818
"ArrayItemRemoval": {
1919
"ignore": [
20-
"ReactParallel\\ObjectProxy\\Proxy::setUpHandlers::122"
20+
"ReactParallel\\ObjectProxy\\Proxy::setUpHandlers::132"
2121
]
2222
},
2323
"InstanceOf_": {
2424
"ignore": [
25-
"ReactParallel\\ObjectProxy\\Proxy::setUpHandlers::122"
25+
"ReactParallel\\ObjectProxy\\Proxy::setUpHandlers::132"
2626
]
2727
},
2828
"MethodCallRemoval": {
2929
"ignore": [
30-
"ReactParallel\\ObjectProxy\\Proxy::__destruct::117"
30+
"ReactParallel\\ObjectProxy\\Proxy::__destruct::127"
3131
]
3232
},
3333
"PublicVisibility": {
3434
"ignore": [
35-
"ReactParallel\\ObjectProxy\\Proxy::__destruct::109",
35+
"ReactParallel\\ObjectProxy\\Proxy::__destruct::119",
3636
"ReactParallel\\ObjectProxy\\NonExistentInterface::interface::22",
3737
"ReactParallel\\ObjectProxy\\Message\\Parcel::notify::27",
3838
"ReactParallel\\ObjectProxy\\Proxy\\Instance::interface::39"

src/Proxy.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ final class Proxy extends ProxyList
4141
/** @var array<string, Instance> */
4242
private array $instances = [];
4343

44+
/** @var array<string, string> */
45+
private array $shared = [];
46+
4447
/** @var array<string, string|false> */
4548
private array $detectedClasses = [];
4649

@@ -89,20 +92,27 @@ public function has(string $interface): bool
8992
return array_key_exists($interface, self::KNOWN_INTERFACE);
9093
}
9194

92-
public function create(object $object, string $interface, bool $locked = false): object
95+
public function create(object $object, string $interface, bool $share = false): object
9396
{
9497
if ($this->has($interface) === self::HASNT_PROXYABLE_INTERFACE) {
9598
throw NonExistentInterface::create($interface);
9699
}
97100

101+
if (array_key_exists($interface, $this->shared)) {
102+
return $this->instances[$this->shared[$interface]];
103+
}
104+
98105
/**
99106
* @psalm-suppress EmptyArrayAccess
100107
*/
101108
$class = self::KNOWN_INTERFACE[$interface];
102-
$instance = new Instance($object, $interface, $locked);
109+
$instance = new Instance($object, $interface, $share);
103110
$hash = $instance->class() . '___' . spl_object_hash($object);
104111

105112
$this->instances[$hash] = $instance;
113+
if ($share) {
114+
$this->shared[$interface] = $hash;
115+
}
106116

107117
if ($this->counterCreate instanceof Counters) {
108118
$this->counterCreate->counter(new Label('class', $this->instances[$hash]->class()), new Label('interface', $interface))->incr();

0 commit comments

Comments
 (0)