Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced phpstan and applied rules #38

Merged
merged 2 commits into from
May 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -17,4 +17,5 @@ install:

script:
- vendor/bin/php-cs-fixer fix --dry-run
- vendor/bin/phpstan analyze
- vendor/bin/phpunit
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -28,7 +28,8 @@
},
"require-dev" : {
"phpunit/phpunit" : "^9.1",
"friendsofphp/php-cs-fixer": "^2.16.3"
"friendsofphp/php-cs-fixer": "^2.16.3",
"phpstan/phpstan": "^0.12.25"
},
"suggest": {
"ext-bcmath" : "This library makes use of bcmod function, so an installed bcmath extension is recommended."
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
paths:
- src
level:
max
checkMissingIterableValueType: false
14 changes: 5 additions & 9 deletions src/CountryInfo.php
Original file line number Diff line number Diff line change
@@ -33,11 +33,7 @@ class CountryInfo
public function __construct(string $countryCode, Registry $swiftRegistry = null)
{
$this->countryCode = $countryCode;
$this->swiftRegistry = $swiftRegistry;

if (null === $swiftRegistry) {
$this->swiftRegistry = new Registry();
}
$this->swiftRegistry = $swiftRegistry ?? new Registry();

if (!$this->swiftRegistry->isCountryAvailable($this->countryCode)) {
throw new UnsupportedCountryCodeException($this->countryCode);
@@ -69,22 +65,22 @@ public function getBbanRegex(): string
return $this->swiftRegistry->getBbanRegex($this->countryCode);
}

public function getIbanLength(): string
public function getIbanLength(): int
{
return $this->swiftRegistry->getIbanLength($this->countryCode);
}

public function getBbanLength(): string
public function getBbanLength(): int
{
return $this->swiftRegistry->getBbanLength($this->countryCode);
}

public function getBbanBankIdentifierStartPos(): string
public function getBbanBankIdentifierStartPos(): int
{
return $this->swiftRegistry->getBbanBankIdentifierStartPos($this->countryCode);
}

public function getBbanBankIdentifierEndPos(): string
public function getBbanBankIdentifierEndPos(): int
{
return $this->swiftRegistry->getBbanBankIdentifierEndPos($this->countryCode);
}
23 changes: 23 additions & 0 deletions src/Exception/CanNotBeNormalizedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the iban-validation library.
*
* (c) Jan Schädlich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Iban\Validation\Exception;

/**
* @author Jan Schädlich <[email protected]>
*/
class CanNotBeNormalizedException extends \RuntimeException
{
public function __construct(string $iban)
{
parent::__construct(sprintf('Given IBAN "%s" can not be normalized.', $iban));
}
}
5 changes: 4 additions & 1 deletion src/Exception/InvalidChecksumException.php
Original file line number Diff line number Diff line change
@@ -16,13 +16,16 @@
*/
class InvalidChecksumException extends \RuntimeException
{
/**
* @var string
*/
protected $validChecksum;

public function __construct(string $iban, string $validChecksum)
{
$this->validChecksum = $validChecksum;

parent::__construct(sprintf('Checksum of given IBAN "%s" is not valid! Valid checksum is %s', $iban, $validChecksum));
parent::__construct(sprintf('Checksum of given IBAN "%s" is not valid. Valid checksum is "%s".', $iban, $validChecksum));
}

public function getValidChecksum(): string
2 changes: 1 addition & 1 deletion src/Exception/InvalidFormatException.php
Original file line number Diff line number Diff line change
@@ -18,6 +18,6 @@ class InvalidFormatException extends \RuntimeException
{
public function __construct(string $iban)
{
parent::__construct(sprintf('Format of given IBAN "%s" is not valid!', $iban));
parent::__construct(sprintf('Format of given IBAN "%s" is not valid.', $iban));
}
}
2 changes: 1 addition & 1 deletion src/Exception/InvalidLengthException.php
Original file line number Diff line number Diff line change
@@ -18,6 +18,6 @@ class InvalidLengthException extends \RuntimeException
{
public function __construct(string $iban)
{
parent::__construct(sprintf('Length of given IBAN "%s" is not valid!', $iban));
parent::__construct(sprintf('Length of given IBAN "%s" is not valid.', $iban));
}
}
12 changes: 7 additions & 5 deletions src/Iban.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@

namespace Iban\Validation;

use Iban\Validation\Exception\CanNotBeNormalizedException;

/**
* Represents an International Bank Account Number (IBAN).
*
@@ -45,13 +47,13 @@ public function __toString(): string

public function getNormalizedIban(): string
{
$iban = $this->iban;
$normalizedIban = trim(strtoupper($this->iban));

$iban = trim(strtoupper($iban));
$iban = preg_replace('/^I?IBAN/', '', $iban);
$iban = preg_replace('/[^a-zA-Z0-9]/', '', $iban);
if (null === $normalizedIban = preg_replace(['/^I?IBAN/', '/[^a-zA-Z0-9]/', '/\s+/'], '', $normalizedIban)) {
throw new CanNotBeNormalizedException($this->iban);
}

return preg_replace('/\s+/', '', $iban);
return $normalizedIban;
}

public function format(string $type = self::FORMAT_PRINT): string
23 changes: 23 additions & 0 deletions src/Swift/Exception/RegexConversionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the iban-validation library.
*
* (c) Jan Schädlich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Iban\Validation\Swift\Exception;

/**
* @author Jan Schädlich <[email protected]>
*/
class RegexConversionException extends \RuntimeException
{
public function __construct(string $input)
{
parent::__construct(sprintf('Can not convert given input "%s".', $input));
}
}
23 changes: 23 additions & 0 deletions src/Swift/Exception/RegistryLoaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the iban-validation library.
*
* (c) Jan Schädlich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Iban\Validation\Swift\Exception;

/**
* @author Jan Schädlich <[email protected]>
*/
class RegistryLoaderException extends \RuntimeException
{
public function __construct(string $filename)
{
parent::__construct(sprintf('Can not load contents of file "%s".', $filename));
}
}
2 changes: 1 addition & 1 deletion src/Swift/Exception/UnsupportedCountryCodeException.php
Original file line number Diff line number Diff line change
@@ -18,6 +18,6 @@ class UnsupportedCountryCodeException extends \RuntimeException
{
public function __construct(string $countryCode)
{
parent::__construct(sprintf('Country with countryCode "%s" is not supported', $countryCode));
parent::__construct(sprintf('Country with countryCode "%s" is not supported.', $countryCode));
}
}
25 changes: 20 additions & 5 deletions src/Swift/RegexConverter.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@

namespace Iban\Validation\Swift;

use Iban\Validation\Swift\Exception\RegexConversionException;

/**
* Converts iban and bban structure notation used in iban_registry text file provided by SWIFT to common regex.
*
@@ -22,11 +24,24 @@ class RegexConverter
{
public function convert(string $input): string
{
$input = preg_replace('/(^[A-Z]{2})/', '${1}', $input);
$input = preg_replace('/(\d+)\!(n)/', '\d{${1}}', $input);
$input = preg_replace('/(\d+)\!(c)/', '[A-Z0-9]{${1}}', $input);
$input = preg_replace('/(\d+)\!(a)/', '[A-Z]{${1}}', $input);
$searchPatterns = [
'/(^[A-Z]{2})/',
'/(\d+)\!(n)/',
'/(\d+)\!(c)/',
'/(\d+)\!(a)/',
];

$replacements = [
'${1}',
'\d{${1}}',
'[A-Z0-9]{${1}}',
'[A-Z]{${1}}',
];

if (null === $convertedInput = preg_replace($searchPatterns, $replacements, $input)) {
throw new RegexConversionException($input);
}

return $input;
return $convertedInput;
}
}
7 changes: 6 additions & 1 deletion src/Swift/RegistryLoader.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

namespace Iban\Validation\Swift;

use Iban\Validation\Swift\Exception\RegistryLoaderException;
use Symfony\Component\Yaml\Yaml;

/**
@@ -32,6 +33,10 @@ public function __construct(string $filename)

public function load(): array
{
return Yaml::parse(file_get_contents($this->filename));
if (false === $content = file_get_contents($this->filename)) {
throw new RegistryLoaderException($this->filename);
}

return Yaml::parse($content);
}
}
20 changes: 8 additions & 12 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -74,11 +74,7 @@ class Validator

public function __construct(array $options = [], Registry $swiftRegistry = null)
{
$this->swiftRegistry = $swiftRegistry;

if (null === $swiftRegistry) {
$this->swiftRegistry = new Registry();
}
$this->swiftRegistry = $swiftRegistry ?? new Registry();

$resolver = new OptionsResolver();
$this->configureOptions($resolver);
@@ -130,7 +126,7 @@ public function getViolations(): array
return $this->violations;
}

protected function configureOptions(OptionsResolver $resolver)
protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'violation.unsupported_country' => 'The requested country is not supported!',
@@ -153,7 +149,7 @@ protected function validateCountryCode(Iban $iban): void
/**
* @throws InvalidLengthException
*/
protected function validateLength(Iban $iban)
protected function validateLength(Iban $iban): void
{
if ((strlen($iban->getNormalizedIban()) !== $this->swiftRegistry->getIbanLength($iban->getCountryCode()))) {
throw new InvalidLengthException($iban);
@@ -182,15 +178,15 @@ protected function validateChecksum(Iban $iban): void
if (!preg_match('/^\d+$/', $checksum)) {
$validChecksumIban = $numericBban.$numericCountryCode.'00';
$validChecksum = 98 - intval($this->local_bcmod($validChecksumIban, '97'));
throw new InvalidChecksumException($iban, $validChecksum);
throw new InvalidChecksumException($iban, (string) $validChecksum);
}

$invertedIban = $numericBban.$numericCountryCode.$checksum;

if ('1' !== $this->local_bcmod($invertedIban, '97')) {
$validChecksumIban = $numericBban.$numericCountryCode.'00';
$validChecksum = 98 - intval($this->local_bcmod($validChecksumIban, '97'));
throw new InvalidChecksumException($iban, $validChecksum);
throw new InvalidChecksumException($iban, (string) $validChecksum);
}
}

@@ -199,7 +195,7 @@ private function getNumericRepresentation(string $letterRepresentation): string
$numericRepresentation = '';
foreach (str_split($letterRepresentation) as $char) {
if (array_search($char, $this->letterMap)) {
$numericRepresentation .= array_search($char, $this->letterMap) + 9;
$numericRepresentation .= (int) array_search($char, $this->letterMap) + 9;
} else {
$numericRepresentation .= $char;
}
@@ -208,7 +204,7 @@ private function getNumericRepresentation(string $letterRepresentation): string
return $numericRepresentation;
}

private function local_bcmod(string $operand, string $modulus): string
private function local_bcmod(string $operand, string $modulus): ?string
{
if (function_exists('bcmod')) {
return PHP_VERSION_ID >= 70200
@@ -220,7 +216,7 @@ private function local_bcmod(string $operand, string $modulus): string
$mod = '';

do {
$a = (int) $mod.substr($operand, 0, $take);
$a = intval($mod.substr($operand, 0, $take));
$operand = substr($operand, $take);
$mod = $a % $modulus;
} while (strlen($operand));