Skip to content

Commit 88d3baa

Browse files
committed
feat: first commit
0 parents  commit 88d3baa

18 files changed

+3708
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 4
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2
16+
17+
[*.md]
18+
trim_trailing_whitespace = false

.github/dependabot.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Please see the documentation for all configuration options:
2+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
3+
4+
version: 2
5+
updates:
6+
7+
# Maintain dependencies for Composer
8+
- package-ecosystem: "composer"
9+
directory: "/"
10+
schedule:
11+
interval: "weekly"
12+
13+
# Maintain dependencies for GitHub Actions
14+
- package-ecosystem: "github-actions"
15+
directory: "/"
16+
schedule:
17+
interval: "weekly"

.github/workflows/test.yml

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
name: 'Run tests'
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- '**/*.php'
7+
- '.github/workflows/*'
8+
push:
9+
paths:
10+
- '**/*.php'
11+
- '.github/workflows/*'
12+
13+
jobs:
14+
cs:
15+
name: 'Check coding style'
16+
runs-on: 'ubuntu-20.04'
17+
18+
steps:
19+
- name: 'Checkout current revision'
20+
uses: 'actions/checkout@v3'
21+
22+
- name: 'Setup PHP'
23+
uses: 'shivammathur/setup-php@v2'
24+
with:
25+
php-version: '8.1'
26+
tools: 'composer'
27+
coverage: 'none'
28+
29+
- name: 'Discover Composer cache directory'
30+
id: 'cachedir'
31+
run: 'echo "::set-output name=path::$(composer global config cache-dir)"'
32+
33+
- name: 'Share Composer cache across runs'
34+
uses: 'actions/cache@v3'
35+
with:
36+
path: '${{ steps.cachedir.outputs.path }}'
37+
key: "composer-${{ github.job }}-${{ hashFiles('**/composer.json') }}"
38+
restore-keys: |
39+
composer-${{ github.job }}-
40+
composer-
41+
42+
- name: 'Install dependencies with Composer'
43+
run: 'composer install --prefer-dist --no-interaction'
44+
45+
- name: 'Run PHP CodeSniffer'
46+
run: 'composer run-script cs-check -- -n'
47+
48+
stan:
49+
name: 'Static code analyzer'
50+
runs-on: 'ubuntu-20.04'
51+
continue-on-error: true
52+
53+
steps:
54+
- name: 'Checkout current revision'
55+
uses: 'actions/checkout@v3'
56+
57+
- name: 'Setup PHP'
58+
uses: 'shivammathur/setup-php@v2'
59+
with:
60+
php-version: '8.1'
61+
tools: 'composer'
62+
coverage: 'none'
63+
64+
- name: 'Discover Composer cache directory'
65+
id: 'cachedir'
66+
run: 'echo "::set-output name=path::$(composer global config cache-dir)"'
67+
68+
- name: 'Share Composer cache across runs'
69+
uses: 'actions/cache@v3'
70+
with:
71+
path: '${{ steps.cachedir.outputs.path }}'
72+
key: "composer-${{ github.job }}-${{ hashFiles('**/composer.json') }}"
73+
restore-keys: |
74+
composer-${{ github.job }}-
75+
composer-
76+
77+
- name: 'Install dependencies with Composer'
78+
run: 'composer install --prefer-dist --no-interaction'
79+
80+
- name: 'Run PHP STAN'
81+
run: 'composer run-script stan -- --no-progress --error-format=github'
82+
83+
unit:
84+
name: 'Run unit tests'
85+
if: "!contains(github.event.commits[0].message, '[skip ci]') && !contains(github.event.commits[0].message, '[ci skip]')"
86+
runs-on: 'ubuntu-20.04'
87+
88+
strategy:
89+
fail-fast: false
90+
matrix:
91+
php:
92+
- '7.4'
93+
- '8.0'
94+
- '8.1'
95+
96+
env:
97+
PHP_VERSION: '${{ matrix.php }}'
98+
99+
steps:
100+
- name: 'Checkout current revision'
101+
uses: 'actions/checkout@v3'
102+
103+
- name: 'Setup PHP'
104+
uses: 'shivammathur/setup-php@v2'
105+
with:
106+
php-version: '${{ matrix.php }}'
107+
tools: 'composer'
108+
coverage: 'xdebug'
109+
110+
- name: 'Discover Composer cache directory'
111+
id: 'cachedir'
112+
run: 'echo "::set-output name=path::$(composer global config cache-dir)"'
113+
114+
- name: 'Share Composer cache across runs'
115+
uses: 'actions/cache@v3'
116+
with:
117+
path: '${{ steps.cachedir.outputs.path }}'
118+
key: "composer-${{ matrix.php }}-${{ hashFiles('**/composer.json') }}"
119+
restore-keys: |
120+
composer-${{ matrix.php }}-
121+
composer-
122+
123+
- name: 'Install dependencies with Composer'
124+
run: 'composer install --prefer-dist --no-interaction'
125+
126+
- name: 'Dump Composer autoloader'
127+
run: 'composer dump-autoload --classmap-authoritative --no-cache'
128+
129+
- name: 'Run PHPUnit'
130+
run: 'phpdbg -qrr vendor/bin/phpunit --coverage-clover=clover.xml'
131+
132+
- name: 'Export coverage results'
133+
uses: 'codecov/codecov-action@v3'
134+
with:
135+
file: './clover.xml'
136+
env_vars: PHP_VERSION
137+
138+
- name: 'Archive code coverage results'
139+
uses: 'actions/upload-artifact@v3'
140+
with:
141+
name: 'PHP ${{ matrix.php }}'
142+
path: 'clover.xml'
143+
144+
unit-lowest:
145+
name: 'Run unit tests with lowest-matching dependencies versions'
146+
if: "!contains(github.event.commits[0].message, '[skip ci]') && !contains(github.event.commits[0].message, '[ci skip]')"
147+
runs-on: 'ubuntu-20.04'
148+
149+
steps:
150+
- name: 'Checkout current revision'
151+
uses: 'actions/checkout@v3'
152+
153+
- name: 'Setup PHP'
154+
uses: 'shivammathur/setup-php@v2'
155+
with:
156+
php-version: '8.1'
157+
tools: 'composer'
158+
159+
- name: 'Discover Composer cache directory'
160+
id: 'cachedir'
161+
run: 'echo "::set-output name=path::$(composer global config cache-dir)"'
162+
163+
- name: 'Share Composer cache across runs'
164+
uses: 'actions/cache@v3'
165+
with:
166+
path: '${{ steps.cachedir.outputs.path }}'
167+
key: "composer-lowest-${{ hashFiles('**/composer.json') }}"
168+
restore-keys: |
169+
composer-lowest-
170+
composer-
171+
172+
- name: 'Update dependencies with Composer'
173+
run: 'composer update --prefer-lowest --prefer-dist --no-interaction'
174+
175+
- name: 'Dump Composer autoloader'
176+
run: 'composer dump-autoload --classmap-authoritative --no-cache'
177+
178+
- name: 'Run PHPUnit'
179+
run: 'vendor/bin/phpunit'

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.phpunit.cache/
2+
/composer.lock
3+
/coverage/
4+
/phpcs.xml
5+
/phpstan.neon
6+
/phpunit.xml
7+
/vendor/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Chia Lab s.r.l.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
IP Address library for PHP
2+
==========================
3+
4+
This library for PHP 7.4+ builds an abstraction over management of
5+
Internet Protocol versions, addresses and CIDR blocks.
6+
7+
Using this library makes it easy to check if an IP address belongs to a subnet or not.
8+
9+
## Installation
10+
11+
Installing this library can be done via Composer:
12+
13+
```console
14+
$ composer require chialab/ip
15+
```
16+
17+
## Usage
18+
19+
```php
20+
use Chialab\Ip;
21+
22+
$address = Ip\Address::parse('192.168.1.1');
23+
var_dump($address->getProtocolVersion() === Ip\ProtocolVersion::ipv4()); // bool(true)
24+
var_dump($address->getProtocolVersion() === Ip\ProtocolVersion::ipv6()); // bool(false)
25+
26+
$subnet = Ip\Subnet::parse('fec0::1/16');
27+
var_dump((string)$subnet->getFirstAddress()); // string(6): "fec0::"
28+
var_dump((string)$subnet->getNetmask()); // string(6) "ffff::"
29+
var_dump($subnet->contains($address)); // bool(false)
30+
var_dump($subnet->contains(Ip\Address::parse('fec0:fe08:0123:4567:89ab:cdef:1234:5678'))); // bool(true)
31+
var_dump($subnet->hasSubnet(Ip\Subnet::parse('fec0:fe08::/32'))); // bool(true)
32+
```

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "chialab/ip",
3+
"description": "Minimal library to manage IP addresses, subnets, netmasks, etc.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Chialab srl",
9+
"email": "dev@chialab.io"
10+
}
11+
],
12+
"require": {
13+
"php": ">= 7.4"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "^9.5",
17+
"phpstan/phpstan": "^1.7",
18+
"cakephp/cakephp-codesniffer": "^4.5",
19+
"phpstan/extension-installer": "^1.1",
20+
"phpstan/phpstan-webmozart-assert": "^1.2",
21+
"phpstan/phpstan-phpunit": "^1.1"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Chialab\\Ip\\": "./src"
26+
}
27+
},
28+
"autoload-dev": {
29+
"psr-4": {
30+
"Chialab\\Ip\\Test\\": "./tests"
31+
}
32+
},
33+
"scripts": {
34+
"check": [
35+
"@test",
36+
"@stan",
37+
"@cs-check"
38+
],
39+
"cs-check": "phpcs",
40+
"cs-fix": "phpcbf",
41+
"stan": "phpstan analyse --memory-limit=-1",
42+
"test": "@unit",
43+
"unit": "phpunit --colors=always"
44+
},
45+
"config": {
46+
"allow-plugins": {
47+
"dealerdirect/phpcodesniffer-composer-installer": true,
48+
"phpstan/extension-installer": true
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)