Skip to content

Commit

Permalink
Applied phpstan rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaedl committed May 17, 2020
1 parent 7b175ee commit f61c3db
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 32 deletions.
14 changes: 5 additions & 9 deletions src/CountryInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
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));
}
}
3 changes: 3 additions & 0 deletions src/Exception/InvalidChecksumException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class InvalidChecksumException extends \RuntimeException
{
/**
* @var string
*/
protected $validChecksum;

public function __construct(string $iban, string $validChecksum)
Expand Down
12 changes: 7 additions & 5 deletions src/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Iban\Validation;

use Iban\Validation\Exception\CanNotBeNormalizedException;

/**
* Represents an International Bank Account Number (IBAN).
*
Expand Down Expand Up @@ -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
Expand Down
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('Could 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('Could not load contents of file "%s"', $filename));
}
}
25 changes: 20 additions & 5 deletions src/Swift/RegexConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand Up @@ -11,6 +11,7 @@

namespace Iban\Validation\Swift;

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

/**
Expand All @@ -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
Expand Up @@ -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);
Expand Down Expand Up @@ -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!',
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

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

0 comments on commit f61c3db

Please sign in to comment.