From 6f3d85de8fe0eb5c4397edec2967893e8a0a86aa Mon Sep 17 00:00:00 2001 From: Roberto Rielo Date: Fri, 15 Mar 2019 10:37:42 -0500 Subject: [PATCH] Refactored methods with php7 operators and scalar types (#12) * Rebase with the master branch * Refactored code with coding standard tool * Removed Travis build using 5.6 * Removing php7.0 support * Added new test to cover parameterCount method --- .travis.yml | 2 - composer.json | 7 +- src/Contracts/ConverterHandlerInterface.php | 10 ++- src/Contracts/ValidationRuleInterface.php | 10 ++- src/Converter/JsonConverter.php | 10 ++- src/Converter/XmlConverter.php | 24 +++--- src/Helpers/FormatsMessages.php | 30 ++++--- src/Rules/AsciiOnly.php | 12 +-- src/Rules/Between.php | 12 +-- src/Rules/ClosureValidationRule.php | 14 ++-- src/Rules/Url.php | 12 +-- src/Validator/ValidationRuleParser.php | 10 ++- src/Validator/Validator.php | 87 +++++++++++---------- tests/data/valid_test_expected.xml | 2 +- tests/data/valid_test_param_expected.xml | 2 +- tests/src/Rules/ClosureValidationTest.php | 20 +++++ tests/src/UppercaseRule.php | 8 +- 17 files changed, 159 insertions(+), 113 deletions(-) create mode 100644 tests/src/Rules/ClosureValidationTest.php diff --git a/.travis.yml b/.travis.yml index d2dacad..00caa32 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: php php: - - 5.6 - - 7.0 - 7.1 - 7.2 - nightly diff --git a/composer.json b/composer.json index 2816a16..98b0bca 100755 --- a/composer.json +++ b/composer.json @@ -12,8 +12,11 @@ ], "minimum-stability": "dev", "require": { - "php": ">=5.6", - "ext-mbstring": "*" + "php": "^7.1.3", + "ext-mbstring": "*", + "ext-json": "*", + "ext-simplexml": "*", + "ext-dom": "*" }, "require-dev": { "phpunit/phpunit": "^5.7 || ^6.5 || ^7.0", diff --git a/src/Contracts/ConverterHandlerInterface.php b/src/Contracts/ConverterHandlerInterface.php index 23f0053..2aad379 100755 --- a/src/Contracts/ConverterHandlerInterface.php +++ b/src/Contracts/ConverterHandlerInterface.php @@ -1,5 +1,7 @@ data = json_encode( $data, @@ -46,7 +48,7 @@ public function convert($data) * * @return bool */ - public function write($filename) + public function write(string $filename): bool { return (file_put_contents($filename, $this->data)) ? true : false; } diff --git a/src/Converter/XmlConverter.php b/src/Converter/XmlConverter.php index 6cb5b00..3ed8e54 100755 --- a/src/Converter/XmlConverter.php +++ b/src/Converter/XmlConverter.php @@ -1,5 +1,7 @@ recordElement = $recordElement; } - $this->data = new SimpleXMLElement(''); + $this->data = new SimpleXMLElement(''); } /** * @return string */ - public function getExtension() + public function getExtension(): string { return self::FILE_EXTENSION; } /** - * @param $data - * @param $xmlData + * @param array $data + * @param SimpleXMLElement $xmlData */ - protected function toXml($data, $xmlData) + protected function toXml(array $data, SimpleXMLElement $xmlData): void { foreach ($data as $key => $value) { if (is_numeric($key)) { @@ -66,11 +68,11 @@ protected function toXml($data, $xmlData) } /** - * @param $data + * @param array $data * - * @return $this|mixed + * @return ConverterHandlerInterface */ - public function convert($data) + public function convert(array $data): ConverterHandlerInterface { $this->toXml($data, $this->data); @@ -78,11 +80,11 @@ public function convert($data) } /** - * @param $filename + * @param string $filename * * @return bool */ - public function write($filename) + public function write(string $filename): bool { $dom = new DOMDocument('1.0'); diff --git a/src/Helpers/FormatsMessages.php b/src/Helpers/FormatsMessages.php index 737e6b7..439911c 100755 --- a/src/Helpers/FormatsMessages.php +++ b/src/Helpers/FormatsMessages.php @@ -1,5 +1,7 @@ getInlineMessage($attribute, $actualRule); @@ -34,7 +36,7 @@ protected function getMessage($attribute, $rule, $actualRule) * * @return string|null */ - protected function getInlineMessage($attribute, $rule) + protected function getInlineMessage(string $attribute, string $rule): ?string { return $this->getFromLocalArray($attribute, $this->ruleToLower($rule)); } @@ -47,7 +49,7 @@ protected function getInlineMessage($attribute, $rule) * * @return string|null */ - protected function getFromLocalArray($attribute, $lowerRule) + protected function getFromLocalArray(string $attribute, string $lowerRule): ?string { $source = $this->customMessages; @@ -60,14 +62,16 @@ protected function getFromLocalArray($attribute, $lowerRule) } } } + + return null; } /** - * @param $rule + * @param string $rule * * @return string|string[]|null */ - protected function ruleToLower($rule) + protected function ruleToLower(string $rule): ?string { $lowerRule = preg_replace('/[A-Z]/', '_$0', $rule); @@ -86,11 +90,11 @@ protected function ruleToLower($rule) * @param mixed $value * @param ValidationRuleInterface $rule * @param array $parameters - * @param $lineNumber + * @param int $lineNumber * * @return string */ - protected function makeReplacements($message, $attribute, $value, $rule, $parameters, $lineNumber) + protected function makeReplacements(string $message, string $attribute, $value, ValidationRuleInterface $rule, array $parameters, int $lineNumber): string { $message = $this->replaceAttributePlaceholder($message, $attribute); @@ -111,7 +115,7 @@ protected function makeReplacements($message, $attribute, $value, $rule, $parame * * @return string */ - protected function replaceAttributePlaceholder($message, $attribute) + protected function replaceAttributePlaceholder(string $message, string $attribute): string { return str_replace([':attribute'], [$attribute], $message); } @@ -124,7 +128,7 @@ protected function replaceAttributePlaceholder($message, $attribute) * * @return string */ - protected function replaceValuePlaceholder($message, $value) + protected function replaceValuePlaceholder(string $message, string $value): string { return str_replace([':value'], [$value], $message); } @@ -132,13 +136,13 @@ protected function replaceValuePlaceholder($message, $value) /** * Replace the :line placeholder in the given message. * - * @param $message - * @param $lineNUmber + * @param string $message + * @param int $lineNumber * * @return mixed */ - protected function replaceErrorLinePlaceholder($message, $lineNUmber) + protected function replaceErrorLinePlaceholder(string $message, int $lineNumber) { - return str_replace([':line'], [$lineNUmber], $message); + return str_replace([':line'], [$lineNumber], $message); } } diff --git a/src/Rules/AsciiOnly.php b/src/Rules/AsciiOnly.php index 045706f..cea5171 100644 --- a/src/Rules/AsciiOnly.php +++ b/src/Rules/AsciiOnly.php @@ -1,5 +1,7 @@ callback = $callback; } @@ -42,7 +44,7 @@ public function __construct($callback) * * @return int */ - public function parameterCount() + public function parameterCount(): int { return 0; } @@ -51,11 +53,11 @@ public function parameterCount() * Determine if the validation rule passes. * * @param mixed $value - * @param $parameters + * @param array $parameters * * @return bool */ - public function passes($value, $parameters) + public function passes($value, array $parameters): bool { $this->failed = false; @@ -73,7 +75,7 @@ public function passes($value, $parameters) * * @return string */ - public function message() + public function message(): string { return $this->message; } @@ -86,7 +88,7 @@ public function message() * * @return string */ - public function parameterReplacer($message, $parameters) + public function parameterReplacer(string $message, array $parameters): string { return $message; } diff --git a/src/Rules/Url.php b/src/Rules/Url.php index 3fdf0a1..d07e45d 100644 --- a/src/Rules/Url.php +++ b/src/Rules/Url.php @@ -1,5 +1,7 @@ filePath = $filePath; $this->delimiter = $delimiter; @@ -130,8 +131,10 @@ public function __construct($filePath, $delimiter, array $rules, array $messages /** * Run the validator's rules against the supplied data. + * + * @return array */ - public function validate() + public function validate(): array { if ($this->fails()) { return $this->errors(); @@ -145,8 +148,10 @@ public function validate() /** * Return validation errors. + * + * @return array */ - public function errors() + public function errors(): array { if (empty($this->message) && empty($this->invalidRows)) { $message = self::NO_ERROR_MESSAGE; @@ -167,7 +172,7 @@ public function errors() * * @return bool */ - public function fails() + public function fails(): bool { return !$this->passes(); } @@ -177,7 +182,7 @@ public function fails() * * @return bool */ - protected function passes() + protected function passes(): bool { if ($this->doesFileExistAndReadable($this->filePath)) { if (false !== ($handle = fopen($this->filePath, 'r'))) { @@ -212,7 +217,7 @@ protected function passes() * * @return bool */ - public function write(Converter $format) + public function write(ConverterHandlerInterface $format): bool { return $format ->convert($this->data) @@ -222,7 +227,7 @@ public function write(Converter $format) /** * Set CSV filename. */ - protected function setFileName() + protected function setFileName(): void { $this->fileName = basename($this->filePath, self::FILE_EXTENSION); } @@ -230,7 +235,7 @@ protected function setFileName() /** * Set CSV file directory. */ - protected function setFileDirectory() + protected function setFileDirectory(): void { $this->directory = dirname($this->filePath) . DIRECTORY_SEPARATOR; } @@ -238,11 +243,11 @@ protected function setFileDirectory() /** * Get the full path and name of the file to be written. * - * @param $extension + * @param string $extension * * @return string */ - protected function getWriteFileName($extension) + protected function getWriteFileName(string $extension): string { return $this->directory . $this->fileName . '.' . $extension; } @@ -250,9 +255,9 @@ protected function getWriteFileName($extension) /** * Validate a given row with the supplied rules. * - * @param $row + * @param array $row */ - protected function validateRow($row) + protected function validateRow(array $row): void { $this->currentRowMessages = []; $this->currentRow = $row; @@ -274,16 +279,14 @@ protected function validateRow($row) /** * Validate a given attribute against a rule. * - * @param string $attribute - * @param string $rule - * - * @return void|null + * @param string $attribute + * @param string|object $rule */ - protected function validateAttribute($attribute, $rule) + protected function validateAttribute(string $attribute, $rule): void { list($rule, $parameters) = ValidationRuleParser::parse($rule); - if ('' == $rule) { + if ('' === $rule) { return; } @@ -306,15 +309,17 @@ protected function validateAttribute($attribute, $rule) $parameters ); } + + return; } } /** - * @param $filePath + * @param string $filePath * * @return bool */ - protected function doesFileExistAndReadable($filePath) + protected function doesFileExistAndReadable(string $filePath): bool { return file_exists($filePath) && is_readable($filePath); } @@ -322,7 +327,7 @@ protected function doesFileExistAndReadable($filePath) /** * @param array $headers */ - protected function setHeaders($headers) + protected function setHeaders(array $headers): void { $this->headers = $headers; } @@ -331,24 +336,23 @@ protected function setHeaders($headers) * Determine if the attribute is validate-able. * * @param object|string $rule - * @param string $parameters + * @param array $parameters * * @return bool */ - protected function isValidateAble($rule, $parameters) + protected function isValidateAble($rule, array $parameters): bool { - return $this->ruleExists($rule) && - $this->passesParameterCheck($rule, $parameters); + return $this->ruleExists($rule) && $this->passesParameterCheck($rule, $parameters); } /** * Get the class of a rule. * - * @param $rule + * @param string $rule * * @return string */ - protected function getRuleClassName($rule) + protected function getRuleClassName(string $rule): string { return 'Oshomo\\CsvUtils\\Rules\\' . $rule; } @@ -356,11 +360,11 @@ protected function getRuleClassName($rule) /** * Get the class of a rule. * - * @param $rule + * @param string $rule * * @return ValidationRuleInterface */ - protected function getRuleClass($rule) + protected function getRuleClass(string $rule): ValidationRuleInterface { $ruleClassName = $this->getRuleClassName($rule); @@ -374,21 +378,20 @@ protected function getRuleClass($rule) * * @return bool */ - protected function ruleExists($rule) + protected function ruleExists($rule): bool { - return $rule instanceof ValidationRule || - class_exists($this->getRuleClassName($rule)); + return $rule instanceof ValidationRule || class_exists($this->getRuleClassName($rule)); } /** * Determine if a given rule expect parameters and that the parameters where sent. * * @param object|string $rule - * @param $parameters + * @param array $parameters * * @return bool */ - protected function passesParameterCheck($rule, $parameters) + protected function passesParameterCheck($rule, array $parameters): bool { if (!$rule instanceof ValidationRule) { $rule = $this->getRuleClass($rule); @@ -403,12 +406,12 @@ protected function passesParameterCheck($rule, $parameters) /** * Validate an attribute using a custom rule object. * - * @param string $attribute - * @param mixed $value - * @param $parameters + * @param string $attribute + * @param mixed $value + * @param array $parameters * @param ValidationRuleInterface $rule */ - protected function validateUsingCustomRule($attribute, $value, $parameters, $rule) + protected function validateUsingCustomRule(string $attribute, $value, array $parameters, ValidationRuleInterface $rule): void { if (!$rule->passes($value, $parameters)) { $this->addFailure($rule->message(), $attribute, $value, $rule, $parameters); @@ -418,13 +421,13 @@ protected function validateUsingCustomRule($attribute, $value, $parameters, $rul /** * Add a failed rule and error message to the collection. * - * @param $message + * @param string $message * @param string $attribute * @param mixed $value * @param ValidationRuleInterface $rule * @param array $parameters */ - protected function addFailure($message, $attribute, $value, $rule, $parameters = []) + protected function addFailure(string $message, string $attribute, $value, ValidationRuleInterface $rule, array $parameters = []): void { $this->currentRowMessages[] = $this->makeReplacements( $message, @@ -443,7 +446,7 @@ protected function addFailure($message, $attribute, $value, $rule, $parameters = * * @return mixed */ - protected function getValue($attribute) + protected function getValue(string $attribute) { return $this->currentRow[$attribute]; } diff --git a/tests/data/valid_test_expected.xml b/tests/data/valid_test_expected.xml index ca98844..dde0701 100644 --- a/tests/data/valid_test_expected.xml +++ b/tests/data/valid_test_expected.xml @@ -1,5 +1,5 @@ - + Well Health Hotels
Inga N. P.O. Box 567
diff --git a/tests/data/valid_test_param_expected.xml b/tests/data/valid_test_param_expected.xml index a8d64e4..86a7d0e 100644 --- a/tests/data/valid_test_param_expected.xml +++ b/tests/data/valid_test_param_expected.xml @@ -1,5 +1,5 @@ - + Well Health Hotels
Inga N. P.O. Box 567
diff --git a/tests/src/Rules/ClosureValidationTest.php b/tests/src/Rules/ClosureValidationTest.php new file mode 100644 index 0000000..e399133 --- /dev/null +++ b/tests/src/Rules/ClosureValidationTest.php @@ -0,0 +1,20 @@ +assertEquals(0, $closureValidationRule->parameterCount()); + } +} diff --git a/tests/src/UppercaseRule.php b/tests/src/UppercaseRule.php index 1ba1fd7..461c5b4 100644 --- a/tests/src/UppercaseRule.php +++ b/tests/src/UppercaseRule.php @@ -9,7 +9,7 @@ class UppercaseRule implements ValidationRuleInterface /** * @return int */ - public function parameterCount() + public function parameterCount(): int { return 0; } @@ -20,7 +20,7 @@ public function parameterCount() * * @return bool */ - public function passes($value, $parameters) + public function passes($value, array $parameters): bool { return strtoupper($value) === $value; } @@ -28,7 +28,7 @@ public function passes($value, $parameters) /** * @return string */ - public function message() + public function message(): string { return 'The :attribute value :value must be uppercase on line :line.'; } @@ -39,7 +39,7 @@ public function message() * * @return string */ - public function parameterReplacer($message, $parameters) + public function parameterReplacer(string $message, array $parameters): string { return $message; }