-
Notifications
You must be signed in to change notification settings - Fork 29
Truncate state_or_province to two characters #43
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
base: develop
Are you sure you want to change the base?
Conversation
Some countries will have state_or_province codes that are longer than two characters. FedEx's API will fail if this value is longer than two. Truncate the string to two characters to avoid such errors.
Summary of ChangesHello @damienmckenna, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a crucial fix to ensure compatibility with the FedEx API. It addresses an issue where FedEx's API would reject requests containing Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request aims to fix an issue with the FedEx API by truncating the state_or_province field to two characters. While the intention is good, the current implementation has a critical flaw: it applies this truncation universally, which will corrupt addresses for countries that use longer state/province codes (e.g., Australia). My review provides feedback on how to address this by making the logic conditional on the country code to avoid silent data corruption and potential shipping failures.
src/FedexRest/Entity/Address.php
Outdated
| // Truncate the state/provice code to work around a limitation in FedEx's API. | ||
| $address['stateOrProvinceCode'] = substr($this->state_or_province, 0, 2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces a significant issue by unconditionally truncating the state_or_province to two characters. This will lead to incorrect addresses for countries that use state/province codes longer than two characters, such as Australia (e.g., 'NSW', 'QLD'). Sending incorrect data is often worse than getting an API error, as it can lead to failed deliveries or other hard-to-debug issues.
A safer approach would be to make the truncation logic conditional on the country code ($this->country_code). For example, you could apply this only for countries where you are certain a two-character code is required, like the US and Canada. Your implementation could look like this, replacing the current line:
if (!empty($this->country_code) && in_array($this->country_code, ['US', 'CA'])) {
$address['stateOrProvinceCode'] = substr($this->state_or_province, 0, 2);
} else {
$address['stateOrProvinceCode'] = $this->state_or_province;
}Also, consider using mb_substr() for multi-byte character safety, although it's less critical for state codes.
Finally, there's a typo in the comment on line 84: provice should be province.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed the comment typo.
The suggestion for making the logic conditional misses the point - it's not that the string must be two characters long, it's that it must not be longer than two digits, so will fail on valid strings like "QLD" for Queensland. In this case working around an API limitation is a simple approach that catches all scenarios.
Some countries will have state_or_province codes that are longer than two characters. FedEx's API will fail if this value is longer than two. Truncate the string to two characters to avoid such errors.
Resolves #42.