Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent PHP notices if ad dates are not set #77

Merged
merged 2 commits into from
Dec 11, 2024
Merged

Prevent PHP notices if ad dates are not set #77

merged 2 commits into from
Dec 11, 2024

Conversation

sorinmarta
Copy link
Contributor

@sorinmarta sorinmarta commented Dec 11, 2024

fixes https://github.com/Strategy11/awpcp/issues/3174

This PR prevents calling on null notices for OG meta-date formatting methods.

If the check fails we return the stored value of the date which in my tests will still be valid for search engines to pass.

@sorinmarta sorinmarta added run analysis Run the phpcs run tests Run the unit tests labels Dec 11, 2024
Copy link

coderabbitai bot commented Dec 11, 2024

Walkthrough

The changes in this pull request involve modifications to the AWPCP_Meta class located in frontend/class-awpcp-meta.php. Specifically, the methods get_the_date and get_the_modified_date have been updated to improve the handling of date metadata. These methods now check for the presence of published-time and modified-time properties in the $properties array and return the original date if these properties are empty. The overall structure and public interface of the class remain unchanged.

Changes

File Change Summary
frontend/class-awpcp-meta.php Updated get_the_date and get_the_modified_date methods to check for published-time and modified-time properties; return original date if properties are empty. No changes to class structure or public interface.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AWPCP_Meta
    participant Properties

    User->>AWPCP_Meta: Request date
    AWPCP_Meta->>Properties: Check published-time
    AWPCP_Meta->>Properties: Check modified-time
    alt If properties are empty
        AWPCP_Meta-->>User: Return original date
    else
        AWPCP_Meta-->>User: Return converted date
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b698cbd and 585ecff.

📒 Files selected for processing (1)
  • frontend/class-awpcp-meta.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • frontend/class-awpcp-meta.php

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
frontend/class-awpcp-meta.php (2)

261-263: LGTM! Consistent null check implementation.

The null check for 'modified-time' follows the same pattern as get_the_date, providing a robust solution for preventing PHP notices.

Consider adding unit tests to verify the behavior when:

  • dates are null
  • dates are empty strings
  • dates are valid

Would you like me to help generate these test cases?


249-251: Consider extracting date validation logic.

Both methods share similar null-checking logic. Consider extracting this into a private helper method to improve maintainability and reduce code duplication.

Here's a suggested implementation:

+ private function get_formatted_date($date_value, $original_date, $format) {
+     if (empty($date_value)) {
+         return $original_date;
+     }
+     return mysql2date($format, $date_value);
+ }

  public function get_the_date($the_date, $format = '') {
      if (!$format) {
          $format = get_option('date_format');
      }
-     if (empty($this->properties['published-time'])) {
-         return $the_date;
-     }
-     return mysql2date($format, $this->properties['published-time']);
+     return $this->get_formatted_date($this->properties['published-time'], $the_date, $format);
  }

  public function get_the_modified_date($the_date, $format) {
      if (!$format) {
          $format = get_option('date_format');
      }
-     if (empty($this->properties['modified-time'])) {
-         return $the_date;
-     }
-     return mysql2date($format, $this->properties['modified-time']);
+     return $this->get_formatted_date($this->properties['modified-time'], $the_date, $format);
  }

Also applies to: 261-263

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d65f545 and b698cbd.

📒 Files selected for processing (1)
  • frontend/class-awpcp-meta.php (2 hunks)
🔇 Additional comments (1)
frontend/class-awpcp-meta.php (1)

249-251: LGTM! Null check prevents PHP notices.

The added null check for 'published-time' properly prevents PHP notices while maintaining backward compatibility by returning the original date.

Let's verify the usage of this method and potential null values:

✅ Verification successful

The null check is correctly implemented and necessary

The verification shows that:

  1. The published-time property is populated via awpcp_get_ad_share_info() in the find_current_listing() method
  2. This property is used in both OpenGraph meta tags and date formatting
  3. The null check prevents PHP notices when the listing data is not found or dates are not set

The implementation is safe because:

  • It maintains backward compatibility by returning the original date
  • It's consistent with the error handling pattern in the codebase
  • The property can legitimately be empty when listing data is not found
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for direct calls to get_the_date to understand the impact
rg "get_the_date" --type php

# Search for assignments to published-time property to understand when it might be null
ast-grep --pattern 'published-time' 

Length of output: 263


Script:

#!/bin/bash
# Let's check where the properties array is set and how published-time is populated
rg "'properties'" --type php -B 2 -A 2

# Also check for any date-related property assignments
rg "->properties\[.*time" --type php -B 2 -A 2

Length of output: 1118


Script:

#!/bin/bash
# Let's try a different approach to find property assignments
rg "\\\$this->properties\[" --type php -B 2 -A 2

# Also check the constructor or initialization of the Meta class
ast-grep --pattern 'class AWPCP_Meta {
  $$$
}'

Length of output: 2514


Script:

#!/bin/bash
# Let's check for the constructor or set_properties method
rg "function __construct|function set_properties" --type php -B 2 -A 10 frontend/class-awpcp-meta.php

# Also check where this class is instantiated
rg "new AWPCP_Meta" --type php -B 2 -A 2

Length of output: 3215


Script:

#!/bin/bash
# Let's check the configure method where properties might be set
rg "function configure" --type php -B 2 -A 20 frontend/class-awpcp-meta.php

# Also check for any listing-related methods that might set these properties
rg "set.*listing|get.*listing" --type php -B 2 -A 10 frontend/class-awpcp-meta.php

Length of output: 6487

@sorinmarta sorinmarta requested a review from shervElmi December 11, 2024 15:15
Copy link
Contributor

@shervElmi shervElmi left a comment

Choose a reason for hiding this comment

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

Thanks @sorinmarta, it looks great to me! 🚀

frontend/class-awpcp-meta.php Show resolved Hide resolved
frontend/class-awpcp-meta.php Show resolved Hide resolved
@sorinmarta
Copy link
Contributor Author

Thanks @shervElmi!

@sorinmarta sorinmarta merged commit dd9da12 into master Dec 11, 2024
5 checks passed
@sorinmarta sorinmarta deleted the fix-3174 branch December 11, 2024 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run analysis Run the phpcs run tests Run the unit tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants