Skip to content

Commit fd1cd75

Browse files
authored
Merge pull request #41 from madewithlove/modernize-php84
Modernize library to PHP 8.4
2 parents 5719411 + 0fa4f10 commit fd1cd75

14 files changed

+238
-216
lines changed

.github/workflows/ci.yml

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
1-
name: Continious Integration
2-
on: push
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
36

47
jobs:
58
phpunit:
69
runs-on: ubuntu-latest
7-
strategy:
8-
matrix:
9-
php-versions: ['8.0', '8.1']
10-
prefer-lowest: ['', '--prefer-lowest']
1110
steps:
12-
- uses: actions/checkout@v1
13-
- uses: shivammathur/setup-php@master
14-
with:
15-
php-version: ${{ matrix.php-versions }}
16-
coverage: pcov
17-
- name: Install dependencies
18-
run: composer update -n --prefer-dist ${{ matrix.prefer-lowest }}
19-
- name: Run unit tests
20-
run: vendor/bin/phpunit --coverage-clover=build/logs/clover.xml --whitelist src/
21-
- uses: codecov/codecov-action@v1
22-
with:
23-
token: ${{ secrets.CODECOV_TOKEN }}
24-
if: matrix.php-versions == '8.0'
11+
- uses: actions/checkout@v4
12+
- uses: shivammathur/setup-php@v2
13+
with:
14+
php-version: '8.4'
15+
coverage: xdebug
16+
- name: Install dependencies
17+
run: composer install --no-interaction --prefer-dist
18+
- name: Run tests
19+
run: vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
20+
- uses: codecov/codecov-action@v4
21+
with:
22+
token: ${{ secrets.CODECOV_TOKEN }}
2523

2624
infection:
2725
runs-on: ubuntu-latest
2826
steps:
29-
- uses: actions/checkout@v1
30-
- uses: actions/cache@v1
31-
with:
32-
path: vendor
33-
key: dependencies-${{ matrix.php-versions }}-${{ hashFiles('composer.json') }}
34-
- uses: shivammathur/setup-php@master
35-
with:
36-
php-version: '8.0'
37-
coverage: pcov
38-
- name: Install dependencies
39-
run: composer install -n --prefer-dist
40-
- name: Run mutation tests with GitHub logger
41-
run: |
42-
git fetch --depth=1 origin $GITHUB_BASE_REF
43-
vendor/bin/infection --show-mutations --debug -vvv --logger-github --git-diff-filter=A
27+
- uses: actions/checkout@v4
28+
- uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: '8.4'
31+
coverage: xdebug
32+
- name: Install dependencies
33+
run: composer install --no-interaction --prefer-dist
34+
- name: Run mutation tests
35+
run: |
36+
git fetch --depth=1 origin $GITHUB_BASE_REF || true
37+
vendor/bin/infection --show-mutations --logger-github
4438
4539
phpstan:
4640
runs-on: ubuntu-latest
4741
steps:
48-
- uses: actions/checkout@v1
49-
- uses: actions/cache@v1
42+
- uses: actions/checkout@v4
43+
- uses: shivammathur/setup-php@v2
5044
with:
51-
path: vendor
52-
key: dependencies-${{ matrix.php-versions }}-${{ hashFiles('composer.json') }}
53-
- uses: shivammathur/setup-php@master
54-
with:
55-
php-version: '8.0'
45+
php-version: '8.4'
5646
- name: Install dependencies
57-
run: composer install -n --prefer-dist
47+
run: composer install --no-interaction --prefer-dist
5848
- name: Run static analysis
5949
run: vendor/bin/phpstan analyse
50+
51+
cs:
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: actions/checkout@v4
55+
- uses: shivammathur/setup-php@v2
56+
with:
57+
php-version: '8.4'
58+
- name: Install dependencies
59+
run: composer install --no-interaction --prefer-dist
60+
- name: Check code style
61+
run: vendor/bin/php-cs-fixer fix --dry-run --diff

.php-cs-fixer.dist.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in([__DIR__ . '/src', __DIR__ . '/tests']);
5+
6+
return (new PhpCsFixer\Config())
7+
->setRules([
8+
'@PHP84Migration' => true,
9+
'@PSR12' => true,
10+
'declare_strict_types' => true,
11+
'strict_param' => true,
12+
'array_syntax' => ['syntax' => 'short'],
13+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
14+
'no_unused_imports' => true,
15+
])
16+
->setRiskyAllowed(true)
17+
->setFinder($finder);

composer.json

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
"description": "API client for the best htaccess tester in the world.",
44
"type": "package",
55
"require": {
6-
"php": "^8.0",
6+
"php": "^8.4",
77
"psr/http-client": "^1.0",
88
"psr/http-factory": "^1.0"
99
},
1010
"autoload": {
11-
"psr-4": {"Madewithlove\\": "src/"}
11+
"psr-4": {"Madewithlove\\HtaccessApiClient\\": "src/"}
1212
},
1313
"autoload-dev": {
14-
"psr-4": {"Madewithlove\\": "tests/"}
14+
"psr-4": {"Madewithlove\\HtaccessApiClient\\": "tests/"}
1515
},
1616
"license": "GPL-3.0-or-later",
1717
"require-dev": {
18-
"phpunit/phpunit": "^9.1",
19-
"http-interop/http-factory-guzzle": "^1.0",
20-
"infection/infection": "^0.26",
21-
"phpstan/phpstan": "^1.0.0",
22-
"php-http/guzzle7-adapter": "^1.0"
18+
"phpunit/phpunit": "^13.0",
19+
"guzzlehttp/guzzle": "^7.0",
20+
"guzzlehttp/psr7": "^2.0",
21+
"infection/infection": "^0.32",
22+
"phpstan/phpstan": "^2.0",
23+
"friendsofphp/php-cs-fixer": "^3.0"
2324
},
2425
"scripts": {
2526
"test": "phpunit",
26-
"stan": "phpstan analyse"
27+
"stan": "phpstan analyse",
28+
"cs-fix": "php-cs-fixer fix",
29+
"cs-check": "php-cs-fixer fix --dry-run --diff"
2730
},
2831
"config": {
2932
"allow-plugins": {

phpunit.xml.dist

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
convertWarningsToExceptions="false"
4-
colors="true">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true">
56
<testsuite name="Tests">
67
<directory>tests</directory>
78
</testsuite>
9+
<source>
10+
<include>
11+
<directory>src</directory>
12+
</include>
13+
</source>
814
</phpunit>

src/HtaccessClient.php

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
<?php declare(strict_types=1);
1+
<?php
22

3-
namespace Madewithlove;
3+
declare(strict_types=1);
4+
5+
namespace Madewithlove\HtaccessApiClient;
46

57
use Psr\Http\Client\ClientInterface;
68
use Psr\Http\Message\ServerRequestFactoryInterface;
@@ -18,7 +20,6 @@
1820
* }>,
1921
* 'output_status_code': int,
2022
* 'url':string,
21-
* 'errors'?: array<int,array{'field': string, 'message': string}>
2223
* }
2324
*/
2425
final class HtaccessClient
@@ -37,7 +38,7 @@ public function test(
3738
string $htaccess,
3839
?ServerVariables $serverVariables = null
3940
): HtaccessResult {
40-
$serverVariables = $serverVariables ?? ServerVariables::default();
41+
$serverVariables ??= ServerVariables::default();
4142
/** @var HtaccessResponseData */
4243
$responseData = $this->request(
4344
'POST',
@@ -52,16 +53,14 @@ public function test(
5253
return new HtaccessResult(
5354
$responseData['output_url'],
5455
array_map(
55-
function (array $line): ResultLine {
56-
return new ResultLine(
57-
$line['value'],
58-
$line['message'],
59-
$line['isMet'],
60-
$line['isValid'],
61-
$line['wasReached'],
62-
$line['isSupported']
63-
);
64-
},
56+
fn (array $line): ResultLine => new ResultLine(
57+
$line['value'],
58+
$line['message'],
59+
$line['isMet'],
60+
$line['isValid'],
61+
$line['wasReached'],
62+
$line['isSupported']
63+
),
6564
$responseData['lines']
6665
),
6766
$responseData['output_status_code']
@@ -76,15 +75,15 @@ public function share(
7675
string $htaccess,
7776
?ServerVariables $serverVariables = null
7877
): ShareResult {
79-
$serverVariables = $serverVariables ?? ServerVariables::default();
78+
$serverVariables ??= ServerVariables::default();
8079
/** @var HtaccessResponseData */
8180
$responseData = $this->request(
8281
'POST',
8382
'/share',
8483
[
8584
'url' => $url,
8685
'htaccess' => $htaccess,
87-
'serverVariables' => $serverVariables->toArray()
86+
'serverVariables' => $serverVariables->toArray(),
8887
]
8988
);
9089

@@ -105,16 +104,14 @@ public function getShared(string $shareUuid): HtaccessResult
105104
return new HtaccessResult(
106105
$responseData['output_url'],
107106
array_map(
108-
function (array $line) {
109-
return new ResultLine(
110-
$line['value'],
111-
$line['message'],
112-
$line['isMet'],
113-
$line['isValid'],
114-
$line['wasReached'],
115-
$line['isSupported']
116-
);
117-
},
107+
fn (array $line): ResultLine => new ResultLine(
108+
$line['value'],
109+
$line['message'],
110+
$line['isMet'],
111+
$line['isValid'],
112+
$line['wasReached'],
113+
$line['isSupported']
114+
),
118115
$responseData['lines']
119116
),
120117
$responseData['output_status_code']
@@ -132,25 +129,32 @@ private function request(string $method, string $endpoint = '', array $requestDa
132129
'https://htaccess.madewithlove.com/api' . $endpoint
133130
);
134131

135-
/** @var string $requestBody */
136-
$requestBody = json_encode($requestData);
132+
$requestBody = json_encode($requestData, JSON_THROW_ON_ERROR);
137133

138134
$body = $request->getBody();
139135
$body->write($requestBody);
140136

141137
$request = $request
142138
->withHeader('Content-Type', 'application/json')
139+
->withHeader('Accept', 'application/json')
143140
->withBody($body);
144141

145142
$response = $this->httpClient->sendRequest($request);
146143

147-
/** @var HtaccessResponseData */
144+
/** @var array{errors?: array<string, array<string>>}|HtaccessResponseData|null */
148145
$responseData = json_decode($response->getBody()->getContents(), true);
149146

147+
if (!is_array($responseData)) {
148+
throw new HtaccessException('Unexpected response from API');
149+
}
150+
150151
if (isset($responseData['errors'])) {
151-
throw HtaccessException::fromApiErrors($responseData['errors']);
152+
/** @var array<string, array<string>> $errors */
153+
$errors = $responseData['errors'];
154+
throw HtaccessException::fromApiErrors($errors);
152155
}
153156

157+
/** @var HtaccessResponseData $responseData */
154158
return $responseData;
155159
}
156160
}

src/HtaccessException.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
<?php declare(strict_types=1);
1+
<?php
22

3-
namespace Madewithlove;
3+
declare(strict_types=1);
4+
5+
namespace Madewithlove\HtaccessApiClient;
46

57
use Exception;
68

7-
class HtaccessException extends Exception
9+
final class HtaccessException extends Exception
810
{
911
/**
10-
* @param array<int,array{field: string, message: string}> $errors
12+
* @param array<string, array<string>> $errors
1113
*/
1214
public static function fromApiErrors(array $errors): self
1315
{
14-
$errorMessages = array_map(
15-
function (array $error): string {
16-
return $error['field'] . ': ' . $error['message'];
17-
},
18-
$errors
19-
);
16+
$errorMessages = [];
17+
foreach ($errors as $field => $messages) {
18+
foreach ($messages as $message) {
19+
$errorMessages[] = $field . ': ' . $message;
20+
}
21+
}
2022

2123
return new self(implode("\n", $errorMessages));
2224
}

src/HtaccessResult.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<?php declare(strict_types=1);
1+
<?php
22

3-
namespace Madewithlove;
3+
declare(strict_types=1);
44

5-
final class HtaccessResult
5+
namespace Madewithlove\HtaccessApiClient;
6+
7+
final readonly class HtaccessResult
68
{
79
/**
810
* @param ResultLine[] $lines

src/ResultLine.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
<?php declare(strict_types=1);
1+
<?php
22

3-
namespace Madewithlove;
3+
declare(strict_types=1);
44

5-
final class ResultLine
5+
namespace Madewithlove\HtaccessApiClient;
6+
7+
final readonly class ResultLine
68
{
79
public function __construct(
810
private string $line,

src/ServerVariable.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)