From b735d29330998741b1bfd4fdca56a0008ee26acb Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 13:49:02 +0200 Subject: [PATCH 01/10] security-package/issues/127: Updated Save Config Validation class. --- Securitytxt/Model/Config/Backend/Validate.php | 83 +++++++++++++++---- 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 30e78bcf..5adf87f1 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -35,26 +35,47 @@ public function validateBeforeSave() $dataGroup = $this->getData()['groups']; $contactInformationFields = $dataGroup['contact_information']['fields']; $otherInformationFields = $dataGroup['other_information']['fields']; - $isExtensionEnabled = (bool)$dataGroup['general']['fields']['enabled']['value']; - $contactEmail = $contactInformationFields['email']['value']; - $contactPhone = $contactInformationFields['phone']['value']; - $contactWebPage = $contactInformationFields['contact_page']['value']; + $isEnabledField = $dataGroup['general']['fields']['enabled']; - if ($isExtensionEnabled) { - if ($contactEmail === '' && $contactPhone === '' && $contactWebPage === '') { + if ($this->existValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { + if (!$this->existValue($contactInformationFields['email']) + && !$this->existValue($contactInformationFields['phone']) + && !$this->existValue($contactInformationFields['contact_page'])) { throw new ValidatorException(__('At least one contact information is required.')); } - } else { - return parent::validateBeforeSave(); } - $this->validateContactEmail($contactEmail); - $this->validateContactWebpageUrl($contactWebPage); - $this->validateUrlField("Contact Page URL", $contactWebPage); - $this->validateUrlField("Encryption URL", $otherInformationFields['encryption']['value']); - $this->validateUrlField("Acknowledgements URL", $otherInformationFields['acknowledgements']['value']); - $this->validateUrlField("Hiring URL", $otherInformationFields['hiring']['value']); - $this->validateUrlField("Policy URL", $otherInformationFields['policy']['value']); + /** + * Validate Email + */ + if ($this->existValue($contactInformationFields['email'])) { + $this->validateContactEmail($this->getDataValue($contactInformationFields['email'])); + } + + /** + * Validate Contact URL + */ + if ($this->existValue($contactInformationFields['contact_page'])) { + $this->validateContactWebpageUrl($this->getDataValue($contactInformationFields['contact_page'])); + } + + /** + * Validate Other Information URLs + */ + ($this->getDataValue($otherInformationFields['acknowledgements']) != '') ? $this->validateUrlField( + "Acknowledgements URL", + $this->getDataValue($otherInformationFields['acknowledgements']) + ) : true; + + ($this->getDataValue($otherInformationFields['hiring']) != '') ? $this->validateUrlField( + "Hiring URL", + $this->getDataValue($otherInformationFields['hiring']) + ) : true; + + ($this->getDataValue($otherInformationFields['policy']) != '') ? $this->validateUrlField( + "Policy URL", + $this->getDataValue($otherInformationFields['policy']) + ) : true; return parent::validateBeforeSave(); } @@ -123,4 +144,36 @@ private function validateUrlField(string $fieldName, string $fieldValue): void ); } } + + /** + * Get Value from form or inheriting value. + * + * @param array $fieldData + * @return string + */ + private function getDataValue(array $fieldData): string + { + $result = ''; + if (isset($fieldData['value'])) { + $result = $fieldData['value']; + } + + return $result; + } + + /** + * Check exists value data + * + * @param array $fieldData + * @return bool + */ + private function existValue(array $fieldData): bool + { + if (isset($fieldData['value']) && $fieldData['value'] !== '') { + return true; + } + + return false; + } + } From 2c625d1ab8fce90e6b6a046e258136970b46017c Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 13:49:02 +0200 Subject: [PATCH 02/10] security-package/issues/127: Updated Save Config Validation class. --- Securitytxt/Model/Config/Backend/Validate.php | 83 +++++++++++++++---- 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 30e78bcf..035390f6 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -35,26 +35,47 @@ public function validateBeforeSave() $dataGroup = $this->getData()['groups']; $contactInformationFields = $dataGroup['contact_information']['fields']; $otherInformationFields = $dataGroup['other_information']['fields']; - $isExtensionEnabled = (bool)$dataGroup['general']['fields']['enabled']['value']; - $contactEmail = $contactInformationFields['email']['value']; - $contactPhone = $contactInformationFields['phone']['value']; - $contactWebPage = $contactInformationFields['contact_page']['value']; + $isEnabledField = $dataGroup['general']['fields']['enabled']; - if ($isExtensionEnabled) { - if ($contactEmail === '' && $contactPhone === '' && $contactWebPage === '') { + if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { + if (!$this->existDataValue($contactInformationFields['email']) + && !$this->existDataValue($contactInformationFields['phone']) + && !$this->existDataValue($contactInformationFields['contact_page'])) { throw new ValidatorException(__('At least one contact information is required.')); } - } else { - return parent::validateBeforeSave(); } - $this->validateContactEmail($contactEmail); - $this->validateContactWebpageUrl($contactWebPage); - $this->validateUrlField("Contact Page URL", $contactWebPage); - $this->validateUrlField("Encryption URL", $otherInformationFields['encryption']['value']); - $this->validateUrlField("Acknowledgements URL", $otherInformationFields['acknowledgements']['value']); - $this->validateUrlField("Hiring URL", $otherInformationFields['hiring']['value']); - $this->validateUrlField("Policy URL", $otherInformationFields['policy']['value']); + /** + * Validate Email + */ + if ($this->existDataValue($contactInformationFields['email'])) { + $this->validateContactEmail($this->getDataValue($contactInformationFields['email'])); + } + + /** + * Validate Contact URL + */ + if ($this->existDataValue($contactInformationFields['contact_page'])) { + $this->validateContactWebpageUrl($this->getDataValue($contactInformationFields['contact_page'])); + } + + /** + * Validate Other Information URLs + */ + ($this->getDataValue($otherInformationFields['acknowledgements']) != '') ? $this->validateUrlField( + "Acknowledgements URL", + $this->getDataValue($otherInformationFields['acknowledgements']) + ) : true; + + ($this->getDataValue($otherInformationFields['hiring']) != '') ? $this->validateUrlField( + "Hiring URL", + $this->getDataValue($otherInformationFields['hiring']) + ) : true; + + ($this->getDataValue($otherInformationFields['policy']) != '') ? $this->validateUrlField( + "Policy URL", + $this->getDataValue($otherInformationFields['policy']) + ) : true; return parent::validateBeforeSave(); } @@ -123,4 +144,36 @@ private function validateUrlField(string $fieldName, string $fieldValue): void ); } } + + /** + * Get Value from form or inheriting value. + * + * @param array $fieldData + * @return string + */ + private function getDataValue(array $fieldData): string + { + $result = ''; + if (isset($fieldData['value'])) { + $result = $fieldData['value']; + } + + return $result; + } + + /** + * Check exists value data + * + * @param array $fieldData + * @return bool + */ + private function existDataValue(array $fieldData): bool + { + if (isset($fieldData['value']) && $fieldData['value'] !== '') { + return true; + } + + return false; + } + } From 582fd0978eec508ee7ead5e958defa73d2e437c8 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 13:55:20 +0200 Subject: [PATCH 03/10] security-package/issues/127: Updated Save Config Validation class. --- Securitytxt/Model/Config/Backend/Validate.php | 83 +++++++++++++++---- 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 30e78bcf..035390f6 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -35,26 +35,47 @@ public function validateBeforeSave() $dataGroup = $this->getData()['groups']; $contactInformationFields = $dataGroup['contact_information']['fields']; $otherInformationFields = $dataGroup['other_information']['fields']; - $isExtensionEnabled = (bool)$dataGroup['general']['fields']['enabled']['value']; - $contactEmail = $contactInformationFields['email']['value']; - $contactPhone = $contactInformationFields['phone']['value']; - $contactWebPage = $contactInformationFields['contact_page']['value']; + $isEnabledField = $dataGroup['general']['fields']['enabled']; - if ($isExtensionEnabled) { - if ($contactEmail === '' && $contactPhone === '' && $contactWebPage === '') { + if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { + if (!$this->existDataValue($contactInformationFields['email']) + && !$this->existDataValue($contactInformationFields['phone']) + && !$this->existDataValue($contactInformationFields['contact_page'])) { throw new ValidatorException(__('At least one contact information is required.')); } - } else { - return parent::validateBeforeSave(); } - $this->validateContactEmail($contactEmail); - $this->validateContactWebpageUrl($contactWebPage); - $this->validateUrlField("Contact Page URL", $contactWebPage); - $this->validateUrlField("Encryption URL", $otherInformationFields['encryption']['value']); - $this->validateUrlField("Acknowledgements URL", $otherInformationFields['acknowledgements']['value']); - $this->validateUrlField("Hiring URL", $otherInformationFields['hiring']['value']); - $this->validateUrlField("Policy URL", $otherInformationFields['policy']['value']); + /** + * Validate Email + */ + if ($this->existDataValue($contactInformationFields['email'])) { + $this->validateContactEmail($this->getDataValue($contactInformationFields['email'])); + } + + /** + * Validate Contact URL + */ + if ($this->existDataValue($contactInformationFields['contact_page'])) { + $this->validateContactWebpageUrl($this->getDataValue($contactInformationFields['contact_page'])); + } + + /** + * Validate Other Information URLs + */ + ($this->getDataValue($otherInformationFields['acknowledgements']) != '') ? $this->validateUrlField( + "Acknowledgements URL", + $this->getDataValue($otherInformationFields['acknowledgements']) + ) : true; + + ($this->getDataValue($otherInformationFields['hiring']) != '') ? $this->validateUrlField( + "Hiring URL", + $this->getDataValue($otherInformationFields['hiring']) + ) : true; + + ($this->getDataValue($otherInformationFields['policy']) != '') ? $this->validateUrlField( + "Policy URL", + $this->getDataValue($otherInformationFields['policy']) + ) : true; return parent::validateBeforeSave(); } @@ -123,4 +144,36 @@ private function validateUrlField(string $fieldName, string $fieldValue): void ); } } + + /** + * Get Value from form or inheriting value. + * + * @param array $fieldData + * @return string + */ + private function getDataValue(array $fieldData): string + { + $result = ''; + if (isset($fieldData['value'])) { + $result = $fieldData['value']; + } + + return $result; + } + + /** + * Check exists value data + * + * @param array $fieldData + * @return bool + */ + private function existDataValue(array $fieldData): bool + { + if (isset($fieldData['value']) && $fieldData['value'] !== '') { + return true; + } + + return false; + } + } From 88cc92ba96ab4867bb04e4773c6dab1f2e5b6de8 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 14:30:48 +0200 Subject: [PATCH 04/10] security-package/issues/127: Reformat code --- Securitytxt/Model/Config/Backend/Validate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 035390f6..aa0079d4 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -169,7 +169,7 @@ private function getDataValue(array $fieldData): string */ private function existDataValue(array $fieldData): bool { - if (isset($fieldData['value']) && $fieldData['value'] !== '') { + if (isset($fieldData['value']) && empty($fieldData['value']) === true) { return true; } From e036b13938a795e18bf19cef3c7d3a5009d010da Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 14:34:46 +0200 Subject: [PATCH 05/10] security-package/issues/127: Reformat code --- Securitytxt/Model/Config/Backend/Validate.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index aa0079d4..4b07d0ed 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -48,6 +48,7 @@ public function validateBeforeSave() /** * Validate Email */ + if ($this->existDataValue($contactInformationFields['email'])) { $this->validateContactEmail($this->getDataValue($contactInformationFields['email'])); } @@ -169,7 +170,7 @@ private function getDataValue(array $fieldData): string */ private function existDataValue(array $fieldData): bool { - if (isset($fieldData['value']) && empty($fieldData['value']) === true) { + if (isset($fieldData['value']) && $fieldData['value'] !== '') { return true; } From fc94ae72868e2cc6c8e5144955f9ad02bba93b34 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Fri, 13 Mar 2020 17:24:55 +0200 Subject: [PATCH 06/10] security-package/issues/127: Reformat code --- Securitytxt/Model/Config/Backend/Validate.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 4b07d0ed..945f866a 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -38,9 +38,9 @@ public function validateBeforeSave() $isEnabledField = $dataGroup['general']['fields']['enabled']; if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { - if (!$this->existDataValue($contactInformationFields['email']) - && !$this->existDataValue($contactInformationFields['phone']) - && !$this->existDataValue($contactInformationFields['contact_page'])) { + if ($this->isEmptyValue('email', $contactInformationFields) + && $this->isEmptyValue('phone', $contactInformationFields) + && $this->isEmptyValue('contact_page', $contactInformationFields)) { throw new ValidatorException(__('At least one contact information is required.')); } } @@ -170,7 +170,23 @@ private function getDataValue(array $fieldData): string */ private function existDataValue(array $fieldData): bool { - if (isset($fieldData['value']) && $fieldData['value'] !== '') { + if (isset($fieldData['value'])) { + if ($fieldData['value'] !== '' || empty($fieldData['value'])) { + return true; + } + } + + return false; + } + + /** + * @param string $key + * @param array $fieldData + * @return bool + */ + private function isEmptyValue(string $key, array $fieldData): bool + { + if ($this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === '') { return true; } From 80e432d44e4f9ddc7050f0e1f0af107ccab89743 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Tue, 17 Mar 2020 15:23:58 +0200 Subject: [PATCH 07/10] security-package/issues/127: Refactoring --- Securitytxt/Model/Config/Backend/Validate.php | 87 +++++++++++++------ 1 file changed, 61 insertions(+), 26 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 945f866a..70f113a3 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -37,46 +37,38 @@ public function validateBeforeSave() $otherInformationFields = $dataGroup['other_information']['fields']; $isEnabledField = $dataGroup['general']['fields']['enabled']; - if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { - if ($this->isEmptyValue('email', $contactInformationFields) - && $this->isEmptyValue('phone', $contactInformationFields) - && $this->isEmptyValue('contact_page', $contactInformationFields)) { - throw new ValidatorException(__('At least one contact information is required.')); - } + if ($this->isEnabledDataValue($isEnabledField) + && $this->isEmptyContactInformationFields($contactInformationFields)) { + throw new ValidatorException(__('At least one contact information is required.')); } /** * Validate Email */ - - if ($this->existDataValue($contactInformationFields['email'])) { - $this->validateContactEmail($this->getDataValue($contactInformationFields['email'])); - } + $this->validateContactEmail($contactInformationFields['email']); /** * Validate Contact URL */ - if ($this->existDataValue($contactInformationFields['contact_page'])) { - $this->validateContactWebpageUrl($this->getDataValue($contactInformationFields['contact_page'])); - } + $this->validateContactWebPageUrl($contactInformationFields['contact_page']); /** * Validate Other Information URLs */ - ($this->getDataValue($otherInformationFields['acknowledgements']) != '') ? $this->validateUrlField( + $this->validateUrlField( "Acknowledgements URL", $this->getDataValue($otherInformationFields['acknowledgements']) - ) : true; + ); - ($this->getDataValue($otherInformationFields['hiring']) != '') ? $this->validateUrlField( + $this->validateUrlField( "Hiring URL", $this->getDataValue($otherInformationFields['hiring']) - ) : true; + ); - ($this->getDataValue($otherInformationFields['policy']) != '') ? $this->validateUrlField( + $this->validateUrlField( "Policy URL", $this->getDataValue($otherInformationFields['policy']) - ) : true; + ); return parent::validateBeforeSave(); } @@ -101,12 +93,17 @@ private function validateSecureUrl(string $url): bool /** * Validate contact email configuration field. * - * @param string $contactEmail - * @return void + * @param array $contactEmailFieldData * @throws ValidatorException */ - private function validateContactEmail(string $contactEmail): void + private function validateContactEmail(array $contactEmailFieldData): void { + $contactEmail = ''; + + if ($this->existDataValue($contactEmailFieldData)) { + $contactEmail = $this->getDataValue($contactEmailFieldData); + } + if ($contactEmail !== '' && !filter_var($contactEmail, FILTER_VALIDATE_EMAIL)) { throw new ValidatorException( __('Contact Information: Email validation failed. Please enter in correct format.') @@ -117,13 +114,18 @@ private function validateContactEmail(string $contactEmail): void /** * Validate contact web page configuration field. * - * @param string $contactWebpage - * @return void + * @param array $contactWebPageFieldData * @throws ValidatorException */ - private function validateContactWebpageUrl(string $contactWebpage): void + private function validateContactWebPageUrl(array $contactWebPageFieldData): void { - if ($contactWebpage !== '' && !$this->validateSecureUrl($contactWebpage)) { + $contactWebPage = ''; + + if ($this->existDataValue($contactWebPageFieldData)) { + $contactWebPage = $this->getDataValue($contactWebPageFieldData); + } + + if ($contactWebPage !== '' && !$this->validateSecureUrl($contactWebPage)) { throw new ValidatorException( __('Contact Information: Contact Page URL should be in correct format and must start with HTTPS.') ); @@ -180,6 +182,8 @@ private function existDataValue(array $fieldData): bool } /** + * Check is Empty value + * * @param string $key * @param array $fieldData * @return bool @@ -193,4 +197,35 @@ private function isEmptyValue(string $key, array $fieldData): bool return false; } + /** + * Check for Empty Contact Information fields + * + * @param array $contactInformationFields + * @return bool + */ + private function isEmptyContactInformationFields(array $contactInformationFields): bool + { + if ($this->isEmptyValue('email', $contactInformationFields) + && $this->isEmptyValue('phone', $contactInformationFields) + && $this->isEmptyValue('contact_page', $contactInformationFields)) { + return true; + } + + return false; + } + + /** + * Check if exist data value Enabled form value + * + * @param array $isEnabledField + * @return bool + */ + private function isEnabledDataValue(array $isEnabledField): bool + { + if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { + return true; + } + + return false; + } } From a29d3c927ede69fb3a6a914df0416a82c2286b11 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Tue, 17 Mar 2020 17:36:40 +0200 Subject: [PATCH 08/10] security-package/issues/127: Refactoring --- Securitytxt/Model/Config/Backend/Validate.php | 43 +++++-------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 70f113a3..cbaf86ca 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -98,10 +98,10 @@ private function validateSecureUrl(string $url): bool */ private function validateContactEmail(array $contactEmailFieldData): void { - $contactEmail = ''; - if ($this->existDataValue($contactEmailFieldData)) { $contactEmail = $this->getDataValue($contactEmailFieldData); + } else { + $contactEmail = ''; } if ($contactEmail !== '' && !filter_var($contactEmail, FILTER_VALIDATE_EMAIL)) { @@ -119,10 +119,10 @@ private function validateContactEmail(array $contactEmailFieldData): void */ private function validateContactWebPageUrl(array $contactWebPageFieldData): void { - $contactWebPage = ''; - if ($this->existDataValue($contactWebPageFieldData)) { $contactWebPage = $this->getDataValue($contactWebPageFieldData); + } else { + $contactWebPage = ''; } if ($contactWebPage !== '' && !$this->validateSecureUrl($contactWebPage)) { @@ -156,12 +156,7 @@ private function validateUrlField(string $fieldName, string $fieldValue): void */ private function getDataValue(array $fieldData): string { - $result = ''; - if (isset($fieldData['value'])) { - $result = $fieldData['value']; - } - - return $result; + return isset($fieldData['value']) ? $fieldData['value'] : ''; } /** @@ -172,13 +167,7 @@ private function getDataValue(array $fieldData): string */ private function existDataValue(array $fieldData): bool { - if (isset($fieldData['value'])) { - if ($fieldData['value'] !== '' || empty($fieldData['value'])) { - return true; - } - } - - return false; + return isset($fieldData['value']) && ($fieldData['value'] !== '' || empty($fieldData['value'])); } /** @@ -190,11 +179,7 @@ private function existDataValue(array $fieldData): bool */ private function isEmptyValue(string $key, array $fieldData): bool { - if ($this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === '') { - return true; - } - - return false; + return ($this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === ''); } /** @@ -205,13 +190,9 @@ private function isEmptyValue(string $key, array $fieldData): bool */ private function isEmptyContactInformationFields(array $contactInformationFields): bool { - if ($this->isEmptyValue('email', $contactInformationFields) + return ($this->isEmptyValue('email', $contactInformationFields) && $this->isEmptyValue('phone', $contactInformationFields) - && $this->isEmptyValue('contact_page', $contactInformationFields)) { - return true; - } - - return false; + && $this->isEmptyValue('contact_page', $contactInformationFields)); } /** @@ -222,10 +203,6 @@ private function isEmptyContactInformationFields(array $contactInformationFields */ private function isEnabledDataValue(array $isEnabledField): bool { - if ($this->existDataValue($isEnabledField) && (bool)$this->getDataValue($isEnabledField) == true) { - return true; - } - - return false; + return ($this->existDataValue($isEnabledField) && $this->getDataValue($isEnabledField)); } } From 24cad2425a1c1d0c992c02d8b0f24f26b3a265c1 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Tue, 24 Mar 2020 15:51:53 +0200 Subject: [PATCH 09/10] security-package/issues/127: Reformat code --- Securitytxt/Model/Config/Backend/Validate.php | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index cbaf86ca..51b97ed8 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -22,6 +22,8 @@ class Validate extends Value * * @return Value * @throws ValidatorException + * @SuppressWarnings(PHPMD.NPathComplexity) + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function validateBeforeSave() { @@ -42,33 +44,34 @@ public function validateBeforeSave() throw new ValidatorException(__('At least one contact information is required.')); } - /** - * Validate Email - */ - $this->validateContactEmail($contactInformationFields['email']); - - /** - * Validate Contact URL - */ - $this->validateContactWebPageUrl($contactInformationFields['contact_page']); - - /** - * Validate Other Information URLs - */ - $this->validateUrlField( - "Acknowledgements URL", - $this->getDataValue($otherInformationFields['acknowledgements']) - ); - - $this->validateUrlField( - "Hiring URL", - $this->getDataValue($otherInformationFields['hiring']) - ); - - $this->validateUrlField( - "Policy URL", - $this->getDataValue($otherInformationFields['policy']) - ); + if (isset($contactInformationFields['email'])) { + $this->validateContactEmail($contactInformationFields['email']); + } + + if (isset($contactInformationFields['contact_page'])) { + $this->validateContactWebPageUrl($contactInformationFields['contact_page']); + } + + if (isset($otherInformationFields['acknowledgements'])) { + $this->validateUrlField( + "Acknowledgements URL", + $this->getDataValue($otherInformationFields['acknowledgements']) + ); + } + + if (isset($otherInformationFields['hiring'])) { + $this->validateUrlField( + "Hiring URL", + $this->getDataValue($otherInformationFields['hiring']) + ); + } + + if (isset($otherInformationFields['policy'])) { + $this->validateUrlField( + "Policy URL", + $this->getDataValue($otherInformationFields['policy']) + ); + } return parent::validateBeforeSave(); } @@ -179,7 +182,7 @@ private function existDataValue(array $fieldData): bool */ private function isEmptyValue(string $key, array $fieldData): bool { - return ($this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === ''); + return (isset($fieldData[$key]) && $this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === ''); } /** From 3e5a342c6c28e9384defae88338ce5a29f037a97 Mon Sep 17 00:00:00 2001 From: Alexander Steshuk Date: Thu, 2 Apr 2020 14:18:43 +0300 Subject: [PATCH 10/10] Fix static test --- Securitytxt/Model/Config/Backend/Validate.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Securitytxt/Model/Config/Backend/Validate.php b/Securitytxt/Model/Config/Backend/Validate.php index 51b97ed8..fa08d8c5 100644 --- a/Securitytxt/Model/Config/Backend/Validate.php +++ b/Securitytxt/Model/Config/Backend/Validate.php @@ -182,7 +182,9 @@ private function existDataValue(array $fieldData): bool */ private function isEmptyValue(string $key, array $fieldData): bool { - return (isset($fieldData[$key]) && $this->existDataValue($fieldData[$key]) && $this->getDataValue($fieldData[$key]) === ''); + return (isset($fieldData[$key]) + && $this->existDataValue($fieldData[$key]) + && $this->getDataValue($fieldData[$key]) === ''); } /**