diff --git a/plugins/optimization-detective/class-od-url-metric.php b/plugins/optimization-detective/class-od-url-metric.php index 3c27631b44..a64db2f1dd 100644 --- a/plugins/optimization-detective/class-od-url-metric.php +++ b/plugins/optimization-detective/class-od-url-metric.php @@ -44,12 +44,6 @@ * 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 */ @@ -364,21 +358,6 @@ 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 The property value, or null if not set. - */ - public function __get( string $key ) { - return $this->get( $key ); - } - /** * Gets UUID. * diff --git a/plugins/optimization-detective/class-od-url-metrics-group-collection.php b/plugins/optimization-detective/class-od-url-metrics-group-collection.php index dd2306a73b..21bce9a0c5 100644 --- a/plugins/optimization-detective/class-od-url-metrics-group-collection.php +++ b/plugins/optimization-detective/class-od-url-metrics-group-collection.php @@ -193,7 +193,7 @@ private function create_groups(): array { */ public function add_url_metric( OD_URL_Metric $new_url_metric ): void { foreach ( $this->groups as $group ) { - if ( $group->is_viewport_width_in_range( $new_url_metric->viewport['width'] ) ) { + if ( $group->is_viewport_width_in_range( $new_url_metric->get_viewport_width() ) ) { $group->add_url_metric( $new_url_metric ); return; } @@ -416,7 +416,7 @@ public function get_all_element_max_intersection_ratios(): array { */ foreach ( $this->groups as $group ) { foreach ( $group as $url_metric ) { - foreach ( $url_metric->elements as $element ) { + foreach ( $url_metric->get_elements() as $element ) { $element_max_intersection_ratios[ $element['xpath'] ] = array_key_exists( $element['xpath'], $element_max_intersection_ratios ) ? max( $element_max_intersection_ratios[ $element['xpath'] ], $element['intersectionRatio'] ) : $element['intersectionRatio']; diff --git a/plugins/optimization-detective/class-od-url-metrics-group.php b/plugins/optimization-detective/class-od-url-metrics-group.php index 6eb364aa19..97e1bf7cc7 100644 --- a/plugins/optimization-detective/class-od-url-metrics-group.php +++ b/plugins/optimization-detective/class-od-url-metrics-group.php @@ -181,7 +181,7 @@ public function is_viewport_width_in_range( int $viewport_width ): bool { * @param OD_URL_Metric $url_metric URL metric. */ public function add_url_metric( OD_URL_Metric $url_metric ): void { - if ( ! $this->is_viewport_width_in_range( $url_metric->viewport['width'] ) ) { + if ( ! $this->is_viewport_width_in_range( $url_metric->get_viewport_width() ) ) { throw new InvalidArgumentException( esc_html__( 'URL metric is not in the viewport range for group.', 'optimization-detective' ) ); @@ -201,7 +201,7 @@ public function add_url_metric( OD_URL_Metric $url_metric ): void { usort( $this->url_metrics, static function ( OD_URL_Metric $a, OD_URL_Metric $b ): int { - return $b->timestamp <=> $a->timestamp; + return $b->get_timestamp() <=> $a->get_timestamp(); } ); @@ -229,7 +229,7 @@ public function is_complete(): bool { } $current_time = microtime( true ); foreach ( $this->url_metrics as $url_metric ) { - if ( $current_time > $url_metric->timestamp + $this->freshness_ttl ) { + if ( $current_time > $url_metric->get_timestamp() + $this->freshness_ttl ) { return false; } } @@ -283,7 +283,7 @@ public function get_lcp_element(): ?array { $breadcrumb_element = array(); foreach ( $this->url_metrics as $url_metric ) { - foreach ( $url_metric->elements as $element ) { + foreach ( $url_metric->get_elements() as $element ) { if ( ! $element['isLCP'] ) { continue; } diff --git a/plugins/optimization-detective/storage/class-od-url-metrics-post-type.php b/plugins/optimization-detective/storage/class-od-url-metrics-post-type.php index 8bfcccd096..43079459a1 100644 --- a/plugins/optimization-detective/storage/class-od-url-metrics-post-type.php +++ b/plugins/optimization-detective/storage/class-od-url-metrics-post-type.php @@ -200,7 +200,7 @@ public static function store_url_metric( string $slug, OD_URL_Metric $new_url_me // multiple URL Metric instances, each of which also contains the URL for which the metric was captured. The URL // appearing in the post title is therefore the most recent URL seen for the URL Metrics which have the same // normalized query vars among them. - 'post_title' => $new_url_metric->url, + 'post_title' => $new_url_metric->get_url(), ); $post = self::get_post( $slug ); @@ -221,7 +221,7 @@ public static function store_url_metric( string $slug, OD_URL_Metric $new_url_me ); try { - $group = $group_collection->get_group_for_viewport_width( $new_url_metric->viewport['width'] ); + $group = $group_collection->get_group_for_viewport_width( $new_url_metric->get_viewport_width() ); $group->add_url_metric( $new_url_metric ); } catch ( InvalidArgumentException $e ) { return new WP_Error( 'invalid_url_metric', $e->getMessage() ); 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 b3732b6ae7..0f9370a0b1 100644 --- a/plugins/optimization-detective/tests/test-class-od-url-metric.php +++ b/plugins/optimization-detective/tests/test-class-od-url-metric.php @@ -204,7 +204,7 @@ static function ( $value ) { * @covers ::get_elements * @covers ::jsonSerialize * @covers ::get - * @covers ::__get + * @covers ::get_json_schema * * @dataProvider data_provider_to_test_constructor * @@ -220,31 +220,25 @@ public function test_constructor( array $data, string $error = '' ): void { $this->assertSame( array_map( 'intval', $data['viewport'] ), $url_metric->get_viewport() ); $this->assertSame( array_map( 'intval', $data['viewport'] ), $url_metric->get( 'viewport' ) ); - $this->assertSame( array_map( 'intval', $data['viewport'] ), $url_metric->viewport ); $this->assertSame( (int) $data['viewport']['width'], $url_metric->get_viewport_width() ); - $this->assertSame( (int) $data['viewport']['width'], $url_metric->viewport['width'] ); $this->assertSame( (float) $data['timestamp'], $url_metric->get_timestamp() ); $this->assertSame( (float) $data['timestamp'], $url_metric->get( 'timestamp' ) ); - $this->assertSame( (float) $data['timestamp'], $url_metric->timestamp ); - $this->assertCount( count( $data['elements'] ), $url_metric->elements ); + $this->assertCount( count( $data['elements'] ), $url_metric->get_elements() ); for ( $i = 0, $length = count( $data['elements'] ); $i < $length; $i++ ) { - $this->assertSame( (bool) $data['elements'][ $i ]['isLCP'], $url_metric->elements[ $i ]['isLCP'] ); - $this->assertSame( (bool) $data['elements'][ $i ]['isLCPCandidate'], $url_metric->elements[ $i ]['isLCPCandidate'] ); - $this->assertSame( (float) $data['elements'][ $i ]['intersectionRatio'], $url_metric->elements[ $i ]['intersectionRatio'] ); - $this->assertSame( array_map( 'floatval', $data['elements'][ $i ]['boundingClientRect'] ), $url_metric->elements[ $i ]['boundingClientRect'] ); - $this->assertSame( array_map( 'floatval', $data['elements'][ $i ]['intersectionRect'] ), $url_metric->elements[ $i ]['intersectionRect'] ); + $this->assertSame( (bool) $data['elements'][ $i ]['isLCP'], $url_metric->get_elements()[ $i ]['isLCP'] ); + $this->assertSame( (bool) $data['elements'][ $i ]['isLCPCandidate'], $url_metric->get_elements()[ $i ]['isLCPCandidate'] ); + $this->assertSame( (float) $data['elements'][ $i ]['intersectionRatio'], $url_metric->get_elements()[ $i ]['intersectionRatio'] ); + $this->assertSame( array_map( 'floatval', $data['elements'][ $i ]['boundingClientRect'] ), $url_metric->get_elements()[ $i ]['boundingClientRect'] ); + $this->assertSame( array_map( 'floatval', $data['elements'][ $i ]['intersectionRect'] ), $url_metric->get_elements()[ $i ]['intersectionRect'] ); } - $this->assertSame( $url_metric->elements, $url_metric->get_elements() ); - $this->assertSame( $url_metric->elements, $url_metric->get( 'elements' ) ); + $this->assertSame( $url_metric->get_elements(), $url_metric->get( '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(); @@ -394,12 +388,12 @@ static function ( array $properties ): array { $original_data = $original_url_metric->jsonSerialize(); $this->assertArrayHasKey( 'isColorful', $original_data['elements'][0] ); $this->assertSame( 'false', $original_data['elements'][0]['isColorful'] ); - $this->assertSame( 'false', $original_url_metric->elements[0]['isColorful'] ); + $this->assertSame( 'false', $original_url_metric->get_elements()[0]['isColorful'] ); $extended_data = $extended_url_metric->jsonSerialize(); $this->assertArrayHasKey( 'isColorful', $extended_data['elements'][0] ); $this->assertFalse( $extended_data['elements'][0]['isColorful'] ); - $this->assertFalse( $extended_url_metric->elements[0]['isColorful'] ); + $this->assertFalse( $extended_url_metric->get_elements()[0]['isColorful'] ); }, ), @@ -464,9 +458,7 @@ static function ( array $properties ): array { /** * Tests construction with extended schema. * - * @covers ::jsonSerialize - * @covers ::get - * @covers ::__get + * @covers ::get_json_schema * * @dataProvider data_provider_to_test_constructor_with_extended_schema *