Skip to content

Commit

Permalink
Add getter/setter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 17, 2024
1 parent a99208b commit 344d6e1
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ private function reduce_layout_shifts( OD_Tag_Visitor_Context $context ): void {
if ( ! is_array( $resized_bounding_client_rect ) ) {
continue;
}
$group = $element->url_metric->group;
$group = $element->get_url_metric_group();
if ( null === $group ) {
continue; // Technically could be null but in practice it never will be.
}
$group_min_width = $group->get_minimum_viewport_width();
if ( ! isset( $minimums[ $group_min_width ] ) ) {
$minimums[ $group_min_width ] = array(
Expand Down
24 changes: 23 additions & 1 deletion plugins/optimization-detective/class-od-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class OD_Element implements ArrayAccess, JsonSerializable {
* @var OD_URL_Metric
* @readonly
*/
public $url_metric;
protected $url_metric;

/**
* Constructor.
Expand All @@ -56,6 +56,28 @@ public function __construct( array $data, OD_URL_Metric $url_metric ) {
$this->url_metric = $url_metric;
}

/**
* Gets the URL metric that this element belongs to.
*
* @since n.e.x.t
*
* @return OD_URL_Metric URL Metric.
*/
public function get_url_metric(): OD_URL_Metric {
return $this->url_metric;
}

/**
* Gets the group that this element's URL metric is a part of (which may not be any).
*
* @since n.e.x.t
*
* @return OD_URL_Metric_Group|null Group.
*/
public function get_url_metric_group(): ?OD_URL_Metric_Group {
return $this->url_metric->get_group();
}

/**
* Gets property value for an arbitrary key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function add_url_metric( OD_URL_Metric $url_metric ): void {
$this->collection->clear_cache();
}

$url_metric->group = $this;
$url_metric->set_group( $this );
$this->url_metrics[] = $url_metric;

// If we have too many URL metrics now, remove the oldest ones up to the sample size.
Expand Down
31 changes: 29 additions & 2 deletions plugins/optimization-detective/class-od-url-metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class OD_URL_Metric implements JsonSerializable {
* Group.
*
* @since n.e.x.t
* @var OD_URL_Metric_Group
* @var OD_URL_Metric_Group|null
*/
public $group;
protected $group = null;

/**
* Constructor.
Expand Down Expand Up @@ -123,6 +123,33 @@ private function prepare_data( array $data ): array {
return rest_sanitize_value_from_schema( $data, $schema, self::class );
}

/**
* Gets the group that this URL metric is a part of (which may not be any).
*
* @since n.e.x.t
*
* @return OD_URL_Metric_Group|null Group.
*/
public function get_group(): ?OD_URL_Metric_Group {
return $this->group;
}

/**
* Sets the group that this URL metric is a part of.
*
* @since n.e.x.t
*
* @param OD_URL_Metric_Group $group Group.
*
* @throws InvalidArgumentException When the supplied group has minimum/maximum viewport widths which are out of bounds with the viewport width for this URL Metric.
*/
public function set_group( OD_URL_Metric_Group $group ): void {
if ( $this->get_viewport_width() < $group->get_minimum_viewport_width() || $this->get_viewport_width() > $group->get_maximum_viewport_width() ) {
throw new InvalidArgumentException( 'Group does not have the correct minimum or maximum viewport widths for this URL Metric.' );
}
$this->group = $group;
}

/**
* Gets JSON schema for URL Metric.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Test_OD_Element extends WP_UnitTestCase {
* Tests construction.
*
* @covers ::get
* @covers ::get_url_metric
* @covers ::get_url_metric_group
* @covers ::is_lcp
* @covers ::is_lcp_candidate
* @covers ::get_xpath
Expand Down Expand Up @@ -67,6 +69,11 @@ static function ( array $schema ): array {

$element = $url_metric->get_elements()[0];
$this->assertInstanceOf( OD_Element::class, $element );
$this->assertSame( $url_metric, $element->get_url_metric() );
$this->assertNull( $element->get_url_metric_group() );
$collection = new OD_URL_Metric_Group_Collection( array( $url_metric ), array(), 1, DAY_IN_SECONDS );
$collection->add_url_metric( $url_metric );
$this->assertSame( iterator_to_array( $collection )[0], $element->get_url_metric_group() );

$this->assertSame( $element_data['xpath'], $element->get_xpath() );
$this->assertSame( $element_data['xpath'], $element['xpath'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ static function ( $value ) {
* @covers ::jsonSerialize
* @covers ::get
* @covers ::get_json_schema
* @covers ::set_group
* @covers ::get_group
*
* @dataProvider data_provider_to_test_constructor
*
Expand All @@ -217,6 +219,10 @@ public function test_constructor( array $data, string $error = '' ): void {
$this->expectExceptionMessage( $error );
}
$url_metric = new OD_URL_Metric( $data );
$this->assertNull( $url_metric->get_group() );
$group = new OD_URL_Metric_Group( array( $url_metric ), 0, PHP_INT_MAX, 1, DAY_IN_SECONDS );
$url_metric->set_group( $group );
$this->assertSame( $group, $url_metric->get_group() );

$this->assertSame( array_map( 'intval', $data['viewport'] ), $url_metric->get_viewport() );
$this->assertSame( array_map( 'intval', $data['viewport'] ), $url_metric->get( 'viewport' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,7 @@ public function test_get_all_element_max_intersection_ratios( array $url_metrics
$this->assertCount( count( $xpath_counts ), $all_elements );
foreach ( $all_elements as $xpath => $elements ) {
foreach ( $elements as $element ) {
$this->assertInstanceOf( OD_URL_Metric::class, $element->url_metric );
$this->assertInstanceOf( OD_URL_Metric_Group::class, $element->url_metric->group );
$this->assertSame( $element->get_url_metric()->get_group(), $element->get_url_metric_group() );
$this->assertInstanceOf( OD_Element::class, $element );
$this->assertSame( $xpath, $element['xpath'] );
}
Expand Down

0 comments on commit 344d6e1

Please sign in to comment.