-
-
Notifications
You must be signed in to change notification settings - Fork 450
Sanitize Input Data: Trim Whitespace on Key Backend Models for Consistent Data Storage #4956
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: main
Are you sure you want to change the base?
Changes from all commits
aa84fc8
2cb504f
2553366
b40b707
2b89804
5466aaf
fdc5bc0
89ca17d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -122,11 +122,19 @@ public function getOrder() | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||
* Before object save manipulations | ||||||||||||||||||||||||||||||
* Trim whitespace for all string data to prevent unwanted spaces on save | ||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||
* @return $this | ||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||
protected function _beforeSave() | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
// Trim all string fields before saving (for clean data storage) | ||||||||||||||||||||||||||||||
foreach ($this->getData() as $key => $value) { | ||||||||||||||||||||||||||||||
if (is_string($value)) { | ||||||||||||||||||||||||||||||
$this->setData($key, trim($value)); | ||||||||||||||||||||||||||||||
Comment on lines
+131
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach may be inefficient as it iterates through all data fields and calls setData() for each string field, potentially triggering change detection for every field. Consider only trimming specific known string fields or implementing a more targeted approach.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
parent::_beforeSave(); | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if (!$this->getParentId() && $this->getOrder()) { | ||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,15 @@ protected function _construct() | |
*/ | ||
protected function _beforeSave() | ||
{ | ||
// Trim whitespace for all relevant fields before validation and save | ||
$this->setCode(trim((string) $this->getCode())); | ||
$this->setTaxCountryId(trim((string) $this->getTaxCountryId())); | ||
$this->setTaxRegionId(trim((string) $this->getTaxRegionId())); | ||
$this->setTaxPostcode(trim((string) $this->getTaxPostcode())); | ||
$this->setRate(trim((string) $this->getRate())); | ||
$this->setZipFrom(trim((string) $this->getZipFrom())); | ||
$this->setZipTo(trim((string) $this->getZipTo())); | ||
Comment on lines
+73
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting the trim operation into a helper method to reduce code duplication. The pattern Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
if ($this->getCode() === '' || $this->getTaxCountryId() === '' || $this->getRate() === '' | ||
|| $this->getZipIsRange() && ($this->getZipFrom() === '' || $this->getZipTo() === '') | ||
) { | ||
|
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.
Using array_map with an anonymous function for every array parameter could impact performance for large arrays. Consider using a foreach loop or array_walk for better performance, especially since this method is called frequently for request parameters.
Copilot uses AI. Check for mistakes.