Skip to content

Commit

Permalink
Updating to PHP7 (#44)
Browse files Browse the repository at this point in the history
On this change, I'm doing some improvements to it works with new features of PHP (properties types, return types)
  • Loading branch information
henriqueholanda authored and tonicospinelli committed Oct 4, 2018
1 parent b549a65 commit 41dff40
Show file tree
Hide file tree
Showing 101 changed files with 559 additions and 535 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Install the library using [composer][1]. Add the following to your `composer.jso
```json
{
"require": {
"brazanation/documents": "1.0.*"
"brazanation/documents": "2.0.*"
}
}
```
Expand All @@ -31,7 +31,7 @@ $ composer.phar install
or

```sh
$ composer require brazanation/documents 1.0.*
$ composer require brazanation/documents 2.0.*
```

### CPF (cadastro de pessoas físicas)
Expand Down
39 changes: 24 additions & 15 deletions src/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,26 @@ abstract class AbstractDocument implements DigitCalculable, Formattable
* @param int $numberOfDigits Max length of checker digits.
* @param string $type Document name/type.
*/
public function __construct($number, $length, $numberOfDigits, $type)
{
$this->type = (string) $type;
$this->numberOfDigits = (int) $numberOfDigits;
$this->length = (int) $length;
public function __construct(
string $number,
int $length,
int $numberOfDigits,
string $type
) {
$this->type = $type;
$this->numberOfDigits = $numberOfDigits;
$this->length = $length;
$this->digit = $this->extractCheckerDigit($number);
$this->assert($number);
$this->number = $number;
}

public function __get($name)
public function __get(string $name)
{
return $this->$name;
}

public function __set($name, $value)
public function __set(string $name, string $value)
{
throw Exception\Readonly::notAllowed(static::class, $name);
}
Expand All @@ -75,7 +79,7 @@ public function __set($name, $value)
*
* @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure.
*/
abstract public static function createFromString($number);
abstract public static function createFromString(string $number);

/**
* Try to create a Document object from given number.
Expand All @@ -87,8 +91,13 @@ abstract public static function createFromString($number);
*
* @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure.
*/
protected static function tryCreateFromString($class, $number, $length, $numberOfDigits, $type)
{
protected static function tryCreateFromString(
string $class,
string $number,
int $length,
int $numberOfDigits,
string $type
) {
try {
return new $class($number, $length, $numberOfDigits, $type);
} catch (Exception\InvalidDocument $exception) {
Expand All @@ -101,7 +110,7 @@ protected static function tryCreateFromString($class, $number, $length, $numberO
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return "{$this->number}";
}
Expand All @@ -114,7 +123,7 @@ public function __toString()
* @throws Exception\InvalidDocument when number is empty
* @throws Exception\InvalidDocument when number is not valid
*/
protected function assert($number)
protected function assert(string $number)
{
if (empty($number)) {
throw Exception\InvalidDocument::notEmpty($this->type);
Expand All @@ -131,7 +140,7 @@ protected function assert($number)
*
* @return bool Returns true if it is a valid number, otherwise false.
*/
protected function isValid($number)
protected function isValid(string $number) : bool
{
$baseNumber = $this->extractBaseNumber($number);

Expand All @@ -157,7 +166,7 @@ protected function isValid($number)
*
* @return string Returns only base number without checker digit.
*/
protected function extractBaseNumber($number)
protected function extractBaseNumber(string $number) : string
{
return substr($number, 0, -($this->numberOfDigits));
}
Expand All @@ -169,7 +178,7 @@ protected function extractBaseNumber($number)
*
* @return string Returns only checker digit.
*/
protected function extractCheckerDigit($number)
protected function extractCheckerDigit(string $number) : string
{
return substr($number, -($this->numberOfDigits));
}
Expand Down
12 changes: 6 additions & 6 deletions src/Cnh.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ final class Cnh extends AbstractDocument
*
* @param string $cnh Only accept numbers
*/
public function __construct($cnh)
public function __construct(string $cnh)
{
$cnh = preg_replace('/\D/', '', $cnh);
parent::__construct($cnh, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

public static function createFromString($number)
public static function createFromString(string $number)
{
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

/**
* {@inheritdoc}
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$firstDigit = $this->calculateFirstDigit($baseNumber);
$secondDigit = $this->calculateSecondDigit($baseNumber);
Expand All @@ -42,7 +42,7 @@ public function calculateDigit($baseNumber)
/**
* {@inheritdoc}
*/
public function format()
public function format() : string
{
return "{$this}";
}
Expand All @@ -54,7 +54,7 @@ public function format()
*
* @return string Returns a calculated checker digit.
*/
private function calculateFirstDigit($baseNumber)
private function calculateFirstDigit(string $baseNumber) : string
{
$calculator = new DigitCalculator($baseNumber);
$calculator->withMultipliersInterval(1, 9);
Expand All @@ -72,7 +72,7 @@ private function calculateFirstDigit($baseNumber)
*
* @return string Returns a calculated checker digit.
*/
private function calculateSecondDigit($baseNumber)
private function calculateSecondDigit(string $baseNumber) : string
{
$calculator = new DigitCalculator($baseNumber);
$calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2, 1]);
Expand Down
8 changes: 4 additions & 4 deletions src/Cnpj.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ final class Cnpj extends AbstractDocument
*
* @param string $cnpj Only accept numbers
*/
public function __construct($cnpj)
public function __construct(string $cnpj)
{
$cnpj = preg_replace('/\D/', '', $cnpj);
parent::__construct($cnpj, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

public static function createFromString($number)
public static function createFromString(string $number)
{
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

/**
* {@inheritdoc}
*/
public function format()
public function format() : string
{
return preg_replace(self::REGEX, '$1.$2.$3/$4-$5', "{$this}");
}

/**
* {@inheritdoc}
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$calculator = new DigitCalculator($baseNumber);
$calculator->useComplementaryInsteadOfModule();
Expand Down
8 changes: 4 additions & 4 deletions src/Cns.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ final class Cns extends AbstractDocument
*
* @param string $number
*/
public function __construct($number)
public function __construct(string $number)
{
$number = preg_replace('/\D/', '', $number);
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

public static function createFromString($number)
public static function createFromString(string $number)
{
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

/**
* {@inheritdoc}
*/
public function format()
public function format() : string
{
return preg_replace(self::REGEX, self::FORMAT, "{$this}");
}
Expand All @@ -49,7 +49,7 @@ public function format()
* For numbers starting with 7, 8 or 9 will use TemporaryCalculator,
* otherwise CnsCalculator.
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$calculator = new CnsCalculator();

Expand Down
2 changes: 1 addition & 1 deletion src/Cns/CnsCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class CnsCalculator implements DigitCalculable
/**
* {@inheritdoc}
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$pis = substr($baseNumber, 0, 11);

Expand Down
2 changes: 1 addition & 1 deletion src/Cns/TemporaryCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TemporaryCalculator implements DigitCalculable
/**
* {@inheritdoc}
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$calculator = new DigitCalculator($baseNumber);
$calculator->withMultipliersInterval(1, 15);
Expand Down
8 changes: 4 additions & 4 deletions src/Cpf.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ final class Cpf extends AbstractDocument
*
* @param string $number Only accept numbers
*/
public function __construct($number)
public function __construct(string $number)
{
$number = preg_replace('/\D/', '', $number);
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

public static function createFromString($number)
public static function createFromString(string $number)
{
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
}

/**
* @return string Returns formatted number, such as: 000.000.000-00
*/
public function format()
public function format() : string
{
return preg_replace(self::REGEX, '$1.$2.$3-$4', "{$this}");
}

/**
* {@inheritdoc}
*/
public function calculateDigit($baseNumber)
public function calculateDigit(string $baseNumber) : string
{
$calculator = new DigitCalculator($baseNumber);
$calculator->withMultipliersInterval(2, 11);
Expand Down
2 changes: 1 addition & 1 deletion src/DigitCalculable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface DigitCalculable
*
* @return string Returns the checker digit.
*/
public function calculateDigit($baseNumber);
public function calculateDigit(string $baseNumber) : string;
}
Loading

0 comments on commit 41dff40

Please sign in to comment.