Skip to content

Commit 77bea30

Browse files
committed
Add helper generator method to get all elements
1 parent 5ba19f4 commit 77bea30

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

plugins/optimization-detective/class-od-url-metrics-group-collection.php

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,28 @@ public function get_common_lcp_element(): ?array {
396396
return $result;
397397
}
398398

399+
/**
400+
* Gets all elements from all URL metrics from all groups.
401+
*
402+
* This is an O(n^3) function so its results must be cached. This being said, the number of groups should be 4 (one
403+
* more than the default number of breakpoints) and the number of URL metrics for each group should be 3
404+
* (the default sample size). Therefore, given the number (n) of visited elements on the page this will only
405+
* end up running n*4*3 times.
406+
*
407+
* @since n.e.x.t
408+
*
409+
* @return Generator<ElementData>
410+
*/
411+
protected function get_all_url_metrics_groups_elements(): Generator {
412+
foreach ( $this->groups as $group ) {
413+
foreach ( $group as $url_metric ) {
414+
foreach ( $url_metric->get_elements() as $element ) {
415+
yield $element;
416+
}
417+
}
418+
}
419+
}
420+
399421
/**
400422
* Gets the max intersection ratios of all elements across all groups and their captured URL metrics.
401423
*
@@ -408,21 +430,10 @@ public function get_all_element_max_intersection_ratios(): array {
408430

409431
$result = ( function () {
410432
$element_max_intersection_ratios = array();
411-
412-
/*
413-
* O(n^3) my! Yes. This is why the result is cached. This being said, the number of groups should be 4 (one
414-
* more than the default number of breakpoints) and the number of URL metrics for each group should be 3
415-
* (the default sample size). Therefore, given the number (n) of visited elements on the page this will only
416-
* end up running n*4*3 times.
417-
*/
418-
foreach ( $this->groups as $group ) {
419-
foreach ( $group as $url_metric ) {
420-
foreach ( $url_metric->get_elements() as $element ) {
421-
$element_max_intersection_ratios[ $element['xpath'] ] = array_key_exists( $element['xpath'], $element_max_intersection_ratios )
422-
? max( $element_max_intersection_ratios[ $element['xpath'] ], $element['intersectionRatio'] )
423-
: $element['intersectionRatio'];
424-
}
425-
}
433+
foreach ( $this->get_all_url_metrics_groups_elements() as $element ) {
434+
$element_max_intersection_ratios[ $element['xpath'] ] = array_key_exists( $element['xpath'], $element_max_intersection_ratios )
435+
? max( $element_max_intersection_ratios[ $element['xpath'] ], $element['intersectionRatio'] )
436+
: $element['intersectionRatio'];
426437
}
427438
return $element_max_intersection_ratios;
428439
} )();
@@ -446,20 +457,10 @@ public function get_all_element_minimum_heights(): array {
446457
$result = ( function () {
447458
$element_min_heights = array();
448459

449-
/*
450-
* O(n^3) my! Yes. This is why the result is cached. This being said, the number of groups should be 4 (one
451-
* more than the default number of breakpoints) and the number of URL metrics for each group should be 3
452-
* (the default sample size). Therefore, given the number (n) of visited elements on the page this will only
453-
* end up running n*4*3 times.
454-
*/
455-
foreach ( $this->groups as $group ) {
456-
foreach ( $group as $url_metric ) {
457-
foreach ( $url_metric->get_elements() as $element ) {
458-
$element_min_heights[ $element['xpath'] ] = array_key_exists( $element['xpath'], $element_min_heights )
459-
? min( $element_min_heights[ $element['xpath'] ], $element['intersectionRect']['height'] )
460-
: $element['intersectionRect']['height'];
461-
}
462-
}
460+
foreach ( $this->get_all_url_metrics_groups_elements() as $element ) {
461+
$element_min_heights[ $element['xpath'] ] = array_key_exists( $element['xpath'], $element_min_heights )
462+
? min( $element_min_heights[ $element['xpath'] ], $element['intersectionRect']['height'] )
463+
: $element['intersectionRect']['height'];
463464
}
464465
return $element_min_heights;
465466
} )();

0 commit comments

Comments
 (0)