Skip to content

Commit 3de4632

Browse files
committed
Implement getter methods
1 parent c3e55f9 commit 3de4632

File tree

6 files changed

+126
-33
lines changed

6 files changed

+126
-33
lines changed

plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,21 @@ private function reduce_layout_shifts( OD_Tag_Visitor_Context $context ): void {
216216

217217
$elements = $context->url_metric_group_collection->get_all_elements()[ $embed_wrapper_xpath ] ?? array();
218218
foreach ( $elements as $element ) {
219-
if ( ! isset( $element['resizedBoundingClientRect'] ) ) {
219+
$resized_bounding_client_rect = $element->get( 'resizedBoundingClientRect' );
220+
if ( ! is_array( $resized_bounding_client_rect ) ) {
220221
continue;
221222
}
222223
$group = $element->url_metric->group;
223224
$group_min_width = $group->get_minimum_viewport_width();
224225
if ( ! isset( $minimums[ $group_min_width ] ) ) {
225226
$minimums[ $group_min_width ] = array(
226227
'group' => $group,
227-
'height' => $element['resizedBoundingClientRect']['height'],
228+
'height' => $resized_bounding_client_rect['height'],
228229
);
229230
} else {
230231
$minimums[ $group_min_width ]['height'] = min(
231232
$minimums[ $group_min_width ]['height'],
232-
$element['resizedBoundingClientRect']['height']
233+
$resized_bounding_client_rect['height']
233234
);
234235
}
235236
}

plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __invoke( OD_Tag_Visitor_Context $context ): bool {
4646
* will also be added. Additionally, ensure that this common LCP element is never lazy-loaded.
4747
*/
4848
$common_lcp_element = $context->url_metric_group_collection->get_common_lcp_element();
49-
if ( ! is_null( $common_lcp_element ) && $xpath === $common_lcp_element['xpath'] ) {
49+
if ( ! is_null( $common_lcp_element ) && $xpath === $common_lcp_element->get_xpath() ) {
5050
if ( 'high' === $this->get_attribute_value( $processor, 'fetchpriority' ) ) {
5151
$processor->set_meta_attribute( 'fetchpriority-already-added', true );
5252
} else {

plugins/optimization-detective/class-od-element.php

Lines changed: 110 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* Data for a single element in a URL metric.
1616
*
1717
* @phpstan-import-type ElementData from OD_URL_Metric
18+
* @phpstan-import-type DOMRect from OD_URL_Metric
19+
* @implements ArrayAccess<key-of<ElementData>, ElementData[key-of<ElementData>]>
1820
*
1921
* @since n.e.x.t
2022
* @access private
@@ -51,6 +53,90 @@ public function __construct( array $data, OD_URL_Metric $url_metric ) {
5153
$this->url_metric = $url_metric;
5254
}
5355

56+
/**
57+
* Gets property value for an arbitrary key.
58+
*
59+
* This is particularly useful in conjunction with the `od_url_metric_schema_element_item_additional_properties` filter.
60+
*
61+
* @since n.e.x.t
62+
*
63+
* @param string $key Property.
64+
* @return mixed|null The property value, or null if not set.
65+
*/
66+
public function get( string $key ) {
67+
return $this->data[ $key ] ?? null;
68+
}
69+
70+
/**
71+
* Determines whether element was detected as LCP.
72+
*
73+
* @since n.e.x.t
74+
*
75+
* @return bool Whether LCP.
76+
*/
77+
public function is_lcp(): bool {
78+
return $this->data['isLCP'];
79+
}
80+
81+
/**
82+
* Determines whether element was detected as an LCP candidate.
83+
*
84+
* @since n.e.x.t
85+
*
86+
* @return bool Whether LCP candidate.
87+
*/
88+
public function is_lcp_candidate(): bool {
89+
return $this->data['isLCPCandidate'];
90+
}
91+
92+
/**
93+
* Gets XPath for element.
94+
*
95+
* @since n.e.x.t
96+
*
97+
* @return non-empty-string XPath.
98+
*/
99+
public function get_xpath(): string {
100+
return $this->data['xpath'];
101+
}
102+
103+
/**
104+
* Gets intersectionRatio for element.
105+
*
106+
* @since n.e.x.t
107+
*
108+
* @return float Intersection ratio.
109+
*/
110+
public function get_intersection_ratio(): float {
111+
return $this->data['intersectionRatio'];
112+
}
113+
114+
/**
115+
* Gets intersectionRect for element.
116+
*
117+
* @since n.e.x.t
118+
*
119+
* @phpstan-return DOMRect
120+
*
121+
* @return array Intersection rect.
122+
*/
123+
public function get_intersection_rect(): array {
124+
return $this->data['intersectionRect'];
125+
}
126+
127+
/**
128+
* Gets boundingClientRect for element.
129+
*
130+
* @since n.e.x.t
131+
*
132+
* @phpstan-return DOMRect
133+
*
134+
* @return array Bounding client rect.
135+
*/
136+
public function get_bounding_client_rect(): array {
137+
return $this->data['boundingClientRect'];
138+
}
139+
54140
/**
55141
* Checks whether an offset exists.
56142
*
@@ -68,40 +154,46 @@ public function offsetExists( $offset ): bool {
68154
*
69155
* @since n.e.x.t
70156
*
157+
* @template T of key-of<ElementData>
158+
* @phpstan-param T $offset
159+
* @phpstan-return ElementData[T]|null
160+
*
71161
* @param mixed $offset Key.
72-
* @return mixed Can return all value types.
162+
* @return mixed May return any value from ElementData including possible extensions.
73163
*/
74-
public function offsetGet( $offset ) { // phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
75-
if ( ! is_scalar( $offset ) ) {
76-
return null;
77-
}
164+
public function offsetGet( $offset ) {
78165
return $this->data[ $offset ] ?? null;
79166
}
80167

81168
/**
82169
* Sets an offset.
83170
*
84-
* @param TKey $offset Key.
85-
* @param TValue $value Value.
171+
* This is disallowed. Attempting to set a property will throw an error.
172+
*
173+
* @since n.e.x.t
174+
*
175+
* @param mixed $offset Key.
176+
* @param mixed $value Value.
177+
*
178+
* @throws Exception When attempting to set a property.
86179
*/
87180
public function offsetSet( $offset, $value ): void {
88-
// @todo Throw an error.
89-
$this->data[ $offset ] = $value;
181+
throw new Exception( 'Offsets may not be set.' );
90182
}
91183

92184
/**
93-
* Offset to unset
94-
* @link https://php.net/manual/en/arrayaccess.offsetunset.php
185+
* Offset to unset.
186+
*
187+
* This is disallowed. Attempting to unset a property will throw an error.
188+
*
189+
* @since n.e.x.t
95190
*
96-
* @param TKey $offset <p>
97-
* The offset to unset.
98-
* </p>
191+
* @param mixed $offset Offset.
99192
*
100-
* @return void
193+
* @throws Exception When attempting to unset a property.
101194
*/
102-
public function offsetUnset( $offset ){
103-
// @todo Throw an error since read only.
104-
unset( $this->data[ $offset ] );
195+
public function offsetUnset( $offset ): void {
196+
throw new Exception( 'Offsets may not be unset.' );
105197
}
106198

107199
/**

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ public function count(): int {
537537
* every_group_complete: bool,
538538
* every_group_populated: bool,
539539
* groups: array<int, array{
540-
* lcp_element: ?ElementData,
540+
* lcp_element: ?OD_Element,
541541
* minimum_viewport_width: 0|positive-int,
542542
* maximum_viewport_width: positive-int,
543543
* complete: bool,
@@ -546,12 +546,13 @@ public function count(): int {
546546
* } Data which can be serialized by json_encode().
547547
*/
548548
public function jsonSerialize(): array {
549+
$common_lcp_element = $this->get_common_lcp_element();
549550
return array(
550551
'breakpoints' => $this->breakpoints,
551552
'freshness_ttl' => $this->freshness_ttl,
552553
'sample_size' => $this->sample_size,
553554
'all_element_max_intersection_ratios' => $this->get_all_element_max_intersection_ratios(),
554-
'common_lcp_element' => $this->get_common_lcp_element(),
555+
'common_lcp_element' => $common_lcp_element instanceof OD_Element ? $common_lcp_element->jsonSerialize() : null,
555556
'every_group_complete' => $this->is_every_group_complete(),
556557
'every_group_populated' => $this->is_every_group_populated(),
557558
'groups' => array_map(

plugins/optimization-detective/class-od-url-metric-group.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* URL metrics grouped by viewport according to breakpoints.
1616
*
1717
* @implements IteratorAggregate<int, OD_URL_Metric>
18-
* @phpstan-import-type ElementData from OD_URL_Metric
1918
*
2019
* @since 0.1.0
2120
* @access private
@@ -281,20 +280,20 @@ public function get_lcp_element(): ?OD_Element {
281280
/**
282281
* Breadcrumb element.
283282
*
284-
* @var array<int, ElementData> $breadcrumb_element
283+
* @var array<int, OD_Element> $breadcrumb_element
285284
*/
286285
$breadcrumb_element = array();
287286

288287
foreach ( $this->url_metrics as $url_metric ) {
289288
foreach ( $url_metric->get_elements() as $element ) {
290-
if ( ! $element['isLCP'] ) {
289+
if ( ! $element->is_lcp() ) {
291290
continue;
292291
}
293292

294-
$i = array_search( $element['xpath'], $seen_breadcrumbs, true );
293+
$i = array_search( $element->get_xpath(), $seen_breadcrumbs, true );
295294
if ( false === $i ) {
296295
$i = count( $seen_breadcrumbs );
297-
$seen_breadcrumbs[ $i ] = $element['xpath'];
296+
$seen_breadcrumbs[ $i ] = $element->get_xpath();
298297
$breadcrumb_counts[ $i ] = 0;
299298
}
300299

@@ -349,7 +348,7 @@ public function count(): int {
349348
* sample_size: positive-int,
350349
* minimum_viewport_width: 0|positive-int,
351350
* maximum_viewport_width: positive-int,
352-
* lcp_element: ?ElementData,
351+
* lcp_element: ?OD_Element,
353352
* complete: bool,
354353
* url_metrics: OD_URL_Metric[]
355354
* } Data which can be serialized by json_encode().

plugins/optimization-detective/class-od-url-metric.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
* @phpstan-type ElementData array{
3232
* isLCP: bool,
3333
* isLCPCandidate: bool,
34-
* xpath: string,
34+
* xpath: non-empty-string,
3535
* intersectionRatio: float,
3636
* intersectionRect: DOMRect,
3737
* boundingClientRect: DOMRect,
3838
* }
3939
* @phpstan-type Data array{
40-
* uuid: string,
41-
* url: string,
40+
* uuid: non-empty-string,
41+
* url: non-empty-string,
4242
* timestamp: float,
4343
* viewport: ViewportRect,
4444
* elements: ElementData[]

0 commit comments

Comments
 (0)