Skip to content

Commit

Permalink
Refactored methods with php7 operators and scalar types (#12)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
roberto910907 authored and hoshomoh committed Mar 15, 2019
1 parent c7d1fd3 commit 6f3d85d
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 113 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
language: php
php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions src/Contracts/ConverterHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Contracts;

interface ConverterHandlerInterface
Expand All @@ -10,17 +12,17 @@ interface ConverterHandlerInterface
*
* @return string
*/
public function getExtension();
public function getExtension(): string;

/**
* Does the actual conversion. This is where the actual conversion
* is carried out.
*
* @param array $data
*
* @return $this
* @return self
*/
public function convert($data);
public function convert(array $data): self;

/**
* Writes the converted data to the path specified.
Expand All @@ -29,5 +31,5 @@ public function convert($data);
*
* @return bool
*/
public function write($filename);
public function write(string $filename): bool;
}
10 changes: 6 additions & 4 deletions src/Contracts/ValidationRuleInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Contracts;

interface ValidationRuleInterface
Expand All @@ -11,7 +13,7 @@ interface ValidationRuleInterface
*
* @return int
*/
public function parameterCount();
public function parameterCount(): int;

/**
* Determines if the validation rule passes. This is where we do the
Expand All @@ -22,7 +24,7 @@ public function parameterCount();
*
* @return bool
*/
public function passes($value, $parameters);
public function passes($value, array $parameters): bool;

/**
* Get the validation error message. Specify the message that should
Expand All @@ -31,7 +33,7 @@ public function passes($value, $parameters);
*
* @return string
*/
public function message();
public function message(): string;

/**
* Replace error messages parameter with right values. If you want
Expand All @@ -49,5 +51,5 @@ public function message();
*
* @return string
*/
public function parameterReplacer($message, $parameters);
public function parameterReplacer(string $message, array $parameters): string;
}
10 changes: 6 additions & 4 deletions src/Converter/JsonConverter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Converter;

use Oshomo\CsvUtils\Contracts\ConverterHandlerInterface;
Expand All @@ -18,17 +20,17 @@ class JsonConverter implements ConverterHandlerInterface
/**
* @return string
*/
public function getExtension()
public function getExtension(): string
{
return self::FILE_EXTENSION;
}

/**
* @param array $data
*
* @return $this|mixed
* @return ConverterHandlerInterface
*/
public function convert($data)
public function convert(array $data): ConverterHandlerInterface
{
$this->data = json_encode(
$data,
Expand All @@ -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;
}
Expand Down
24 changes: 13 additions & 11 deletions src/Converter/XmlConverter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Converter;

use DOMDocument;
Expand Down Expand Up @@ -29,28 +31,28 @@ class XmlConverter implements ConverterHandlerInterface
*
* @param string $recordElement
*/
public function __construct($recordElement = self::DEFAULT_RECORD_ELEMENT)
public function __construct(string $recordElement = self::DEFAULT_RECORD_ELEMENT)
{
if (!empty($recordElement)) {
$this->recordElement = $recordElement;
}

$this->data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$this->data = new SimpleXMLElement('<?xml version="1.0"?><data value=""></data>');
}

/**
* @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)) {
Expand All @@ -66,23 +68,23 @@ 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);

return $this;
}

/**
* @param $filename
* @param string $filename
*
* @return bool
*/
public function write($filename)
public function write(string $filename): bool
{
$dom = new DOMDocument('1.0');

Expand Down
30 changes: 17 additions & 13 deletions src/Helpers/FormatsMessages.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Helpers;

use Oshomo\CsvUtils\Contracts\ValidationRuleInterface;
Expand All @@ -15,7 +17,7 @@ trait FormatsMessages
*
* @return string
*/
protected function getMessage($attribute, $rule, $actualRule)
protected function getMessage(string $attribute, ValidationRuleInterface $rule, string $actualRule): string
{
$inlineMessage = $this->getInlineMessage($attribute, $actualRule);

Expand All @@ -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));
}
Expand All @@ -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;

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -124,21 +128,21 @@ 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);
}

/**
* 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);
}
}
12 changes: 7 additions & 5 deletions src/Rules/AsciiOnly.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Oshomo\CsvUtils\Rules;

use Oshomo\CsvUtils\Contracts\ValidationRuleInterface;
Expand All @@ -11,7 +13,7 @@ class AsciiOnly implements ValidationRuleInterface
*
* @return int
*/
public function parameterCount()
public function parameterCount(): int
{
return 0;
}
Expand All @@ -20,11 +22,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
{
return (mb_detect_encoding($value, 'ASCII', true)) ? true : false;
}
Expand All @@ -34,7 +36,7 @@ public function passes($value, $parameters)
*
* @return string
*/
public function message()
public function message(): string
{
return 'The :attribute value :value contains a non-ascii character on line :line.';
}
Expand All @@ -47,7 +49,7 @@ public function message()
*
* @return string
*/
public function parameterReplacer($message, $parameters)
public function parameterReplacer(string $message, array $parameters): string
{
return $message;
}
Expand Down
Loading

0 comments on commit 6f3d85d

Please sign in to comment.