Skip to content

Commit

Permalink
Add get() and __get() methods to OD_URL_Metric
Browse files Browse the repository at this point in the history
Co-authored-by: felixarntz <flixos90@git.wordpress.org>
  • Loading branch information
westonruter and felixarntz committed Aug 21, 2024
1 parent dfaff4f commit 10865a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
36 changes: 36 additions & 0 deletions plugins/optimization-detective/class-od-url-metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions plugins/optimization-detective/tests/test-class-od-url-metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'] ) );
Expand Down

0 comments on commit 10865a7

Please sign in to comment.