Skip to content

Commit

Permalink
Eliminate magic getter
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 Sep 11, 2024
1 parent 907d8c3 commit 0db296f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 48 deletions.
21 changes: 0 additions & 21 deletions plugins/optimization-detective/class-od-url-metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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'];
Expand Down
8 changes: 4 additions & 4 deletions plugins/optimization-detective/class-od-url-metrics-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
);
Expand All @@ -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();
}
);

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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() );
Expand Down
30 changes: 11 additions & 19 deletions plugins/optimization-detective/tests/test-class-od-url-metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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();
Expand Down Expand Up @@ -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'] );
},
),

Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 0db296f

Please sign in to comment.