diff --git a/plugins/optimization-detective/class-od-url-metric.php b/plugins/optimization-detective/class-od-url-metric.php index 9ed3ed74a5..2c7a5bc2d5 100644 --- a/plugins/optimization-detective/class-od-url-metric.php +++ b/plugins/optimization-detective/class-od-url-metric.php @@ -44,6 +44,12 @@ * elements: ElementData[] * } * + * @property-read string $uuid + * @property-read string $url + * @property-read float $timestamp + * @property-read ViewportRect $viewport + * @property-read ElementData[] $elements + * * @since 0.1.0 * @access private */ @@ -306,6 +312,36 @@ protected static function extend_schema_with_optional_properties( array $propert return $properties_schema; } + /** + * Gets property value for an arbitrary key. + * + * This is particularly useful in conjunction with the `od_url_metric_schema_root_additional_properties` filter. + * + * @since n.e.x.t + * @todo Instead of returning null when the key doesn't exist, should the `default` value be returned as defined in the schema? + * + * @param string $key Property. + * @return mixed|null + */ + public function get( string $key ) { + return $this->data[ $key ] ?? null; + } + + /** + * Gets property value for an arbitrary key. + * + * This is useful with the `@property-read` annotations for the class. For accessing other data, + * it's likely the `get()` method will be more useful for static analysis reasons. + * + * @since n.e.x.t + * + * @param string $key Property. + * @return mixed|null + */ + public function __get( string $key ) { + return $this->get( $key ); + } + /** * Gets UUID. * diff --git a/plugins/optimization-detective/tests/test-class-od-url-metric.php b/plugins/optimization-detective/tests/test-class-od-url-metric.php index dd74a2f8f8..7d842d2d46 100644 --- a/plugins/optimization-detective/tests/test-class-od-url-metric.php +++ b/plugins/optimization-detective/tests/test-class-od-url-metric.php @@ -152,6 +152,8 @@ public function data_provider(): array { /** * Tests construction. * + * @todo PHPStan complains when adding @covers tags for `::get()` and `::__get()` saying they are invalid method references. + * * @covers ::get_viewport * @covers ::get_viewport_width * @covers ::get_timestamp @@ -169,11 +171,29 @@ public function test_constructor( array $data, string $error = '' ): void { $this->expectExceptionMessage( $error ); } $url_metric = new OD_URL_Metric( $data ); + $this->assertSame( $data['viewport'], $url_metric->get_viewport() ); + $this->assertSame( $data['viewport'], $url_metric->get( 'viewport' ) ); + $this->assertSame( $data['viewport'], $url_metric->viewport ); $this->assertSame( $data['viewport']['width'], $url_metric->get_viewport_width() ); + $this->assertSame( $data['viewport']['width'], $url_metric->viewport['width'] ); + $this->assertSame( $data['timestamp'], $url_metric->get_timestamp() ); + $this->assertSame( $data['timestamp'], $url_metric->get( 'timestamp' ) ); + $this->assertSame( $data['timestamp'], $url_metric->timestamp ); + $this->assertSame( $data['elements'], $url_metric->get_elements() ); + $this->assertSame( $data['elements'], $url_metric->get( 'elements' ) ); + $this->assertSame( $data['elements'], $url_metric->elements ); + + $this->assertSame( $data['url'], $url_metric->get_url() ); + $this->assertSame( $data['url'], $url_metric->get( 'url' ) ); + $this->assertSame( $data['url'], $url_metric->url ); + $this->assertTrue( wp_is_uuid( $url_metric->get_uuid() ) ); + $this->assertSame( $url_metric->get_uuid(), $url_metric->uuid ); + $this->assertSame( $url_metric->get_uuid(), $url_metric->get( 'uuid' ) ); + $serialized = $url_metric->jsonSerialize(); if ( ! array_key_exists( 'uuid', $data ) ) { $this->assertTrue( wp_is_uuid( $serialized['uuid'] ) );