Skip to content

Ensure coupon codes are uppercase#2254

Merged
glennjacobs merged 17 commits into1.xfrom
ensure-coupon-codes-are-uppercase
Aug 14, 2025
Merged

Ensure coupon codes are uppercase#2254
glennjacobs merged 17 commits into1.xfrom
ensure-coupon-codes-are-uppercase

Conversation

@alecritson
Copy link
Collaborator

@alecritson alecritson commented Jul 23, 2025

Should Close #2252 by ensuring coupons are stored and retrieved uppercase regardless of where they are introduced.

Summary by CodeRabbit

  • New Features

    • Coupon codes are now automatically converted to uppercase when applied to carts and discounts.
  • Bug Fixes

    • Adjusted tests to expect coupon codes in uppercase format.
  • Chores

    • Removed unnecessary test group annotation.

@alecritson alecritson requested a review from glennjacobs July 23, 2025 12:21
@vercel
Copy link

vercel bot commented Jul 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 25, 2025 1:06pm

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 23, 2025

Walkthrough

This change introduces a custom Eloquent attribute cast, CouponString, which automatically transforms coupon code attributes to uppercase when getting or setting them in the Cart and Discount models. Related unit tests are updated to expect uppercase coupon codes, and a test group annotation is removed. No other logic or method changes are made.

Changes

File(s) Change Summary
packages/core/src/Base/Casts/CouponString.php Adds new CouponString class implementing CastsAttributes, converting values to uppercase.
packages/core/src/Models/Cart.php
packages/core/src/Models/Discount.php
Applies the CouponString cast to coupon code attributes in Cart and Discount models.
tests/core/Unit/Models/CartTest.php Updates test assertion to expect uppercase coupon code.
tests/core/Unit/DiscountTypes/AbstractDiscountTypeTest.php Removes group annotation from a test case.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c1f6d8a and eddd5e7.

📒 Files selected for processing (3)
  • packages/core/src/Base/Casts/CouponString.php (1 hunks)
  • packages/core/src/Models/Cart.php (2 hunks)
  • packages/core/src/Models/Discount.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/core/src/Models/Discount.php
  • packages/core/src/Models/Cart.php
🧰 Additional context used
🪛 PHPMD (2.15.0)
packages/core/src/Base/Casts/CouponString.php

10-10: Avoid unused parameters such as '$model'. (Unused Code Rules)

(UnusedFormalParameter)


10-10: Avoid unused parameters such as '$key'. (Unused Code Rules)

(UnusedFormalParameter)


10-10: Avoid unused parameters such as '$attributes'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$model'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$key'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$attributes'. (Unused Code Rules)

(UnusedFormalParameter)

🔇 Additional comments (4)
packages/core/src/Base/Casts/CouponString.php (4)

1-8: LGTM! Clean class structure following Laravel casting conventions.

The namespace, imports, and class declaration are well-structured and follow Laravel's Eloquent casting patterns.


10-13: LGTM! Correct implementation of the get() method.

The null handling and uppercase transformation logic are implemented correctly. The unused parameters flagged by static analysis are required by the CastsAttributes interface and cannot be removed.


15-18: LGTM! Consistent implementation ensures bidirectional uppercase transformation.

The set() method correctly mirrors the get() method logic, ensuring coupon codes are normalized to uppercase both when storing and retrieving values. The unused parameters are required by the interface.


20-23: LGTM! Safe uppercase transformation that preserves non-ASCII characters.

Using Str::upper() is the correct approach here, as it only transforms ASCII characters to uppercase while leaving non-ASCII characters (emojis, international characters) unchanged. This addresses the concerns discussed in the PR about handling diverse coupon code formats safely.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ensure-coupon-codes-are-uppercase

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @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
Contributor

@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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 60ac49f and 4998e17.

📒 Files selected for processing (2)
  • packages/core/src/Models/Cart.php (2 hunks)
  • packages/core/src/Models/Discount.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (16)
  • GitHub Check: stripe - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.4 - L12.* ↑
  • GitHub Check: search - PHP 8.3 - L12.* ↑ E
  • GitHub Check: shipping - PHP 8.3 - L12.* ↑ E
  • GitHub Check: admin - PHP 8.4 - L12.* ↑ E
  • GitHub Check: search - PHP 8.4 - L11.* ↑
  • GitHub Check: core - PHP 8.4 - L11.* ↑
  • GitHub Check: core - PHP 8.4 - L11.* ↑ E
  • GitHub Check: core - PHP 8.3 - L12.* ↑ E
  • GitHub Check: core - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.3 - L11.* ↑ E
  • GitHub Check: admin - PHP 8.3 - L12.* ↑ E
  • GitHub Check: core - PHP 8.3 - L11.* ↑ E
  • GitHub Check: admin - PHP 8.3 - L12.* ↑
  • GitHub Check: admin - PHP 8.3 - L11.* ↑
  • GitHub Check: core - PHP 8.3 - L11.* ↑
🔇 Additional comments (2)
packages/core/src/Models/Cart.php (1)

8-8: LGTM!

The import is correctly added and necessary for the new Attribute casting functionality.

packages/core/src/Models/Discount.php (1)

6-6: LGTM!

The import is correctly added and necessary for the new Attribute casting functionality.

@glennjacobs
Copy link
Contributor

@alecritson I think this is OK, as long as we restrict coupon codes to ASCII-only. I'm guessing we don't have that in place currently.

@alecritson
Copy link
Collaborator Author

@alecritson I think this is OK, as long as we restrict coupon codes to ASCII-only. I'm guessing we don't have that in place currently.

We don't have that in place currently afaik so it would be it's own feature if required.

@glennjacobs
Copy link
Contributor

@alecritson I think this is OK, as long as we restrict coupon codes to ASCII-only. I'm guessing we don't have that in place currently.

We don't have that in place currently afaik so it would be it's own feature if required.

It's not really a separate feature. This PR can't be merged if we're uppercasing non-ASCII characters.

@alecritson alecritson marked this pull request as draft July 24, 2025 11:53
@alecritson alecritson marked this pull request as ready for review July 25, 2025 08:06
Copy link
Contributor

@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: 1

🧹 Nitpick comments (1)
tests/core/Unit/Base/Casts/UppercaseAsciiTest.php (1)

102-114: Consider adding tests for potential problematic Unicode sequences.

While the coverage is excellent, consider adding tests for edge cases like:

  • RTL characters
  • Emoji characters
  • Combining diacritical marks
  • Very long Unicode strings
// Additional test cases to consider:
['🎉party', 'PARTY'], // Emoji handling
['café́', 'CAFE'], // Combining diacritical marks
['متجر', 'MTR'], // RTL script (Arabic)
['お店', ''], // Non-Latin script handling
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9a3fa7a and 6cd5223.

📒 Files selected for processing (5)
  • packages/core/src/Base/Casts/UppercaseAscii.php (1 hunks)
  • packages/core/src/Models/Cart.php (2 hunks)
  • packages/core/src/Models/Discount.php (2 hunks)
  • tests/core/Unit/Base/Casts/UppercaseAsciiTest.php (1 hunks)
  • tests/core/Unit/DiscountTypes/AbstractDiscountTypeTest.php (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/core/Unit/DiscountTypes/AbstractDiscountTypeTest.php
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/core/src/Models/Discount.php
  • packages/core/src/Models/Cart.php
🧰 Additional context used
🪛 PHPMD (2.15.0)
packages/core/src/Base/Casts/UppercaseAscii.php

10-10: Avoid unused parameters such as '$model'. (Unused Code Rules)

(UnusedFormalParameter)


10-10: Avoid unused parameters such as '$key'. (Unused Code Rules)

(UnusedFormalParameter)


10-10: Avoid unused parameters such as '$attributes'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$model'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$key'. (Unused Code Rules)

(UnusedFormalParameter)


15-15: Avoid unused parameters such as '$attributes'. (Unused Code Rules)

(UnusedFormalParameter)

🔇 Additional comments (5)
packages/core/src/Base/Casts/UppercaseAscii.php (4)

8-28: Excellent approach to handle non-ASCII characters - addresses PR discussion concerns.

The implementation elegantly solves the Unicode character issue raised in the PR discussion by using transliteration to convert non-ASCII characters to their ASCII equivalents before uppercasing. This ensures consistent handling regardless of input character encoding.


10-18: Interface compliance requires these parameters - static analysis warnings are false positives.

The unused parameter warnings from PHPMD are false positives. These parameters ($model, $key, $attributes) are required by the CastsAttributes interface contract and cannot be removed.


23-24: Character replacement logic handles edge cases well.

The apostrophe variants and space/hyphen replacement logic is comprehensive and handles various Unicode quotation marks that could appear in coupon codes.


22-22: Intl extension requirement verified
The ext-intl dependency is already declared in composer.json and documented in both docs/core/installation.md and docs/core/starter-kits.md. No further action needed.

tests/core/Unit/Base/Casts/UppercaseAsciiTest.php (1)

7-115: Excellent comprehensive test coverage for Unicode transliteration.

The test suite provides outstanding coverage of various Unicode character scenarios, demonstrating that the transliteration approach effectively handles the non-ASCII character concerns raised in the PR discussion.

@glennjacobs
Copy link
Contributor

@alecritson can you explain the recent updates for me?

@glennjacobs
Copy link
Contributor

@alecritson
Copy link
Collaborator Author

alecritson commented Jul 25, 2025

I think this is OK, as long as we restrict coupon codes to ASCII-only. I'm guessing we don't have that in place currently.

It's not really a separate feature. This PR can't be merged if we're uppercasing non-ASCII characters.

@glennjacobs
So the changes should make sure this is handled, as the tests should demonstrate.

@glennjacobs
Copy link
Contributor

I think this is OK, as long as we restrict coupon codes to ASCII-only. I'm guessing we don't have that in place currently.

It's not really a separate feature. This PR can't be merged if we're uppercasing non-ASCII characters.

@glennjacobs So the changes should make sure this is handled, as the tests should demonstrate.

I think I've got this wrong. I'll do some research and come back.

@alecritson alecritson marked this pull request as draft July 25, 2025 12:59
@glennjacobs
Copy link
Contributor

@alecritson

So initially I was concerned that uppercasing everything would fundamentally change the code. E.g. ß would become SS.

However, using strtoupper()only uppercases ASCII characters, so I think we were OK in the first place, doh!

E.g.

Str::upper('🏆promo');     // '🏆PROMO'

Sorry, I think we simply need to revert the recent changes...

@alecritson alecritson marked this pull request as ready for review July 25, 2025 13:11
@alecritson
Copy link
Collaborator Author

@alecritson

So initially I was concerned that uppercasing everything would fundamentally change the code. E.g. ß would become SS.

However, using strtoupper()only uppercases ASCII characters, so I think we were OK in the first place, doh!

E.g.

Str::upper('🏆promo');     // '🏆PROMO'

Sorry, I think we simply need to revert the recent changes...

Reverted but kept the casting class, just renamed for future scaling if needed.

@glennjacobs glennjacobs merged commit 402ccac into 1.x Aug 14, 2025
48 checks passed
@glennjacobs glennjacobs deleted the ensure-coupon-codes-are-uppercase branch August 14, 2025 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Case‑sensitivity issue in CouponValidator

3 participants