Skip to content

Commit 170776c

Browse files
seanleong-WMDEWMDE bot
authored andcommitted
Refactor: Use WeakMap in RestrictedEntityLookupFactory
Use WeakMap instead of the current array implementation in RestrictedEntityLookupFactory.php after we dropped PHP 7.4 support. Bug: T279069 Bug: T341957 Change-Id: I5141fb08f983d9be3ebddf85f61a9f357d94c0af
1 parent 32caf26 commit 170776c

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

src/Lookup/RestrictedEntityLookupFactory.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Wikibase\DataModel\Services\Lookup;
44

55
use MediaWiki\Parser\Parser;
6+
use WeakMap;
67

78
/**
89
* Factory class for creating RestrictedEntityLookup instances
@@ -13,9 +14,6 @@
1314
* This factory maintains a separate RestrictedEntityLookup instance
1415
* for each Parser, tracking entity access counts independently.
1516
*
16-
* Note: WeakMap should be used once we drop PHP 7.4 support. An
17-
* array implementation is used as of now.
18-
*
1917
* @license GPL-2.0-or-later
2018
* @author Sean Leong < sean.leong@wikimedia.de >
2119
*/
@@ -26,21 +24,19 @@ class RestrictedEntityLookupFactory {
2624
private int $entityAccessLimit;
2725

2826
/**
29-
* @var array<string, EntityLookup>
27+
* @var WeakMap<Parser, EntityLookup>
3028
*/
31-
private array $restrictedEntityLookupArray = [];
29+
private WeakMap $restrictedEntityLookupMap;
3230

3331
public function __construct( EntityLookup $entityLookup, int $entityAccessLimit ) {
3432
$this->entityLookup = $entityLookup;
3533
$this->entityAccessLimit = $entityAccessLimit;
34+
$this->restrictedEntityLookupMap = new WeakMap();
3635
}
3736

3837
public function getRestrictedEntityLookup( Parser $parser ): RestrictedEntityLookup {
39-
$id = spl_object_hash( $parser );
40-
if ( !isset( $this->restrictedEntityLookupArray[$id] ) ) {
41-
$this->restrictedEntityLookupArray[ $id ] = new RestrictedEntityLookup( $this->entityLookup, $this->entityAccessLimit );
42-
}
38+
$this->restrictedEntityLookupMap[$parser] ??= new RestrictedEntityLookup( $this->entityLookup, $this->entityAccessLimit );
4339

44-
return $this->restrictedEntityLookupArray[ $id ];
40+
return $this->restrictedEntityLookupMap[$parser];
4541
}
4642
}

0 commit comments

Comments
 (0)