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

Fix incorrect check error while converting IP + Fix psalm #59

Merged
merged 8 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- 'phpunit.xml.dist'

push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'README.md'
Expand All @@ -28,4 +30,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['7.4', '8.0', '8.1', '8.2', '8.3']
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## 1.0.2 under development

- no changes in this release.
- Bug #59: Fix incorrect check error occurred while converting IP address to bits representation in PHP 8.0+ (@vjik)
vjik marked this conversation as resolved.
Show resolved Hide resolved

## 1.0.1 January 27, 2022

- Bug #40: Fix return type for callback of `set_error_handler()` function (devanych)
- Bug #40: Fix return type for callback of `set_error_handler()` function (@devanych)

## 1.0.0 March 04, 2021

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"rector/rector": "^1.2",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18"
"vimeo/psalm": "^4.30|^5.25"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
3 changes: 3 additions & 0 deletions src/DnsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
{
set_error_handler(static function (int $errorNumber, string $errorString) use ($hostname): bool {
throw new RuntimeException(
sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,

Check warning on line 25 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ public static function existsMx(string $hostname) : bool { set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { - throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); + throw new RuntimeException($errorString . sprintf('Failed to get DNS record "%s". ', $hostname), $errorNumber); }); $hostname = rtrim($hostname, '.') . '.'; $result = dns_get_record($hostname, DNS_MX);

Check warning on line 25 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ public static function existsMx(string $hostname) : bool { set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { - throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); + throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname), $errorNumber); }); $hostname = rtrim($hostname, '.') . '.'; $result = dns_get_record($hostname, DNS_MX);
$errorNumber
);
});

$hostname = rtrim($hostname, '.') . '.';

Check warning on line 30 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); }); - $hostname = rtrim($hostname, '.') . '.'; + $hostname = $hostname . '.'; $result = dns_get_record($hostname, DNS_MX); restore_error_handler(); return count($result) > 0;

Check warning on line 30 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); }); - $hostname = rtrim($hostname, '.') . '.'; + $hostname = rtrim($hostname, '.'); $result = dns_get_record($hostname, DNS_MX); restore_error_handler(); return count($result) > 0;
$result = dns_get_record($hostname, DNS_MX);

restore_error_handler();

Check warning on line 33 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ }); $hostname = rtrim($hostname, '.') . '.'; $result = dns_get_record($hostname, DNS_MX); - restore_error_handler(); + return count($result) > 0; } /**

return count($result) > 0;
}
Expand All @@ -46,14 +46,14 @@
{
set_error_handler(static function (int $errorNumber, string $errorString) use ($hostname): bool {
throw new RuntimeException(
sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString,

Check warning on line 49 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ public static function existsA(string $hostname) : bool { set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { - throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); + throw new RuntimeException($errorString . sprintf('Failed to get DNS record "%s". ', $hostname), $errorNumber); }); $result = dns_get_record($hostname, DNS_A); restore_error_handler();

Check warning on line 49 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ public static function existsA(string $hostname) : bool { set_error_handler(static function (int $errorNumber, string $errorString) use($hostname) : bool { - throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); + throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname), $errorNumber); }); $result = dns_get_record($hostname, DNS_A); restore_error_handler();
$errorNumber
);
});

$result = dns_get_record($hostname, DNS_A);

restore_error_handler();

Check warning on line 56 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ throw new RuntimeException(sprintf('Failed to get DNS record "%s". ', $hostname) . $errorString, $errorNumber); }); $result = dns_get_record($hostname, DNS_A); - restore_error_handler(); + return count($result) > 0; } /**

return count($result) > 0;
}
Expand All @@ -70,8 +70,11 @@
public static function acceptsEmails(string $hostnameOrEmail): bool
{
if (strpos($hostnameOrEmail, '@') !== false) {
/**
* @psalm-suppress PossiblyUndefinedArrayOffset In this cse `explode()` always returns array with 2 elements.
vjik marked this conversation as resolved.
Show resolved Hide resolved
*/
[, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2);

Check warning on line 76 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ /** * @psalm-suppress PossiblyUndefinedArrayOffset In this cse `explode()` always returns array with 2 elements. */ - [, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2); + [, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 3); } return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail); } }
}
return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail);

Check warning on line 78 in src/DnsHelper.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ */ [, $hostnameOrEmail] = explode('@', $hostnameOrEmail, 2); } - return self::existsMx($hostnameOrEmail) || self::existsA($hostnameOrEmail); + return self::existsMx($hostnameOrEmail) && self::existsA($hostnameOrEmail); } }
}
}
4 changes: 2 additions & 2 deletions src/IpHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class IpHelper
* @param string $ip The valid IPv4 or IPv6 address.
* @param bool $validate Enable perform IP address validation. False is best practice if the data comes from a trusted source.
*
* @return int {@see IPV4} or {@see IPV6}.
* @return int Value of {@see IPV4} or {@see IPV6} constants.
vjik marked this conversation as resolved.
Show resolved Hide resolved
*/
public static function getIpVersion(string $ip, bool $validate = true): int
{
Expand Down Expand Up @@ -173,7 +173,7 @@ public static function ip2bin(string $ip): string
$result = '';
for ($i = 0, $iMax = strlen($ipBinary); $i < $iMax; $i += 4) {
$data = substr($ipBinary, $i, 4);
if (!is_string($data)) {
if (empty($data)) {
throw new RuntimeException('An error occurred while converting IP address to bits representation.');
}
/** @psalm-suppress MixedArgument */
Expand Down