Skip to content

Commit

Permalink
Fix tests, Psalm level 1, add BC GitHub workflow and cleanup other wo…
Browse files Browse the repository at this point in the history
…rkflows (#39)
  • Loading branch information
vjik committed Oct 16, 2021
1 parent aaf1f40 commit 345dcf4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -12,3 +12,6 @@ trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
15 changes: 15 additions & 0 deletions .github/workflows/bc.yml
@@ -0,0 +1,15 @@
on:
- pull_request
- push

name: backwards compatibility
jobs:
roave_bc_check:
name: Roave BC Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: fetch tags
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
- name: Roave BC Check
uses: docker://nyholm/roave-bc-check-ga
15 changes: 0 additions & 15 deletions .github/workflows/bc.yml_

This file was deleted.

13 changes: 4 additions & 9 deletions .github/workflows/build.yml
Expand Up @@ -22,12 +22,12 @@ jobs:
- windows-latest

php:
- "7.4"
- "8.0"
- 7.4
- 8.0

steps:
- name: Checkout
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -56,13 +56,8 @@ jobs:
- name: Update composer
run: composer self-update

- name: Install dependencies with composer php 7.4
if: matrix.php == '7.4'
- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.0
if: matrix.php == '8.0'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit
run: vendor/bin/phpunit --colors=always
8 changes: 4 additions & 4 deletions .github/workflows/mutation.yml
Expand Up @@ -18,18 +18,18 @@ jobs:
- ubuntu-latest

php:
- "7.4"
- 7.4

steps:
- name: Checkout
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
php-version: ${{ matrix.php }}
ini-values: memory_limit=-1
coverage: "pcov"
coverage: pcov
tools: composer:v2

- name: Determine composer cache directory
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/static.yml
Expand Up @@ -18,16 +18,17 @@ jobs:
- ubuntu-latest

php:
- "7.4"
- 7.4
- 8.0

steps:
- name: Checkout
uses: actions/checkout@v2.3.4
uses: actions/checkout@v2

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
php-version: ${{ matrix.php }}
tools: composer:v2, cs2pr
coverage: none

Expand Down
3 changes: 1 addition & 2 deletions psalm.xml
@@ -1,7 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
errorLevel="1"
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
9 changes: 7 additions & 2 deletions src/IpHelper.php
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use RuntimeException;

use function assert;
use function is_string;
use function strlen;
Expand Down Expand Up @@ -120,7 +121,7 @@ public static function inRange(string $subnet, string $range): bool

$binIp = self::ip2bin($ip);
$binNet = self::ip2bin($net);
$masked = substr($binNet, 0, (int)$netMask);
$masked = substr($binNet, 0, (int) $netMask);

return ($masked === '' || strpos($binIp, $masked) === 0) && $mask >= $netMask;
}
Expand All @@ -143,7 +144,10 @@ public static function expandIPv6(string $ip): string
}
throw new InvalidArgumentException("Unrecognized address $ip.");
}

/** @psalm-var array{hex:string} $hex */
$hex = unpack('H*hex', $ipRaw);

return substr(preg_replace('/([a-f0-9]{4})/i', '$1:', $hex['hex']), 0, -1);
}

Expand Down Expand Up @@ -172,6 +176,7 @@ public static function ip2bin(string $ip): string
if (!is_string($data)) {
throw new RuntimeException('An error occurred while converting IP address to bits representation.');
}
/** @psalm-suppress MixedArgument */
$result .= str_pad(decbin(unpack('N', $data)[1]), 32, '0', STR_PAD_LEFT);
}
return $result;
Expand All @@ -195,7 +200,7 @@ public static function getCidrBits(string $ip): int
if ($bits === null) {
return $maxBits;
}
$bits = (int)$bits;
$bits = (int) $bits;
if ($bits < 0) {
throw new InvalidArgumentException('The number of CIDR bits cannot be negative.', 2);
}
Expand Down
11 changes: 7 additions & 4 deletions tests/DnsHelperTest.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\NetworkUtilities\Tests;

use PHPUnit\Framework\TestCase;
use RuntimeException;
use Yiisoft\NetworkUtilities\DnsHelper;

/**
Expand All @@ -23,8 +24,9 @@ public function testMx(): void

public function testMxWithWrongDomain(): void
{
$this->expectException(\RuntimeException::class);
DnsHelper::existsMx('ya2.ru');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to get DNS record ".wrongdomain".');
DnsHelper::existsMx('.wrongdomain');
}

public function testA(): void
Expand All @@ -35,8 +37,9 @@ public function testA(): void

public function testAWithWrongDomain(): void
{
$this->expectException(\RuntimeException::class);
DnsHelper::existsA('ya2.ru');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to get DNS record ".wrongdomain".');
DnsHelper::existsA('.wrongdomain');
}

public function testAcceptsEmail(): void
Expand Down

0 comments on commit 345dcf4

Please sign in to comment.