diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d9374b7..98daf2b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,9 @@ - - + - + ./test @@ -11,4 +12,4 @@ ./src - \ No newline at end of file + diff --git a/src/AbstractCalculator.php b/src/AbstractCalculator.php index 6283ce4..0f4752f 100644 --- a/src/AbstractCalculator.php +++ b/src/AbstractCalculator.php @@ -35,7 +35,7 @@ abstract class AbstractCalculator ); /** - * Array of all avaialable odd characters. + * Array of all available odd characters. */ protected $odd = array( '0' => 1, @@ -77,7 +77,7 @@ abstract class AbstractCalculator ); /** - * Array of all avaialable even characters. + * Array of all available even characters. */ protected $even = array( '0' => 0, @@ -120,7 +120,7 @@ abstract class AbstractCalculator /** - * Array of all avaialable omocodia characters. + * Array of all available omocodia characters. */ protected $omocodiaCodes = array( '0' => 'L', @@ -136,7 +136,7 @@ abstract class AbstractCalculator ); /** - * Array of all avaialable omocodia positions. + * Array of all available omocodia positions. */ protected $omocodiaPositions = array(14, 13, 12, 10, 9, 7, 6); @@ -151,9 +151,9 @@ abstract class AbstractCalculator protected function calculateSumByDictionary($temporaryCodiceFiscale, $dictionaryArray, $i) { $sum = 0; - for (; $i < 15; $i = $i + 2) { - $k = $temporaryCodiceFiscale[$i]; - $sum = $sum + $dictionaryArray[$k]; + for (; $i < 15; $i += 2) { + $k = $temporaryCodiceFiscale[$i] ?? ''; + $sum += $dictionaryArray[$k] ?? 0; } return $sum; diff --git a/test/CalculatorTest.php b/test/CalculatorTest.php index 2def096..74e39f9 100644 --- a/test/CalculatorTest.php +++ b/test/CalculatorTest.php @@ -14,11 +14,11 @@ class CalculatorTest extends \PHPUnit\Framework\TestCase * * @dataProvider calculateProvider */ - public function testCalculate($subject, $omocodiaLevel, $expectedCodiceFiscale) + public function testCalculate(Subject $subject, int $omocodiaLevel, string $expectedCodiceFiscale): void { - $calculator = new Calculator($subject, array( + $calculator = new Calculator($subject, [ 'omocodiaLevel' => $omocodiaLevel, - )); + ]); $actual = $calculator->calculate(); $this->assertEquals($expectedCodiceFiscale, $actual); } @@ -28,7 +28,7 @@ public function testCalculate($subject, $omocodiaLevel, $expectedCodiceFiscale) * * @dataProvider calculateAllPossibilitiesProvider */ - public function testCalculateAllPossibilities($subject, $expected) + public function testCalculateAllPossibilities(Subject $subject, array $expected): void { $calculator = new Calculator($subject); $actual = $calculator->calculateAllPossibilities(); @@ -38,302 +38,315 @@ public function testCalculateAllPossibilities($subject, $expected) /** * The calculate data provider. */ - public function calculateProvider() + public static function calculateProvider(): array { - return array( - array( + return [ + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'RSSMRA85T10A562S', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Roberto', 'surname' => 'Santini', 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ), 0, 'SNTRRT63E08H501T', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Davide', 'surname' => 'Rossi', 'birthDate' => '1989-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'RSSDVD89T10A562S', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Maria', 'surname' => 'Montessori', 'birthDate' => '1870-08-31', 'gender' => 'F', 'belfioreCode' => 'C615', - ) + ] ), 0, 'MNTMRA70M71C615I', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => new \DateTime('1985-12-10'), 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'RSSMRA85T10A562S', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Gianfranco', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'RSSGFR85T10A562I', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Fo', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'FOXMRA85T10A562G', - ), - array( + ], + [ new Subject( - array( + [ 'name' => '', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'RSSXXX85T10A562R', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => '', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'XXXMRA85T10A562B', - ), - array( + ], + [ new Subject( - array( + [ 'name' => '', 'surname' => '', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 0, 'XXXXXX85T10A562A', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Roberto', 'surname' => 'Santi', 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ), 1, 'SNTRRT63E08H50ML', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 1, 'RSSMRA85T10A56NH', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 3, 'RSSMRA85T10A5SNT', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'De Rossi', 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ), -1, 'DRSMRA90A01F839W', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Anna Paola', 'surname' => 'Rossi', 'birthDate' => '1990-01-01', 'gender' => 'F', 'belfioreCode' => 'F839', - ) + ] ), -1, 'RSSNPL90A41F839J', - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => "D'Ossi", 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ), -1, 'DSSMRA90A01F839X', - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Lu'ay", 'surname' => "D'ari", 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ), -1, 'DRALYU90A01F839U', - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Annalisa", 'surname' => "Lisà", 'birthDate' => '1980-04-04', 'gender' => 'F', 'belfioreCode' => 'H501', - ) + ] ), -1, 'LSINLS80D44H501F', - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Marco—Antonio", 'surname' => "D’Andrea", 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ), -1, 'DNDMCN90A01F839X', - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Anna", 'surname' => "Betaña", 'birthDate' => '1972-04-02', 'gender' => 'F', 'belfioreCode' => 'H501', - ) + ] ), -1, 'BTENNA72D42H501M', - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Žáç", 'surname' => "Öß", 'birthDate' => '1954-07-01', 'gender' => 'M', 'belfioreCode' => 'A794', - ) + ] ), -1, 'SSOZCA54L01A794X', - ), - ); + ], + [ + new Subject( + [ + 'name' => "Massimiliano", + 'surname' => "Arione", + 'birthDate' => '1974-04-01', + 'gender' => 'M', + 'belfioreCode' => 'EE', + ] + ), + -1, + 'RNAMSM74D01EEV', + ], + ]; } /** * The calculate all possibilities data provider. */ - public function calculateAllPossibilitiesProvider() + public static function calculateAllPossibilitiesProvider(): array { - return array( - array( + return [ + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - array( + [ 'RSSMRA85T10A562S', 'RSSMRA85T10A56NH', 'RSSMRA85T10A5S2E', @@ -462,19 +475,19 @@ public function calculateAllPossibilitiesProvider() 'RSSMRAURTMLAR6NZ', 'RSSMRAURTMLARS2W', 'RSSMRAURTMLARSNL', - ), - ), - array( + ], + ], + [ new Subject( - array( + [ 'name' => 'Roberto', 'surname' => 'Santi', 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ), - array( + [ 'SNTRRT63E08H501T', 'SNTRRT63E08H50ML', 'SNTRRT63E08H5L1E', @@ -603,8 +616,8 @@ public function calculateAllPossibilitiesProvider() 'SNTRRTSPELUHR0MX', 'SNTRRTSPELUHRL1Q', 'SNTRRTSPELUHRLMI', - ), - ), - ); + ], + ], + ]; } } diff --git a/test/CheckerTest.php b/test/CheckerTest.php index 061be31..6882e4b 100644 --- a/test/CheckerTest.php +++ b/test/CheckerTest.php @@ -16,10 +16,10 @@ class CheckerTest extends \PHPUnit\Framework\TestCase */ public function testAllLevels($subject, $codiceFiscaleToCheck, $omocodiaLevel, $expected) { - $checker = new Checker($subject, array( + $checker = new Checker($subject, [ 'codiceFiscaleToCheck' => $codiceFiscaleToCheck, 'omocodiaLevel' => $omocodiaLevel, - )); + ]); $actual = $checker->check(); $this->assertEquals($expected, $actual); } @@ -29,77 +29,77 @@ public function testAllLevels($subject, $codiceFiscaleToCheck, $omocodiaLevel, $ */ public function checkerProvider() { - return array( - array( + return [ + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 'RSSMRA85T10A562S', 0, true, - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 'RSSMRA85T10A562S', 2, false, - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Roberto', 'surname' => 'Santi', 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ), 'SNTRRT63E08H50ML', Checker::ALL_OMOCODIA_LEVELS, true, - ), - array( + ], + [ new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), 'RSSMRAURTMLAR6NZ', Checker::ALL_OMOCODIA_LEVELS, true, - ), - array( + ], + [ new Subject( - array( + [ 'name' => "Annalisa", 'surname' => "Lisà", 'birthDate' => '1980-04-04', 'gender' => 'F', 'belfioreCode' => 'H501', - ) + ] ), 'LSINLS80D44H501F', -1, true, - ), - ); + ], + ]; } } diff --git a/test/InverseCalculatorTest.php b/test/InverseCalculatorTest.php index 7904101..c4fa880 100644 --- a/test/InverseCalculatorTest.php +++ b/test/InverseCalculatorTest.php @@ -15,12 +15,12 @@ class InverseCalculatorTest extends \PHPUnit\Framework\TestCase * * @dataProvider calculateProvider */ - public function testInverseCalculate($codiceFiscale, $omocodiaAllowed, $century, $expectedSubject) + public function testInverseCalculate(string $codiceFiscale, bool $omocodiaAllowed, ?int $century, Subject $expectedSubject): void { - $inverseCalculator = new InverseCalculator($codiceFiscale, array( + $inverseCalculator = new InverseCalculator($codiceFiscale, [ 'omocodiaAllowed' => $omocodiaAllowed, 'century' => $century - )); + ]); $actual = $inverseCalculator->getSubject(); $this->assertEquals($expectedSubject->getBelfioreCode(), $actual->getBelfioreCode()); $this->assertEquals($expectedSubject->getBirthDate(), $actual->getBirthDate()); @@ -29,234 +29,239 @@ public function testInverseCalculate($codiceFiscale, $omocodiaAllowed, $century, $this->assertEquals(null, $actual->getSurname()); } + public function todotestInverseCalculatorDoesNotThrowWarnings(): void + { + $inverseCalculator = new InverseCalculator('RNAMSM74D01H501C'); + $this->assertInstanceOf(Subject::class, $inverseCalculator->getSubject()); + } /** * The calculate data provider. */ - public function calculateProvider() + public static function calculateProvider(): array { $subject = new Subject( - array( + [ 'name' => 'Mario', 'surname' => 'Rossi', 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ); return array_merge( - array( - array( + [ + [ 'RSSMRA85T10A562S', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - ), - array( + ], + [ 'SNTRRT63E08H501T', true, null, new Subject( - array( + [ 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ) - ), - array( + ], + [ 'RSSDVD89T10A562S', true, null, new Subject( - array( + [ 'birthDate' => '1989-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ) - ), - array( + ], + [ 'MNTMRA70M71C615I', true, 18, new Subject( - array( + [ 'birthDate' => '1870-08-31', 'gender' => 'F', 'belfioreCode' => 'C615', - ) + ] ) - ), - array( + ], + [ 'RSSMRA85T10A562S', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - ), - array( + ], + [ 'RSSGFR85T10A562I', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - ), - array( + ], + [ 'FOXMRA85T10A562G', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - ), - array( + ], + [ 'RSSXXX85T10A562R', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ) - ), - array( + ], + [ 'SNTRRT63E08H50ML', true, null, new Subject( - array( + [ 'birthDate' => '1963-05-08', 'gender' => 'M', 'belfioreCode' => 'H501', - ) + ] ) - ), - array( + ], + [ 'RSSMRA85T10A56NH', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ) - ), - array( + ], + [ 'RSSMRA85T10ARSNO', true, null, new Subject( - array( + [ 'birthDate' => '1985-12-10', 'gender' => 'M', 'belfioreCode' => 'A562', - ) + ] ), - ), - array( + ], + [ 'DRSMRA90A01F839W', true, null, new Subject( - array( + [ 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ) - ), - array( + ], + [ 'RSSNPL90A41F839J', true, null, new Subject( - array( + [ 'birthDate' => '1990-01-01', 'gender' => 'F', 'belfioreCode' => 'F839', - ) + ] ) - ), - array( + ], + [ 'DSSMRA90A01F839X', true, null, new Subject( - array( + [ 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ), - ), - array( + ], + [ 'DRALYU90A01F839U', true, null, new Subject( - array( + [ 'birthDate' => '1990-01-01', 'gender' => 'M', 'belfioreCode' => 'F839', - ) + ] ) - ), - ), + ], + ], // Handle all the different omocodia levels - $this->generateAllOmocodiaLevels($subject) + self::generateAllOmocodiaLevels($subject) ); } /** * Generate all the codici fiscali for the given $subject. - * @param $subject The subject of which generate all the codici fiscali. - * @returns Returns all the codici fiscali for the given $subject. + * @param Subject $subject The subject of which generate all the codici fiscali. + * @return array Returns all the codici fiscali for the given $subject. */ - private function generateAllOmocodiaLevels($subject) + private static function generateAllOmocodiaLevels(Subject $subject): array { - $allOmocodiaLevels = array(); + $allOmocodiaLevels = []; for ($omocodiaLevel = 0; $omocodiaLevel < 128; $omocodiaLevel++) { - $calculator = new Calculator($subject, array( + $calculator = new Calculator($subject, [ 'omocodiaLevel' => $omocodiaLevel, - )); - $allOmocodiaLevels[] = array( + ]); + $allOmocodiaLevels[] = [ $calculator->calculate(), true, null, new Subject( - array( + [ 'birthDate' => $subject->getBirthDate(), 'gender' => $subject->getGender(), 'belfioreCode' => $subject->getBelfioreCode(), - ) + ] ) - ); + ]; } return $allOmocodiaLevels; diff --git a/test/ValidatorTest.php b/test/ValidatorTest.php index b5353cc..9b035cf 100644 --- a/test/ValidatorTest.php +++ b/test/ValidatorTest.php @@ -15,16 +15,16 @@ class ValidatorTest extends \PHPUnit\Framework\TestCase * @dataProvider validatorProvider */ public function testValidationTrue( - $codiceFiscaleToValidate, - $omocodiaAllowed, - $secular, - $isFormallyValid, - $isOmocodia = false - ) { + string $codiceFiscaleToValidate, + bool $omocodiaAllowed, + bool $secular, + bool $isFormallyValid, + bool $isOmocodia = false + ): void { $validator = new Validator( $codiceFiscaleToValidate, - array('omocodiaAllowed' => $omocodiaAllowed, - 'secular' => $secular) + ['omocodiaAllowed' => $omocodiaAllowed, + 'secular' => $secular] ); $actual = $validator->isFormallyValid(); @@ -40,87 +40,76 @@ public function testValidationTrue( /** * The validator provider. */ - public function validatorProvider() + public static function validatorProvider(): array { - return array( - // valid - array( + return [ + 'valid' => [ 'RSSMRA85T10A562S', true, false, true, - ), - // valid altough omocodia not allowed - array( + ], + 'valid although omocodia not allowed' => [ 'RSSMRA85T10A562S', false, false, true, - ), - // check digit - array( + ], + 'check digit' => [ 'RSSMRA85T10A562T', true, false, false, - ), - // valid with omocodia - array( + ], + 'valid with omocodia' => [ 'SNTRRT63E08H50ML', true, false, true, true - ), - // empty - array( + ], + 'empty' => [ '', true, false, false, - ), - // length - array( + ], + 'length' => [ 'RSSMRA85T10A562', true, false, false, - ), - // regexp - array( + ], + 'regexp' => [ 'RS3MRA85T10A562S', true, false, false, - ), - // birth day - array( + ], + 'birthday' => [ 'RSSMRA85T99A562U', true, false, false, - ), - // omocodia character - array( + ], + 'omocodia character' => [ 'RSSMRA85T10A56AO', true, false, false, - ), - // month - array( + ], + 'month' => [ 'RSSMRA85Z10A562B', true, false, false, - ), - // date - array( + ], + 'date' => [ 'RSSMRA85B30A562G', true, false, false, - ), - ); + ], + ]; } }