Skip to content

Commit 02feb7d

Browse files
Merge pull request #9 from aarondfrancis/ReFactor
Modernize codebase with PHP 8.1+ features and expand test coverage
2 parents 4f7dee2 + 46f1463 commit 02feb7d

File tree

16 files changed

+424
-115
lines changed

16 files changed

+424
-115
lines changed

.github/workflows/style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup PHP
1919
uses: shivammathur/setup-php@v2
2020
with:
21-
php-version: "8.3"
21+
php-version: "8.4"
2222
extensions: json, dom, curl, libxml, mbstring
2323
coverage: none
2424

.github/workflows/tests.yml

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
fail-fast: false
1919
matrix:
20-
php: [ 8.1, 8.2, 8.3 ]
20+
php: [ 8.1, 8.2, 8.3, 8.4 ]
2121
laravel: [ '10.*', '11.*', '12.*' ]
2222
dependency-version: [ prefer-lowest, prefer-stable ]
2323

@@ -29,19 +29,6 @@ jobs:
2929

3030
name: P${{ matrix.php }} / L${{ matrix.laravel }} / ${{ matrix.dependency-version }}
3131

32-
services:
33-
mysql:
34-
image: mysql:8.0
35-
env:
36-
MYSQL_DATABASE: fast_paginate
37-
MYSQL_HOST: 127.0.0.1
38-
MYSQL_USER: test
39-
MYSQL_PASSWORD: root
40-
MYSQL_ROOT_PASSWORD: root
41-
ports:
42-
- 3306:3306
43-
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
44-
4532
steps:
4633
- name: Checkout code
4734
uses: actions/checkout@v4

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ node_modules
1212
docs/*.blade.php
1313
docs/**/*.blade.php
1414
.phpunit.cache/test-results
15+
.claude/settings.local.json

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^8.0",
14-
"illuminate/support": "^10|^11.0|^12.0",
15-
"illuminate/cache": "^10|^11.0|^12.0",
16-
"illuminate/console": "^10|^11.|^12.00"
13+
"php": "^8.1",
14+
"illuminate/support": "^10.0|^11.0|^12.0",
15+
"illuminate/cache": "^10.0|^11.0|^12.0",
16+
"illuminate/console": "^10.0|^11.0|^12.0"
1717
},
1818
"require-dev": {
19-
"mockery/mockery": "^1.3.3",
20-
"phpunit/phpunit": ">=8.5.23|^9|^10",
21-
"orchestra/testbench": "^8.0|^9.0|^10.0"
19+
"mockery/mockery": "^1.6",
20+
"phpunit/phpunit": "^10.5|^11.0",
21+
"orchestra/testbench": "^8.21|^9.5|^10.0"
2222
},
2323
"autoload": {
2424
"psr-4": {

src/Arbiter.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,102 +6,106 @@
66

77
namespace AaronFrancis\Flaky;
88

9+
use Illuminate\Contracts\Cache\Repository;
910
use Illuminate\Support\Arr;
1011
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Traits\Macroable;
13+
use Throwable;
1214

1315
class Arbiter
1416
{
1517
use Macroable;
1618

17-
public $failuresAllowedForSeconds = 60 * 60 * 24 * 365 * 10;
19+
public int $failuresAllowedForSeconds = 60 * 60 * 24 * 365 * 10;
1820

19-
public $consecutiveFailuresAllowed = INF;
21+
public int|float $consecutiveFailuresAllowed = INF;
2022

21-
public $totalFailuresAllowed = INF;
23+
public int|float $totalFailuresAllowed = INF;
2224

25+
/** @var callable(Throwable): void */
2326
public $handleFailuresWith;
2427

25-
protected $key;
28+
protected string $key;
2629

27-
protected $totalFailures;
30+
protected int $totalFailures;
2831

29-
protected $consecutiveFailures;
32+
protected int $consecutiveFailures;
3033

31-
protected $deadline;
34+
protected ?int $deadline;
3235

33-
protected $cache;
36+
protected Repository $cache;
3437

35-
public function __construct($id)
38+
public function __construct(string $id)
3639
{
3740
$this->key = "flaky::$id";
3841
$this->cache = Cache::store();
3942

43+
/** @var array{total?: int, consecutive?: int, deadline?: int|null} $stats */
4044
$stats = $this->cache->get($this->key, []);
4145

4246
$this->totalFailures = Arr::get($stats, 'total', 0);
4347
$this->consecutiveFailures = Arr::get($stats, 'consecutive', 0);
4448
$this->deadline = Arr::get($stats, 'deadline');
4549

46-
$this->handleFailuresWith = function ($e) {
50+
$this->handleFailuresWith = function (Throwable $e): never {
4751
throw $e;
4852
};
4953
}
5054

51-
public function handle($exception)
55+
public function handle(?Throwable $exception): void
5256
{
5357
$this->deadline = $this->deadline ?? $this->freshDeadline();
5458

55-
if ($exception) {
59+
if ($exception !== null) {
5660
$this->totalFailures++;
5761
$this->consecutiveFailures++;
5862
}
5963

6064
$this->updateCachedStats($exception);
6165

62-
if (!is_null($exception) && $this->outOfBounds()) {
66+
if ($exception !== null && $this->outOfBounds()) {
6367
$this->callHandler($exception);
6468
}
6569
}
6670

67-
public function handleFailures($callback)
71+
public function handleFailures(callable $callback): void
6872
{
6973
$this->handleFailuresWith = $callback;
7074
}
7175

72-
public function outOfBounds()
76+
public function outOfBounds(): bool
7377
{
7478
return $this->tooManyConsecutiveFailures() || $this->tooManyTotalFailures() || $this->beyondDeadline();
7579
}
7680

77-
public function tooManyConsecutiveFailures()
81+
public function tooManyConsecutiveFailures(): bool
7882
{
7983
return $this->consecutiveFailures > $this->consecutiveFailuresAllowed;
8084
}
8185

82-
public function tooManyTotalFailures()
86+
public function tooManyTotalFailures(): bool
8387
{
8488
return $this->totalFailures > $this->totalFailuresAllowed;
8589
}
8690

87-
public function beyondDeadline()
91+
public function beyondDeadline(): bool
8892
{
8993
return now()->timestamp > $this->deadline;
9094
}
9195

92-
protected function callHandler($exception)
96+
protected function callHandler(Throwable $exception): void
9397
{
9498
call_user_func($this->handleFailuresWith, $exception);
9599
}
96100

97-
protected function freshDeadline()
101+
protected function freshDeadline(): int
98102
{
99103
return now()->addSeconds($this->failuresAllowedForSeconds)->timestamp;
100104
}
101105

102-
protected function updateCachedStats($exception)
106+
protected function updateCachedStats(?Throwable $exception): void
103107
{
104-
$failed = !is_null($exception);
108+
$failed = $exception !== null;
105109

106110
$stats = $failed ? [
107111
// Reset if we passed, otherwise just store the incremented value.

0 commit comments

Comments
 (0)