Debian Codegraph @

tests/phpunit/DynamicPropertyTestHelper.php

raw symbols SCIP
1 <?php
2 declare( strict_types = 1 );
3
4 use Wikimedia\Assert\Assert;
5
6 // We want to allow dynamic property creation on any object.
7 // phpcs:disable MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam
8
9 /**
10 * Utility class for dealing with dynamic property creation in tests.
11 *
12 * PHP 8.2 has deprecated dynamic property creation for objects not explicitly annotated with #[AllowDynamicProperties].
13 * The recommended migration path for associating arbitrary data with objects is WeakMap, which is only available starting from PHP 8.0.
14 * Whilst MediaWiki still supported PHP 7.4, this required a compatibility layer for dynamic property creation on classes
15 * that do not support it, by utilizing WeakMap when available and falling back to regular dynamic property creation
16 * on PHP 7.4.
17 *
18 * This class can be removed and its usages converted into direct WeakMap usage now that MediaWiki only supports PHP 8.0 and above.
19 *
20 * @since 1.42 (also backported to 1.39.8, 1.40.4 and 1.41.2)
21 * @internal Only for use by core PHPUnit setup functionality.
22 */
23 class DynamicPropertyTestHelper {
24 /**
25 * Associative array of WeakMaps holding dynamic properties keyed by property name.
26 * @var WeakMap[]
27 */
28 private static $properties = [];
29
30 /**
31 * Get the value of the given dynamic property from the given object.
32 * @param object $owner Object to fetch the dynamic property for
33 * @param string $propName Name of the dynamic property to get
34 * @return mixed The value of the dynamic property, or null if not set
35 */
36 public static function getDynamicProperty( object $owner, string $propName ) {
37 if ( class_exists( WeakMap::class ) ) {
38 self::$properties[$propName] ??= new WeakMap();
39 return self::$properties[$propName][$owner] ?? null;
40 }
41
42 return $owner->$propName ?? null;
43 }
44
45 /**
46 * Set the given dynamic property to the given value on an object.
47 * @param object $owner Object to set the dynamic property on
48 * @param string $propName Name of the dynamic property to set
49 * @param mixed $value The property value to set; must not be null
50 * @return void
51 */
52 public static function setDynamicProperty( object $owner, string $propName, $value ): void {
53 // getDynamicProperty() uses a null return value as an idiom for "property does not exist",
54 // which precludes supporting null values for dynamic properties without a separate method
55 // to check for the existence of a possibly nullable property.
56 // Since existing test cases do not seem to extensively rely on setting null values,
57 // explicitly forbid them here.
58 Assert::parameter( $value !== null, '$value', 'must not be null' );
59
60 if ( class_exists( WeakMap::class ) ) {
61 self::$properties[$propName] ??= new WeakMap();
62 self::$properties[$propName][$owner] = $value;
63 } else {
64 $owner->$propName = $value;
65 }
66 }
67
68 /**
69 * Unset the given dynamic property on the given object.
70 * @param object $owner Object to unset the dynamic property on
71 * @param string $propName Name of the dynamic property to unset
72 * @return void
73 */
74 public static function unsetDynamicProperty( object $owner, string $propName ): void {
75 if ( class_exists( WeakMap::class ) ) {
76 self::$properties[$propName] ??= new WeakMap();
77 unset( self::$properties[$propName][$owner] );
78 } else {
79 unset( $owner->$propName );
80 }
81 }
82
83 }
84
85 // phpcs:enable