Skip to content

Commit

Permalink
Improve code to meet phpstan level 6
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 25, 2024
1 parent 48e0ad8 commit 18e0ff1
Show file tree
Hide file tree
Showing 23 changed files with 137 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: vendor/bin/phpunit

- name: Run phpstan
run: vendor/bin/phpstan analyze --no-progress --autoload-file=tests/phpstan/bootstrap.php --level=5 src/
run: vendor/bin/phpstan analyze --no-progress --autoload-file=tests/phpstan/bootstrap.php src/

- name: Validate coding standards
run: vendor/bin/phpcs
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
analysis:
build: ./
working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpstan analyze --level=4 ./src"
command: bash -c "composer install && ./vendor/bin/phpstan analyze ./src"
volumes:
- ./:/project
standards:
Expand Down
12 changes: 12 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
level: 6
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
uncheckedExceptionClasses:
- ImagickException
- ImagickDrawException
- ImagickPixelException
- Error
6 changes: 3 additions & 3 deletions src/Laravel/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function ($attribute, $value, $parameters, $validator) use ($rulename) {
* Return rule object for given shortname
*
* @param string $rulename
* @param array $parameters
* @param array<mixed> $parameters
* @return Rule
* @throws NotExistingRuleException
*/
Expand All @@ -58,7 +58,7 @@ private function getInterventionRule(string $rulename, array $parameters): Rule
/**
* List all shortnames of new rule objects
*
* @return array
* @return array<string>
*/
private function getRuleShortnames(): array
{
Expand All @@ -81,7 +81,7 @@ protected function getErrorMessage(string $rulename): string
/**
* Get the services provided by the provider.
*
* @return array
* @return array<string>
*/
public function provides()
{
Expand Down
12 changes: 6 additions & 6 deletions src/Rules/AustrianInsuranceNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AustrianInsuranceNumber extends AbstractRule
* Multiplier series to calculate checksum
* https://www.sozialversicherung.at/cdscontent/?contentid=10007.820902&viewmode=content
*
* @var array
* @var array<int>
*/
private array $multiplierSeries = [
3, 7, 9, 5, 8, 4, 2, 1, 6
Expand All @@ -33,25 +33,25 @@ class AustrianInsuranceNumber extends AbstractRule
*/
public function isValid(mixed $value): bool
{
$value = str_replace(' ', '', $value);
$value = str_replace(' ', '', strval($value));

return is_numeric($value)
&& $this->startsNotWithZero($value)
&& $this->hasValidLength($value)
&& $this->checkChecksum($value);
}

private function hasValidLength($svnumber): bool
private function hasValidLength(string $svnumber): bool
{
return $this->length === strlen($svnumber);
}

private function startsNotWithZero($svnumber): bool
private function startsNotWithZero(string $svnumber): bool
{
return (int) $svnumber[0] !== 0;
}

private function checkChecksum($svnumber): bool
private function checkChecksum(string $svnumber): bool
{
if (strlen($svnumber) !== $this->length) {
return false;
Expand All @@ -62,7 +62,7 @@ private function checkChecksum($svnumber): bool

$sum = 0;
for ($c = 0, $cMax = strlen($svnumberWithoutChecksum); $c < $cMax; $c++) {
$result = $svnumberWithoutChecksum[$c] * $this->multiplierSeries[$c];
$result = intval($svnumberWithoutChecksum[$c]) * $this->multiplierSeries[$c];
$sum += $result;
}
$checksum = $sum % 11;
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/Creditcard.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public function isValid(mixed $value): bool
/**
* Check if the given value has the proper length for creditcards
*
* @param mixed $value
* @return bool
*/
private function hasValidLength($value): bool
private function hasValidLength(mixed $value): bool
{
return (strlen($value) >= 13 && strlen($value) <= 19);
return (strlen(strval($value)) >= 13 && strlen(strval($value)) <= 19);
}
}
33 changes: 26 additions & 7 deletions src/Rules/DataUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DataUri extends AbstractRule
/**
* Create new instance with allowed media types or null for all valid media types
*
* @param null|array $media_types
* @param null|array<string> $media_types
* @return void
*/
public function __construct(protected ?array $media_types = null)
Expand All @@ -27,6 +27,7 @@ public function __construct(protected ?array $media_types = null)
public function isValid(mixed $value): bool
{
$info = $this->dataUriInfo($value);

if (!$info->isValid()) {
return false;
}
Expand Down Expand Up @@ -104,20 +105,37 @@ protected function isValidBase64EncodedValue(mixed $value): bool
/**
* Parse data url info from current value
*
* @param mixed $value
* @return object
*/
protected function dataUriInfo($value): object
protected function dataUriInfo(mixed $value): object
{
$pattern = "/^data:(?P<mediatype>\w+\/[-+.\w]+)?(?P<parameters>" .
"(;[-\w]+=[-\w]+)*)(?P<base64>;base64)?,(?P<data>.*)/";
$result = preg_match($pattern, $value, $matches);
$result = preg_match($pattern, strval($value), $matches);

return new class ($matches, $result)
{
private $matches;
private $result;

public function __construct($matches, $result)
/**
* Matches of regex operation
*
* @var array<mixed> $matches
*/
private array $matches;

/**
* Result of regex operation
*
* @var int|false $result
*/
private int|false $result;

/**
* @param array<mixed> $matches
* @param int|false $result
* @return void
*/
public function __construct(array $matches, int|false $result)
{
$this->matches = $matches;
$this->result = $result;
Expand All @@ -142,6 +160,7 @@ public function hasMediaType(): bool
return !empty($this->mediaType());
}

/** @return array<mixed> */
public function parameters(): array
{
if (isset($this->matches['parameters']) && !empty($this->matches['parameters'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Domainname.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function isValid(mixed $value): bool
/**
* Get all labels of domainname
*
* @return array
* @return array<string>
*/
private function getLabels(mixed $value): array
{
Expand Down
19 changes: 11 additions & 8 deletions src/Rules/Ean.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Ean extends AbstractRule
/**
* Create a new rule instance.
*
* @param array $lengths
* @param array<int> $lengths
* @return void
*/
public function __construct(protected array $lengths = [8, 13])
Expand All @@ -32,32 +32,35 @@ public function isValid(mixed $value): bool
/**
* Determine if the current value has the lengths of EAN-8 or EAN-13
*
* @param mixed $value
* @return bool
*/
public function hasAllowedLength($value): bool
public function hasAllowedLength(mixed $value): bool
{
return in_array(strlen((string) $value), $this->lengths);
return in_array(strlen(strval($value)), $this->lengths);
}

/**
* Try to calculate the EAN checksum of the
* current value and check the matching.
*
* @param mixed $value
* @return bool
*/
protected function checksumMatches($value): bool
protected function checksumMatches(mixed $value): bool
{
return $this->calculateChecksum($value) === $this->cutChecksum($value);
}

/**
* Cut out the checksum of the current value and return
*
* @param mixed $value
* @return int
*/
protected function cutChecksum($value): int
protected function cutChecksum(mixed $value): int
{
return intval(substr((string) $value, -1));
return intval(substr(strval($value), -1));
}

/**
Expand All @@ -66,12 +69,12 @@ protected function cutChecksum($value): int
* @param mixed $value
* @return int
*/
protected function calculateChecksum($value): int
protected function calculateChecksum(mixed $value): int
{
$checksum = 0;

// chars without check digit in reverse
$chars = array_reverse(str_split(substr((string) $value, 0, -1)));
$chars = array_reverse(str_split(substr(strval($value), 0, -1)));

foreach ($chars as $key => $char) {
$multiplier = $key % 2 ? 1 : 3;
Expand Down
4 changes: 4 additions & 0 deletions src/Rules/Gtin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class Gtin extends Ean
{
/**
* @param array<int> $lengths
* @return void
*/
public function __construct(protected array $lengths = [8, 12, 13, 14])
{
}
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/Hexadecimalcolor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Hexadecimalcolor extends AbstractRegexRule
/**
* Create a new rule instance.
*
* @param array $lengths
* @param array<int> $lengths
* @return void
*/
public function __construct(protected array $lengths = [3, 4, 6, 8])
Expand Down Expand Up @@ -41,10 +41,11 @@ public function isValid(mixed $value): bool
/**
* Determine if the current value has correct length
*
* @param mixed $value
* @return bool
*/
public function hasAllowedLength($value): bool
public function hasAllowedLength(mixed $value): bool
{
return in_array(strlen(trim($value, '#')), $this->lengths);
return in_array(strlen(trim(strval($value), '#')), $this->lengths);
}
}
24 changes: 17 additions & 7 deletions src/Rules/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Iban extends AbstractRule
/**
* IBAN lengths for countries
*
* @var array
* @var array<string, int>
*/
private $lengths = [
'AL' => 28,
Expand Down Expand Up @@ -130,7 +130,7 @@ class Iban extends AbstractRule
public function isValid(mixed $value): bool
{
// normalize value
$value = str_replace(' ', '', strtoupper($value));
$value = str_replace(' ', '', strtoupper(strval($value)));

// check iban length and checksum
return $this->hasValidLength($value) && $this->getChecksum($value) === 1;
Expand All @@ -142,7 +142,7 @@ public function isValid(mixed $value): bool
* @param string $iban
* @return int
*/
private function getChecksum($iban)
private function getChecksum(string $iban)
{
$iban = substr($iban, 4) . substr($iban, 0, 4);
$iban = str_replace(
Expand All @@ -168,7 +168,7 @@ private function getChecksum($iban)
* @param string $iban
* @return int
*/
private function getDesignatedIbanLength($iban)
private function getDesignatedIbanLength(string $iban)
{
$countrycode = substr($iban, 0, 2);

Expand All @@ -181,17 +181,27 @@ private function getDesignatedIbanLength($iban)
* @param string $iban
* @return bool
*/
private function hasValidLength($iban)
private function hasValidLength(string $iban): bool
{
return $this->getDesignatedIbanLength($iban) == strlen($iban);
}

private function getReplacementsChars()
/**
* Get chars to be replaced in checksum calculation
*
* @return array<string>
*/
private function getReplacementsChars(): array
{
return range('A', 'Z');
}

private function getReplacementsValues()
/**
* Get values to replace chars in checksum calculation
*
* @return array<string>
*/
private function getReplacementsValues(): array
{
$values = [];
foreach (range(10, 35) as $value) {
Expand Down
3 changes: 2 additions & 1 deletion src/Rules/Imei.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public function isValid(mixed $value): bool
/**
* Determine if current value has valid IMEI length
*
* @param string $value
* @return bool
*/
private function hasValidLength($value): bool
private function hasValidLength(string $value): bool
{
return strlen($value) === 15;
}
Expand Down

0 comments on commit 18e0ff1

Please sign in to comment.