Skip to content

Conversation

@westonruter
Copy link
Member

When working with URL metrics, I found it difficult when debugging to not have a stable identifier as part of a given URL metric instance. This adds a uuid field to the URL metric to serve as this stable identifier.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Aug 21, 2024
@westonruter westonruter self-assigned this Aug 21, 2024
@github-actions
Copy link

github-actions bot commented Aug 21, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

singular

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter LGTM, just one nit-pick.

* @throws OD_Data_Validation_Exception When the input is invalid.
*/
public function __construct( array $data ) {
if ( ! array_key_exists( 'uuid', $data ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset() is faster than array_key_exists(), this has been subject of several core changes. I'd argue anyway that it would be safer to use the following here to ensure this is never empty:

Suggested change
if ( ! array_key_exists( 'uuid', $data ) ) {
if ( ! isset( $data['uuid'] ) || ! $data['uuid'] ) {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, but what about #1219?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should reduce the scope of that issue to remove use of empty() only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the ! $data['uuid'] part is flagged by PHPStan's strict rules:

Only booleans are allowed in a negated boolean, mixed given.

So I'll just commit the ! isset( $data['uuid'] ) part. The JSON Schema will handle validation of uuid. It should be absent for everyone right now, resulting in a new uuid being assigned to all existing URL metrics whenever a new URL metric is captured and added to an od_url_metrics post.

Done in 7a1b892

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I don't think there's any problem with isset() when used with array keys or object properties. In other words, isset( $arr['something'] ) is perfectly fine, but isset( $something ) is not.

It's a good idea to remove empty() usages and, if reasonable to implement, also warn about using isset() when it's not with array keys or object properties.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only booleans are allowed in a negated boolean, mixed given.

On another note, this is the only thing that PHPStan complains about that I don't like. I don't like writing '' === $value just as little as I like writing false === $value, and I don't see any benefit of preventing ! on a string. What is the benefit? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree it can be annoying at times, I think the explicitness is good for type safety and making sure that you're always working with the data you expect. Otherwise, I had to ask Gemini for advice: https://g.co/gemini/share/270a3e76fcdd

@swissspidy may have more thoughts.

@westonruter westonruter requested a review from felixarntz August 21, 2024 16:30
@westonruter westonruter merged commit 81842e5 into trunk Aug 21, 2024
@westonruter westonruter deleted the add/od-debugging branch August 21, 2024 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature

Projects

Status: Done 😃

Development

Successfully merging this pull request may close these issues.

4 participants