Skip to content

Commit 41dff40

Browse files
henriqueholandatonicospinelli
authored andcommitted
Updating to PHP7 (#44)
On this change, I'm doing some improvements to it works with new features of PHP (properties types, return types)
1 parent b549a65 commit 41dff40

File tree

101 files changed

+559
-535
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+559
-535
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Install the library using [composer][1]. Add the following to your `composer.jso
1717
```json
1818
{
1919
"require": {
20-
"brazanation/documents": "1.0.*"
20+
"brazanation/documents": "2.0.*"
2121
}
2222
}
2323
```
@@ -31,7 +31,7 @@ $ composer.phar install
3131
or
3232

3333
```sh
34-
$ composer require brazanation/documents 1.0.*
34+
$ composer require brazanation/documents 2.0.*
3535
```
3636

3737
### CPF (cadastro de pessoas físicas)

src/AbstractDocument.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ abstract class AbstractDocument implements DigitCalculable, Formattable
4848
* @param int $numberOfDigits Max length of checker digits.
4949
* @param string $type Document name/type.
5050
*/
51-
public function __construct($number, $length, $numberOfDigits, $type)
52-
{
53-
$this->type = (string) $type;
54-
$this->numberOfDigits = (int) $numberOfDigits;
55-
$this->length = (int) $length;
51+
public function __construct(
52+
string $number,
53+
int $length,
54+
int $numberOfDigits,
55+
string $type
56+
) {
57+
$this->type = $type;
58+
$this->numberOfDigits = $numberOfDigits;
59+
$this->length = $length;
5660
$this->digit = $this->extractCheckerDigit($number);
5761
$this->assert($number);
5862
$this->number = $number;
5963
}
6064

61-
public function __get($name)
65+
public function __get(string $name)
6266
{
6367
return $this->$name;
6468
}
6569

66-
public function __set($name, $value)
70+
public function __set(string $name, string $value)
6771
{
6872
throw Exception\Readonly::notAllowed(static::class, $name);
6973
}
@@ -75,7 +79,7 @@ public function __set($name, $value)
7579
*
7680
* @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure.
7781
*/
78-
abstract public static function createFromString($number);
82+
abstract public static function createFromString(string $number);
7983

8084
/**
8185
* Try to create a Document object from given number.
@@ -87,8 +91,13 @@ abstract public static function createFromString($number);
8791
*
8892
* @return AbstractDocument|boolean Returns a new Document instance or FALSE on failure.
8993
*/
90-
protected static function tryCreateFromString($class, $number, $length, $numberOfDigits, $type)
91-
{
94+
protected static function tryCreateFromString(
95+
string $class,
96+
string $number,
97+
int $length,
98+
int $numberOfDigits,
99+
string $type
100+
) {
92101
try {
93102
return new $class($number, $length, $numberOfDigits, $type);
94103
} catch (Exception\InvalidDocument $exception) {
@@ -101,7 +110,7 @@ protected static function tryCreateFromString($class, $number, $length, $numberO
101110
*
102111
* @return string
103112
*/
104-
public function __toString()
113+
public function __toString() : string
105114
{
106115
return "{$this->number}";
107116
}
@@ -114,7 +123,7 @@ public function __toString()
114123
* @throws Exception\InvalidDocument when number is empty
115124
* @throws Exception\InvalidDocument when number is not valid
116125
*/
117-
protected function assert($number)
126+
protected function assert(string $number)
118127
{
119128
if (empty($number)) {
120129
throw Exception\InvalidDocument::notEmpty($this->type);
@@ -131,7 +140,7 @@ protected function assert($number)
131140
*
132141
* @return bool Returns true if it is a valid number, otherwise false.
133142
*/
134-
protected function isValid($number)
143+
protected function isValid(string $number) : bool
135144
{
136145
$baseNumber = $this->extractBaseNumber($number);
137146

@@ -157,7 +166,7 @@ protected function isValid($number)
157166
*
158167
* @return string Returns only base number without checker digit.
159168
*/
160-
protected function extractBaseNumber($number)
169+
protected function extractBaseNumber(string $number) : string
161170
{
162171
return substr($number, 0, -($this->numberOfDigits));
163172
}
@@ -169,7 +178,7 @@ protected function extractBaseNumber($number)
169178
*
170179
* @return string Returns only checker digit.
171180
*/
172-
protected function extractCheckerDigit($number)
181+
protected function extractCheckerDigit(string $number) : string
173182
{
174183
return substr($number, -($this->numberOfDigits));
175184
}

src/Cnh.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ final class Cnh extends AbstractDocument
1717
*
1818
* @param string $cnh Only accept numbers
1919
*/
20-
public function __construct($cnh)
20+
public function __construct(string $cnh)
2121
{
2222
$cnh = preg_replace('/\D/', '', $cnh);
2323
parent::__construct($cnh, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2424
}
2525

26-
public static function createFromString($number)
26+
public static function createFromString(string $number)
2727
{
2828
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2929
}
3030

3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function calculateDigit($baseNumber)
34+
public function calculateDigit(string $baseNumber) : string
3535
{
3636
$firstDigit = $this->calculateFirstDigit($baseNumber);
3737
$secondDigit = $this->calculateSecondDigit($baseNumber);
@@ -42,7 +42,7 @@ public function calculateDigit($baseNumber)
4242
/**
4343
* {@inheritdoc}
4444
*/
45-
public function format()
45+
public function format() : string
4646
{
4747
return "{$this}";
4848
}
@@ -54,7 +54,7 @@ public function format()
5454
*
5555
* @return string Returns a calculated checker digit.
5656
*/
57-
private function calculateFirstDigit($baseNumber)
57+
private function calculateFirstDigit(string $baseNumber) : string
5858
{
5959
$calculator = new DigitCalculator($baseNumber);
6060
$calculator->withMultipliersInterval(1, 9);
@@ -72,7 +72,7 @@ private function calculateFirstDigit($baseNumber)
7272
*
7373
* @return string Returns a calculated checker digit.
7474
*/
75-
private function calculateSecondDigit($baseNumber)
75+
private function calculateSecondDigit(string $baseNumber) : string
7676
{
7777
$calculator = new DigitCalculator($baseNumber);
7878
$calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2, 1]);

src/Cnpj.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ final class Cnpj extends AbstractDocument
1717
*
1818
* @param string $cnpj Only accept numbers
1919
*/
20-
public function __construct($cnpj)
20+
public function __construct(string $cnpj)
2121
{
2222
$cnpj = preg_replace('/\D/', '', $cnpj);
2323
parent::__construct($cnpj, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2424
}
2525

26-
public static function createFromString($number)
26+
public static function createFromString(string $number)
2727
{
2828
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2929
}
3030

3131
/**
3232
* {@inheritdoc}
3333
*/
34-
public function format()
34+
public function format() : string
3535
{
3636
return preg_replace(self::REGEX, '$1.$2.$3/$4-$5', "{$this}");
3737
}
3838

3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function calculateDigit($baseNumber)
42+
public function calculateDigit(string $baseNumber) : string
4343
{
4444
$calculator = new DigitCalculator($baseNumber);
4545
$calculator->useComplementaryInsteadOfModule();

src/Cns.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ final class Cns extends AbstractDocument
2222
*
2323
* @param string $number
2424
*/
25-
public function __construct($number)
25+
public function __construct(string $number)
2626
{
2727
$number = preg_replace('/\D/', '', $number);
2828
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2929
}
3030

31-
public static function createFromString($number)
31+
public static function createFromString(string $number)
3232
{
3333
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
3434
}
3535

3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function format()
39+
public function format() : string
4040
{
4141
return preg_replace(self::REGEX, self::FORMAT, "{$this}");
4242
}
@@ -49,7 +49,7 @@ public function format()
4949
* For numbers starting with 7, 8 or 9 will use TemporaryCalculator,
5050
* otherwise CnsCalculator.
5151
*/
52-
public function calculateDigit($baseNumber)
52+
public function calculateDigit(string $baseNumber) : string
5353
{
5454
$calculator = new CnsCalculator();
5555

src/Cns/CnsCalculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class CnsCalculator implements DigitCalculable
1010
/**
1111
* {@inheritdoc}
1212
*/
13-
public function calculateDigit($baseNumber)
13+
public function calculateDigit(string $baseNumber) : string
1414
{
1515
$pis = substr($baseNumber, 0, 11);
1616

src/Cns/TemporaryCalculator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TemporaryCalculator implements DigitCalculable
1010
/**
1111
* {@inheritdoc}
1212
*/
13-
public function calculateDigit($baseNumber)
13+
public function calculateDigit(string $baseNumber) : string
1414
{
1515
$calculator = new DigitCalculator($baseNumber);
1616
$calculator->withMultipliersInterval(1, 15);

src/Cpf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ final class Cpf extends AbstractDocument
1717
*
1818
* @param string $number Only accept numbers
1919
*/
20-
public function __construct($number)
20+
public function __construct(string $number)
2121
{
2222
$number = preg_replace('/\D/', '', $number);
2323
parent::__construct($number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2424
}
2525

26-
public static function createFromString($number)
26+
public static function createFromString(string $number)
2727
{
2828
return parent::tryCreateFromString(self::class, $number, self::LENGTH, self::NUMBER_OF_DIGITS, self::LABEL);
2929
}
3030

3131
/**
3232
* @return string Returns formatted number, such as: 000.000.000-00
3333
*/
34-
public function format()
34+
public function format() : string
3535
{
3636
return preg_replace(self::REGEX, '$1.$2.$3-$4', "{$this}");
3737
}
3838

3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function calculateDigit($baseNumber)
42+
public function calculateDigit(string $baseNumber) : string
4343
{
4444
$calculator = new DigitCalculator($baseNumber);
4545
$calculator->withMultipliersInterval(2, 11);

src/DigitCalculable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface DigitCalculable
1111
*
1212
* @return string Returns the checker digit.
1313
*/
14-
public function calculateDigit($baseNumber);
14+
public function calculateDigit(string $baseNumber) : string;
1515
}

0 commit comments

Comments
 (0)