Skip to content

Commit

Permalink
Remove the plural from URL Metrics in symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 2, 2024
1 parent b515f87 commit 942b397
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
return false;
}

$max_intersection_ratio = $context->url_metrics_group_collection->get_element_max_intersection_ratio( $processor->get_xpath() );
$max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( $processor->get_xpath() );

if ( $max_intersection_ratio > 0 ) {
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function ( OD_Tag_Visitor_Context $context ) use ( $test_case ): bool {
} else {
$test_case->assertTrue( $processor->has_bookmark( 'the_first_video' ) );
}
if ( $context->url_metrics_group_collection->get_element_max_intersection_ratio( $processor->get_xpath() ) > 0 ) {
if ( $context->url_metric_group_collection->get_element_max_intersection_ratio( $processor->get_xpath() ) > 0 ) {
$context->link_collection->add_link(
array(
'rel' => 'preload',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
$xpath = $processor->get_xpath();

// If this element is the LCP (for a breakpoint group), add a preload link for it.
foreach ( $context->url_metrics_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
foreach ( $context->url_metric_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
$link_attributes = array(
'rel' => 'preload',
'fetchpriority' => 'high',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
* fetchpriority=high, even though it won't really be needed because a preload link with fetchpriority=high
* will also be added. Additionally, ensure that this common LCP element is never lazy-loaded.
*/
$common_lcp_element = $context->url_metrics_group_collection->get_common_lcp_element();
$common_lcp_element = $context->url_metric_group_collection->get_common_lcp_element();
if ( ! is_null( $common_lcp_element ) && $xpath === $common_lcp_element['xpath'] ) {
if ( 'high' === $get_attribute_value( 'fetchpriority' ) ) {
$processor->set_meta_attribute( 'fetchpriority-already-added', true );
Expand All @@ -70,9 +70,9 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
is_string( $processor->get_attribute( 'fetchpriority' ) )
&&
// Temporary condition in case someone updates Image Prioritizer without also updating Optimization Detective.
method_exists( $context->url_metrics_group_collection, 'is_any_group_populated' )
method_exists( $context->url_metric_group_collection, 'is_any_group_populated' )
&&
$context->url_metrics_group_collection->is_any_group_populated()
$context->url_metric_group_collection->is_any_group_populated()
) {
/*
* At this point, the element is not the shared LCP across all viewport groups. Nevertheless, server-side
Expand All @@ -87,7 +87,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
$processor->remove_attribute( 'fetchpriority' );
}

$element_max_intersection_ratio = $context->url_metrics_group_collection->get_element_max_intersection_ratio( $xpath );
$element_max_intersection_ratio = $context->url_metric_group_collection->get_element_max_intersection_ratio( $xpath );

// If the element was not found, we don't know if it was visible for not, so don't do anything.
if ( is_null( $element_max_intersection_ratio ) ) {
Expand Down Expand Up @@ -122,7 +122,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
}

// If this element is the LCP (for a breakpoint group), add a preload link for it.
foreach ( $context->url_metrics_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
foreach ( $context->url_metric_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
$link_attributes = array_merge(
array(
'rel' => 'preload',
Expand Down
56 changes: 46 additions & 10 deletions plugins/optimization-detective/class-od-tag-visitor-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
* @since 0.4.0
* @access private
*
* @property-read OD_URL_Metric_Group_Collection $url_metrics_group_collection
*/
final class OD_Tag_Visitor_Context {

Expand All @@ -28,12 +30,12 @@ final class OD_Tag_Visitor_Context {
public $processor;

/**
* URL metrics group collection.
* URL metric group collection.
*
* @var OD_URL_Metrics_Group_Collection
* @var OD_URL_Metric_Group_Collection
* @readonly
*/
public $url_metrics_group_collection;
public $url_metric_group_collection;

/**
* Link collection.
Expand All @@ -46,13 +48,47 @@ final class OD_Tag_Visitor_Context {
/**
* Constructor.
*
* @param OD_HTML_Tag_Processor $processor HTML tag processor.
* @param OD_URL_Metrics_Group_Collection $url_metrics_group_collection URL metrics group collection.
* @param OD_Link_Collection $link_collection Link collection.
* @param OD_HTML_Tag_Processor $processor HTML tag processor.
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL metric group collection.
* @param OD_Link_Collection $link_collection Link collection.
*/
public function __construct( OD_HTML_Tag_Processor $processor, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Link_Collection $link_collection ) {
$this->processor = $processor;
$this->url_metric_group_collection = $url_metric_group_collection;
$this->link_collection = $link_collection;
}

/**
* Gets magic properties.
*
* @param string $name Property name.
* @return OD_URL_Metric_Group_Collection URL metric group collection.
*
* @throws Error When property is unknown.
*/
public function __construct( OD_HTML_Tag_Processor $processor, OD_URL_Metrics_Group_Collection $url_metrics_group_collection, OD_Link_Collection $link_collection ) {
$this->processor = $processor;
$this->url_metrics_group_collection = $url_metrics_group_collection;
$this->link_collection = $link_collection;
public function __get( string $name ): OD_URL_Metric_Group_Collection {
if ( 'url_metrics_group_collection' === $name ) {
_doing_it_wrong(
__CLASS__ . '::$url_metrics_group_collection',
esc_html(
sprintf(
/* translators: %s is class member variable name */
__( 'Use %s instead.', 'optimization-detective' ),
__CLASS__ . '::$url_metric_group_collection'
)
),
'optimization-detective n.e.x.t'
);
return $this->url_metric_group_collection;
}
throw new Error(
esc_html(
sprintf(
/* translators: %s is class member variable name */
__( 'Unknown property %s.', 'optimization-detective' ),
__CLASS__ . '::$' . $name
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Optimization Detective: OD_URL_Metrics_Group_Collection class
* Optimization Detective: OD_URL_Metric_Group_Collection class
*
* @package optimization-detective
* @since 0.1.0
Expand All @@ -16,24 +16,24 @@
*
* @phpstan-import-type ElementData from OD_URL_Metric
*
* @implements IteratorAggregate<int, OD_URL_Metrics_Group>
* @implements IteratorAggregate<int, OD_URL_Metric_Group>
*
* @since 0.1.0
* @access private
*/
final class OD_URL_Metrics_Group_Collection implements Countable, IteratorAggregate, JsonSerializable {
final class OD_URL_Metric_Group_Collection implements Countable, IteratorAggregate, JsonSerializable {

/**
* URL metrics groups.
* URL metric groups.
*
* The number of groups corresponds to one greater than the number of
* breakpoints. This is because breakpoints are the dividing line between
* the groups of URL metrics with specific viewport widths. This extends
* even to when there are zero breakpoints: there will still be one group
* in this case, in which every single URL metric is added.
*
* @var OD_URL_Metrics_Group[]
* @phpstan-var non-empty-array<OD_URL_Metrics_Group>
* @var OD_URL_Metric_Group[]
* @phpstan-var non-empty-array<OD_URL_Metric_Group>
*/
private $groups;

Expand Down Expand Up @@ -74,11 +74,11 @@ final class OD_URL_Metrics_Group_Collection implements Countable, IteratorAggreg
* Result cache.
*
* @var array{
* get_group_for_viewport_width?: array<int, OD_URL_Metrics_Group>,
* get_group_for_viewport_width?: array<int, OD_URL_Metric_Group>,
* is_every_group_populated?: bool,
* is_any_group_populated?: bool,
* is_every_group_complete?: bool,
* get_groups_by_lcp_element?: array<string, OD_URL_Metrics_Group[]>,
* get_groups_by_lcp_element?: array<string, OD_URL_Metric_Group[]>,
* get_common_lcp_element?: ElementData|null,
* get_all_element_max_intersection_ratios?: array<string, float>
* }
Expand Down Expand Up @@ -167,18 +167,18 @@ public function clear_cache(): void {
/**
* Create groups.
*
* @phpstan-return non-empty-array<OD_URL_Metrics_Group>
* @phpstan-return non-empty-array<OD_URL_Metric_Group>
*
* @return OD_URL_Metrics_Group[] Groups.
* @return OD_URL_Metric_Group[] Groups.
*/
private function create_groups(): array {
$groups = array();
$min_width = 0;
foreach ( $this->breakpoints as $max_width ) {
$groups[] = new OD_URL_Metrics_Group( array(), $min_width, $max_width, $this->sample_size, $this->freshness_ttl, $this );
$groups[] = new OD_URL_Metric_Group( array(), $min_width, $max_width, $this->sample_size, $this->freshness_ttl, $this );
$min_width = $max_width + 1;
}
$groups[] = new OD_URL_Metrics_Group( array(), $min_width, PHP_INT_MAX, $this->sample_size, $this->freshness_ttl, $this );
$groups[] = new OD_URL_Metric_Group( array(), $min_width, PHP_INT_MAX, $this->sample_size, $this->freshness_ttl, $this );
return $groups;
}

Expand Down Expand Up @@ -209,9 +209,9 @@ public function add_url_metric( OD_URL_Metric $new_url_metric ): void {
* @throws InvalidArgumentException When there is no group for the provided viewport width. This would only happen if a negative width is provided.
*
* @param int $viewport_width Viewport width.
* @return OD_URL_Metrics_Group URL metrics group for the viewport width.
* @return OD_URL_Metric_Group URL metric group for the viewport width.
*/
public function get_group_for_viewport_width( int $viewport_width ): OD_URL_Metrics_Group {
public function get_group_for_viewport_width( int $viewport_width ): OD_URL_Metric_Group {
if ( array_key_exists( __FUNCTION__, $this->result_cache ) && array_key_exists( $viewport_width, $this->result_cache[ __FUNCTION__ ] ) ) {
return $this->result_cache[ __FUNCTION__ ][ $viewport_width ];
}
Expand All @@ -225,8 +225,8 @@ public function get_group_for_viewport_width( int $viewport_width ): OD_URL_Metr
throw new InvalidArgumentException(
esc_html(
sprintf(
/* translators: %d is viewport width */
__( 'No URL metrics group found for viewport width: %d', 'optimization-detective' ),
/* translators: %d is viewport width */
__( 'No URL metric group found for viewport width: %d', 'optimization-detective' ),
$viewport_width
)
)
Expand Down Expand Up @@ -268,7 +268,7 @@ public function is_any_group_populated(): bool {
* should be contrasted with the `is_every_group_complete()`
* method below.
*
* @see OD_URL_Metrics_Group_Collection::is_every_group_complete()
* @see OD_URL_Metric_Group_Collection::is_every_group_complete()
*
* @return bool Whether all groups have some URL metrics.
*/
Expand All @@ -293,7 +293,7 @@ public function is_every_group_populated(): bool {
/**
* Checks whether every group is complete.
*
* @see OD_URL_Metrics_Group::is_complete()
* @see OD_URL_Metric_Group::is_complete()
*
* @return bool Whether all groups are complete.
*/
Expand All @@ -319,10 +319,11 @@ public function is_every_group_complete(): bool {
/**
* Gets the groups with the provided LCP element XPath.
*
* @see OD_URL_Metrics_Group::get_lcp_element()
* @see OD_URL_Metric_Group::get_lcp_element()
*
* @param string $xpath XPath for LCP element.
* @return OD_URL_Metrics_Group[] Groups which have the LCP element.
*
* @return OD_URL_Metric_Group[] Groups which have the LCP element.
*/
public function get_groups_by_lcp_element( string $xpath ): array {
if ( array_key_exists( __FUNCTION__, $this->result_cache ) && array_key_exists( $xpath, $this->result_cache[ __FUNCTION__ ] ) ) {
Expand Down Expand Up @@ -460,14 +461,14 @@ public function get_flattened_url_metrics(): array {
/**
* Returns an iterator for the groups of URL metrics.
*
* @return ArrayIterator<int, OD_URL_Metrics_Group> Array iterator for OD_URL_Metric_Group instances.
* @return ArrayIterator<int, OD_URL_Metric_Group> Array iterator for OD_URL_Metric_Group instances.
*/
public function getIterator(): ArrayIterator {
return new ArrayIterator( $this->groups );
}

/**
* Counts the URL metrics groups in the collection.
* Counts the URL metric groups in the collection.
*
* @return int<0, max> Group count.
*/
Expand Down Expand Up @@ -507,7 +508,7 @@ public function jsonSerialize(): array {
'every_group_complete' => $this->is_every_group_complete(),
'every_group_populated' => $this->is_every_group_populated(),
'groups' => array_map(
static function ( OD_URL_Metrics_Group $group ): array {
static function ( OD_URL_Metric_Group $group ): array {
$group_data = $group->jsonSerialize();
// Remove redundant data.
unset(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Optimization Detective: OD_URL_Metrics_Group class
* Optimization Detective: OD_URL_Metric_Group class
*
* @package optimization-detective
* @since 0.1.0
Expand All @@ -20,7 +20,7 @@
* @since 0.1.0
* @access private
*/
final class OD_URL_Metrics_Group implements IteratorAggregate, Countable, JsonSerializable {
final class OD_URL_Metric_Group implements IteratorAggregate, Countable, JsonSerializable {

/**
* URL metrics.
Expand Down Expand Up @@ -64,7 +64,7 @@ final class OD_URL_Metrics_Group implements IteratorAggregate, Countable, JsonSe
/**
* Collection that this instance belongs to.
*
* @var OD_URL_Metrics_Group_Collection|null
* @var OD_URL_Metric_Group_Collection|null
*/
private $collection;

Expand All @@ -83,14 +83,14 @@ final class OD_URL_Metrics_Group implements IteratorAggregate, Countable, JsonSe
*
* @throws InvalidArgumentException If arguments are invalid.
*
* @param OD_URL_Metric[] $url_metrics URL metrics to add to the group.
* @param int $minimum_viewport_width Minimum possible viewport width for the group. Must be zero or greater.
* @param int $maximum_viewport_width Maximum possible viewport width for the group. Must be greater than zero and the minimum viewport width.
* @param int $sample_size Sample size for the maximum number of viewports in a group between breakpoints.
* @param int $freshness_ttl Freshness age (TTL) for a given URL metric.
* @param OD_URL_Metrics_Group_Collection|null $collection Collection that this instance belongs to. Optional.
* @param OD_URL_Metric[] $url_metrics URL metrics to add to the group.
* @param int $minimum_viewport_width Minimum possible viewport width for the group. Must be zero or greater.
* @param int $maximum_viewport_width Maximum possible viewport width for the group. Must be greater than zero and the minimum viewport width.
* @param int $sample_size Sample size for the maximum number of viewports in a group between breakpoints.
* @param int $freshness_ttl Freshness age (TTL) for a given URL metric.
* @param OD_URL_Metric_Group_Collection|null $collection Collection that this instance belongs to. Optional.
*/
public function __construct( array $url_metrics, int $minimum_viewport_width, int $maximum_viewport_width, int $sample_size, int $freshness_ttl, ?OD_URL_Metrics_Group_Collection $collection = null ) {
public function __construct( array $url_metrics, int $minimum_viewport_width, int $maximum_viewport_width, int $sample_size, int $freshness_ttl, ?OD_URL_Metric_Group_Collection $collection = null ) {
if ( $minimum_viewport_width < 0 ) {
throw new InvalidArgumentException(
esc_html__( 'The minimum viewport width must be at least zero.', 'optimization-detective' )
Expand Down Expand Up @@ -211,7 +211,7 @@ static function ( OD_URL_Metric $a, OD_URL_Metric $b ): int {
}

/**
* Determines whether the URL metrics group is complete.
* Determines whether the URL metric group is complete.
*
* A group is complete if it has the full sample size of URL metrics
* and all of these URL metrics are fresh.
Expand Down
Loading

0 comments on commit 942b397

Please sign in to comment.