Skip to content

Commit

Permalink
Modernize code (#92)
Browse files Browse the repository at this point in the history
* Update phpstan version
* Modernize code
  • Loading branch information
olivervogel authored Jan 5, 2025
1 parent 04bb683 commit 10129ad
Show file tree
Hide file tree
Showing 20 changed files with 55 additions and 101 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"require-dev": {
"phpunit/phpunit": "^10.0 || ^11.0",
"phpstan/phpstan": "^2",
"phpstan/phpstan": "^2.1",
"symfony/uid": "^5.1|^6.2",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractRegexRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ abstract protected function pattern(): string;
*/
public function isValid(mixed $value): bool
{
return (bool) preg_match($this->pattern(), $value);
return (bool) preg_match($this->pattern(), (string) $value);
}
}
12 changes: 6 additions & 6 deletions src/Laravel/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ public function boot()
foreach ($this->ruleShortnames() as $rulename) {
$this->app['validator']->extend(
$rulename,
function ($attribute, $value, $parameters, $validator) use ($rulename) {
return $this->interventionRule($rulename, $parameters)
->isValid($value);
function ($attribute, $value, $parameters, $validator) use ($rulename): bool {
return $this->interventionRule($rulename, $parameters)->isValid($value);
},
$this->errorMessage($rulename)
);
Expand Down Expand Up @@ -62,9 +61,10 @@ private function interventionRule(string $rulename, array $parameters): Rule
*/
private function ruleShortnames(): array
{
return array_map(function ($filename) {
return mb_strtolower(substr($filename, 0, -4));
}, array_diff(scandir(__DIR__ . '/../Rules'), ['.', '..']));
return array_map(
fn(string $filename): string => mb_strtolower(substr($filename, 0, -4)),
array_diff(scandir(__DIR__ . '/../Rules'), ['.', '..']),
);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Base64 extends AbstractRule
*/
public function isValid(mixed $value): bool
{
$decoded = base64_decode($value, true);
$decoded = base64_decode((string) $value, true);

return $decoded ? base64_encode($decoded) === $value : false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Cidr.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function isValid(mixed $value): bool
// A CIDR should consist of an IP part and a mask bit number delimited by a `/`

// split by the slash that should be there
$parts = explode('/', $value, 2);
$parts = explode('/', (string) $value, 2);
// we should have 2 parts (the ip and mask)
if (count($parts) !== 2) {
return false;
Expand Down
37 changes: 6 additions & 31 deletions src/Rules/DataUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,11 @@ private function isAllowedMimeType(mixed $type): bool
return true;
}

if (count($this->media_types) === 0) {
if ($this->media_types === []) {
return false;
}

foreach ($this->media_types as $allowed) {
if ($type === $allowed) {
return true;
}
}

return false;
return in_array($type, $this->media_types, true);
}

private function isValidBase64EncodedValue(mixed $value): bool
Expand All @@ -115,29 +109,14 @@ private function dataUriInfo(mixed $value): object

return new class ($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)
public function __construct(private array $matches, private int|false $result)
{
$this->matches = $matches;
$this->result = $result;
//
}

public function isValid(): bool
Expand All @@ -163,19 +142,15 @@ public function hasMediaType(): bool
public function parameters(): array
{
if (isset($this->matches['parameters']) && !empty($this->matches['parameters'])) {
return explode(';', trim($this->matches['parameters'], ';'));
return explode(';', trim((string) $this->matches['parameters'], ';'));
}

return [];
}

public function isBase64Encoded(): bool
{
if (isset($this->matches['base64']) && $this->matches['base64'] === ';base64') {
return true;
}

return false;
return isset($this->matches['base64']) && $this->matches['base64'] === ';base64';
}

public function data(): ?string
Expand Down
11 changes: 3 additions & 8 deletions src/Rules/Domainname.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public function isValid(mixed $value): bool
}
}

// tld must be valid
if (!$this->isValidTld($tld)) {
return false;
}

return true;
return $this->isValidTld($tld);
}

/**
Expand Down Expand Up @@ -69,7 +64,7 @@ private function isValidLabel(string $value): bool
*/
private function isValidALabel(string $value): bool
{
return substr($value, 0, 4) === 'xn--' && $this->idnToUtf8($value) != false;
return str_starts_with($value, 'xn--') && $this->idnToUtf8($value) != false;
}

/**
Expand Down Expand Up @@ -119,6 +114,6 @@ private function idnToAscii(string $domain): string
{
$domain = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);

return $domain ? $domain : '';
return $domain ?: '';
}
}
1 change: 1 addition & 0 deletions src/Rules/Ean.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Ean extends AbstractRule
*/
public function __construct(private array $lengths = [8, 13])
{
//
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function charValue(string $char): int

return (int) str_replace(
range('A', 'Z'),
array_map(fn (int $val) => strval($val), range(10, 35)),
array_map(fn(int $val): string => strval($val), range(10, 35)),
strtoupper($char),
);
}
Expand Down
19 changes: 6 additions & 13 deletions src/Rules/Gtin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,11 @@ public function isValid(mixed $value): bool
return false;
}

switch (strlen($value)) {
case 8:
case 13:
return parent::isValid($value);

case 14:
return parent::checksumMatches(substr($value, 1));

case 12:
return parent::checksumMatches('0' . $value);
}

return false;
return match (strlen($value)) {
8, 13 => parent::isValid($value),
12 => parent::checksumMatches('0' . $value),
14 => parent::checksumMatches(substr($value, 1)),
default => false,
};
}
}
2 changes: 1 addition & 1 deletion src/Rules/Htmlclean.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function isValid(mixed $value): bool
return true;
}

return (strip_tags($value) == $value);
return strip_tags((string) $value) == $value;
}
}
4 changes: 2 additions & 2 deletions src/Rules/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Iban extends AbstractRule
*
* @var array<string, int>
*/
private $lengths = [
private array $lengths = [
'AL' => 28,
'AD' => 24,
'AT' => 20,
Expand Down Expand Up @@ -171,7 +171,7 @@ private function designatedIbanLength(string $iban): int|false
{
$countrycode = substr($iban, 0, 2);

return isset($this->lengths[$countrycode]) ? $this->lengths[$countrycode] : false;
return $this->lengths[$countrycode] ?? false;
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/Rules/Isbn.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ public function __construct(private array $lengths = [10, 13])
public function isValid(mixed $value): bool
{
// normalize value
$value = preg_replace("/[^0-9x]/i", '', $value);
$value = preg_replace("/[^0-9x]/i", '', (string) $value);

if (!$this->hasAllowedLength($value)) {
return false;
}

switch (strlen($value)) {
case 10:
return $this->shortChecksumMatches($value);

case 13: // isbn-13 is a subset of ean-13
return preg_match("/^(978|979)/", $value) && parent::checksumMatches($value);
}

return false;
return match (strlen((string) $value)) {
10 => $this->shortChecksumMatches($value),
13 => preg_match("/^(978|979)/", (string) $value) && parent::checksumMatches($value),
default => false,
};
}

/**
Expand All @@ -62,7 +58,7 @@ private function shortChecksum(string $value): int
$checksum = 0;
$multiplier = 10;
foreach (str_split($value) as $digit) {
$digit = strtolower($digit) == 'x' ? 10 : intval($digit);
$digit = strtolower($digit) === 'x' ? 10 : intval($digit);
$checksum += $digit * $multiplier;
$multiplier--;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Isin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function replaceChars(string $value): string
{
return str_replace(
$this->chars,
array_map(fn (int $value) => strval($value), array_keys($this->chars)),
array_map(fn(int $value): string => strval($value), array_keys($this->chars)),
$value,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Lowercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Lowercase extends AbstractRule
*/
public function isValid(mixed $value): bool
{
$lowerCaseValue = mb_strtolower(strval($value), mb_detect_encoding($value));
$lowerCaseValue = mb_strtolower(strval($value), mb_detect_encoding((string) $value));

return $value === $lowerCaseValue;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Rules/Luhn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function isValid(mixed $value): bool
/**
* Determine if the given checksum is valid
*
* @param mixed $checksum
* @param int $checksum
* @return bool
*/
private function checksumIsValid($checksum): bool
private function checksumIsValid(int $checksum): bool
{
return $checksum % 10 === 0;
}
Expand All @@ -35,7 +35,7 @@ private function checksumIsValid($checksum): bool
* @param mixed $value
* @return int
*/
private function checksum($value): int
private function checksum(mixed $value): int
{
$checksum = 0;
$reverse = strrev(strval($value));
Expand Down
22 changes: 10 additions & 12 deletions src/Rules/Postalcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function countrycode(array $countrycodes): self
public function isValid(mixed $value): bool
{
foreach ($this->patterns() as $pattern) {
if (preg_match($pattern, $value)) {
if (preg_match($pattern, (string) $value)) {
return true;
}
}
Expand All @@ -95,13 +95,9 @@ public function isValid(mixed $value): bool
*/
private function patterns(): array
{
$patterns = array_map(function ($countrycode) {
return $this->pattern($countrycode);
}, $this->countryCodes());
$patterns = array_map(fn(string $countrycode): ?string => $this->pattern($countrycode), $this->countryCodes());

return array_filter($patterns, function ($pattern) {
return !is_null($pattern);
});
return array_filter($patterns, fn(?string $pattern): bool => !is_null($pattern));
}

/**
Expand All @@ -111,11 +107,13 @@ private function patterns(): array
*/
private function countryCodes(): array
{
if (count($this->countrycodes) == 0) {
// return country code by reference
if (!is_null($this->reference) && array_key_exists($this->reference, $this->data)) {
return [$this->data[$this->reference]];
}
if (count($this->countrycodes) != 0) {
return $this->countrycodes;
}

// return country code by reference
if (!is_null($this->reference) && array_key_exists($this->reference, $this->data)) {
return [$this->data[$this->reference]];
}

return $this->countrycodes;
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Titlecase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function isValid(mixed $value): bool
*/
private function words(mixed $value): array
{
return explode(" ", $value);
return explode(" ", (string) $value);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Rules/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ public function isValid(mixed $value): bool
return false;
}

if ($this->ulidTooLarge($value)) {
return false;
}

return true;
return !$this->ulidTooLarge($value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Uppercase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Uppercase extends AbstractRule
*/
public function isValid(mixed $value): bool
{
$upperCaseValue = mb_strtoupper(strval($value), mb_detect_encoding($value));
$upperCaseValue = mb_strtoupper(strval($value), mb_detect_encoding((string) $value));

return $value === $upperCaseValue;
}
Expand Down

0 comments on commit 10129ad

Please sign in to comment.