Skip to content

Commit 20f2742

Browse files
committed
tweak: improve typing
1 parent e4d15cf commit 20f2742

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
name: CI
22

3-
on: [push]
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
actions: read
410

511
jobs:
612
composer:
713
runs-on: ubuntu-latest
814
strategy:
915
matrix:
10-
php: [ 8.1, 8.2, 8.3, 8.4 ]
16+
php: [ 8.2, 8.3, 8.4, 8.5 ]
1117

1218
steps:
13-
- uses: actions/checkout@v4
19+
- uses: actions/checkout@v6
1420

1521
- name: Cache Composer dependencies
16-
uses: actions/cache@v4
22+
uses: actions/cache@v5
1723
with:
1824
path: /tmp/composer-cache
1925
key: ${{ runner.os }}-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}
@@ -22,22 +28,24 @@ jobs:
2228
uses: php-actions/composer@v6
2329
with:
2430
php_version: ${{ matrix.php }}
31+
php_extensions: pcntl
2532

2633
- name: Archive build
27-
run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./
34+
run: mkdir /tmp/github-actions/ && tar --exclude=".git" -cvf /tmp/github-actions/build.tar ./
2835

2936
- name: Upload build archive for test runners
3037
uses: actions/upload-artifact@v4
3138
with:
3239
name: build-artifact-${{ matrix.php }}
3340
path: /tmp/github-actions
41+
retention-days: 1
3442

3543
phpunit:
3644
runs-on: ubuntu-latest
3745
needs: [ composer ]
3846
strategy:
3947
matrix:
40-
php: [ 8.1, 8.2, 8.3, 8.4 ]
48+
php: [ 8.2, 8.3, 8.4, 8.5 ]
4149

4250
outputs:
4351
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
@@ -72,7 +80,7 @@ jobs:
7280
needs: [ phpunit ]
7381
strategy:
7482
matrix:
75-
php: [ 8.1, 8.2, 8.3, 8.4 ]
83+
php: [ 8.2, 8.3, 8.4, 8.5 ]
7684

7785
steps:
7886
- uses: actions/checkout@v4
@@ -87,13 +95,15 @@ jobs:
8795

8896
- name: Upload to Codecov
8997
uses: codecov/codecov-action@v5
98+
with:
99+
token: ${{ secrets.CODECOV_TOKEN }}
90100

91101
phpstan:
92102
runs-on: ubuntu-latest
93103
needs: [ composer ]
94104
strategy:
95105
matrix:
96-
php: [ 8.1, 8.2, 8.3, 8.4 ]
106+
php: [ 8.2, 8.3, 8.4, 8.5 ]
97107

98108
steps:
99109
- uses: actions/download-artifact@v4
@@ -109,13 +119,15 @@ jobs:
109119
with:
110120
php_version: ${{ matrix.php }}
111121
path: src/
122+
level: 6
123+
memory_limit: 256M
112124

113125
phpmd:
114126
runs-on: ubuntu-latest
115127
needs: [ composer ]
116128
strategy:
117129
matrix:
118-
php: [ 8.1, 8.2, 8.3, 8.4 ]
130+
php: [ 8.2, 8.3, 8.4, 8.5 ]
119131

120132
steps:
121133
- uses: actions/download-artifact@v4
@@ -139,7 +151,7 @@ jobs:
139151
needs: [ composer ]
140152
strategy:
141153
matrix:
142-
php: [ 8.1, 8.2, 8.3, 8.4 ]
154+
php: [ 8.2, 8.3, 8.4, 8.5 ]
143155

144156
steps:
145157
- uses: actions/download-artifact@v4
@@ -156,18 +168,3 @@ jobs:
156168
php_version: ${{ matrix.php }}
157169
path: src/
158170
standard: phpcs.xml
159-
160-
remove_old_artifacts:
161-
runs-on: ubuntu-latest
162-
163-
steps:
164-
- name: Remove old artifacts for prior workflow runs on this repository
165-
env:
166-
GH_TOKEN: ${{ github.token }}
167-
run: |
168-
gh api "/repos/${{ github.repository }}/actions/artifacts?name=build-artifact" | jq ".artifacts[] | select(.name | startswith(\"build-artifact\")) | .id" > artifact-id-list.txt
169-
while read id
170-
do
171-
echo -n "Deleting artifact ID $id ... "
172-
gh api --method DELETE /repos/${{ github.repository }}/actions/artifacts/$id && echo "Done"
173-
done <artifact-id-list.txt

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
"squizlabs/php_codesniffer": "^3.7 || ^4.0"
1616
},
1717

18+
"scripts": {
19+
"phpunit": "vendor/bin/phpunit --configuration phpunit.xml",
20+
"phpunit:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --configuration phpunit.xml --coverage-text",
21+
"phpstan": "vendor/bin/phpstan analyse --level 6 src --memory-limit 256M",
22+
"phpcs": "vendor/bin/phpcs src --standard=phpcs.xml",
23+
"phpmd": "vendor/bin/phpmd src/ text phpmd.xml",
24+
"test": [
25+
"@phpunit",
26+
"@phpstan",
27+
"@phpcs",
28+
"@phpmd"
29+
]
30+
},
31+
1832
"license": "MIT",
1933
"authors": [
2034
{

example/02-class.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require __DIR__ . "/../vendor/autoload.php";
3+
4+
use Gt\FileCache\Cache;
5+
6+
$fileCache = new Cache("/tmp/phpgt-filecache-datetime");
7+
8+
$dateTime = $fileCache->getInstance("current-date", DateTime::class, static function(): DateTime {
9+
return new DateTime();
10+
});
11+
12+
echo $dateTime->format(DateTimeInterface::ATOM) . PHP_EOL;

src/Cache.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function getDateTime(string $name, callable $callback):DateTimeInterface
6464
return new DateTimeImmutable($value);
6565
}
6666

67+
/** @return array<mixed> */
6768
public function getArray(string $name, callable $callback):array {
6869
$value = $this->get($name, $callback);
6970
if(!is_array($value)) {
@@ -74,16 +75,17 @@ public function getArray(string $name, callable $callback):array {
7475
}
7576

7677
/**
77-
* @template T
78-
* @param class-string<T> $className
79-
* @return array<T>
78+
* @template TObject of object
79+
* @param class-string<TObject>|"int"|"integer"|"float"|"double"|"string"|"bool"|"boolean" $className
80+
* @phpstan-return ($className is class-string<TObject> ? array<TObject> : ($className is "int"|"integer" ? array<int> : ($className is "float"|"double" ? array<float> : ($className is "string" ? array<string> : array<bool>))))
8081
*/
8182
public function getTypedArray(string $name, string $className, callable $callback):array {
8283
$array = $this->get($name, $callback);
8384
if(!is_array($array)) {
8485
throw new TypeError("Data '$name' is not an array");
8586
}
8687

88+
/** @var array<int|string, mixed> $array */
8789
foreach($array as $key => $value) {
8890
$array[$key] = $this->validateAndConvertValue($value, $className, $key);
8991
}
@@ -92,11 +94,11 @@ public function getTypedArray(string $name, string $className, callable $callbac
9294
}
9395

9496
/**
95-
* @template T
97+
* @template TObject of object
9698
* @param mixed $value
97-
* @param class-string<T> $className
99+
* @param class-string<TObject>|"int"|"integer"|"float"|"double"|"string"|"bool"|"boolean" $className
98100
* @param string|int $key
99-
* @return T
101+
* @phpstan-return ($className is class-string<TObject> ? TObject : ($className is "int"|"integer" ? int : ($className is "float"|"double" ? float : ($className is "string" ? string : bool))))
100102
*/
101103
private function validateAndConvertValue(mixed $value, string $className, string|int $key): mixed {
102104
return match(strtolower($className)) {
@@ -159,11 +161,11 @@ private function convertToBool(mixed $value): bool {
159161
}
160162

161163
/**
162-
* @template T
164+
* @template TObject of object
163165
* @param mixed $value
164-
* @param class-string<T> $className
166+
* @param class-string<TObject> $className
165167
* @param string|int $key
166-
* @return T
168+
* @return TObject
167169
*/
168170
private function validateInstance(mixed $value, string $className, string|int $key): object {
169171
if($value instanceof $className) {
@@ -174,16 +176,17 @@ private function validateInstance(mixed $value, string $className, string|int $k
174176
}
175177

176178
/**
177-
* @template T
179+
* @template T of object
178180
* @param class-string<T> $className
179181
* @return T
180182
*/
181183
public function getInstance(string $name, string $className, callable $callback):object {
182184
$value = $this->get($name, $callback);
183-
if(get_class($value) !== $className) {
185+
if(!$value instanceof $className) {
184186
throw new TypeError("Value is not an instance of $className");
185187
}
186188

189+
/** @var T $value */
187190
return $value;
188191
}
189192
}

0 commit comments

Comments
 (0)