Skip to content

Commit 0e7dede

Browse files
committed
Test stubs and examples with phpstan and phpcs
1 parent 406f57d commit 0e7dede

13 files changed

+353
-21
lines changed

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,47 @@ permissions:
1414
contents: read
1515

1616
jobs:
17+
lint:
18+
runs-on: ubuntu-latest
19+
name: "Lint | PHP ${{ matrix.php-version }}"
20+
strategy:
21+
matrix:
22+
php-version:
23+
- "8.1"
24+
- "8.2"
25+
- "8.3"
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Install PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
coverage: "none"
33+
php-version: "${{ matrix.php-version }}"
34+
tools: composer:v2
35+
extensions: json, opcache
36+
37+
- name: Validate composer.json and composer.lock
38+
run: composer validate --strict
39+
40+
- name: Cache Composer packages
41+
id: composer-cache
42+
uses: actions/cache@v4
43+
with:
44+
path: vendor
45+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
46+
restore-keys: |
47+
${{ runner.os }}-php-
48+
49+
- name: Install dependencies
50+
run: composer install --prefer-dist --no-progress --ignore-platform-req=php+ --ignore-platform-req=ext-perfidious
51+
52+
- name: phpcs
53+
run: php vendor/bin/phpcs
54+
55+
- name: phpstan
56+
run: php vendor/bin/phpstan analyze
57+
1758
test:
1859
runs-on: ubuntu-latest
1960
name: "Test | PHP ${{ matrix.php-version }}"

.phpcs.xml.dist

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
3+
<file>perfidious.stub.php</file>
4+
<file>examples</file>
5+
6+
<exclude-pattern>*/.direnv/*</exclude-pattern>
7+
<exclude-pattern>*/vendor/*</exclude-pattern>
8+
9+
<arg name="basepath" value="."/>
10+
<arg name="colors"/>
11+
<arg name="parallel" value="4"/>
12+
<arg name="encoding" value="utf-8"/>
13+
<arg name="tab-width" value="4"/>
14+
<arg name="extensions" value="php" />
15+
16+
<rule ref="PSR12">
17+
<exclude name="PSR12.Files.FileHeader" />
18+
<exclude name="PSR12.Files.OpenTag" />
19+
</rule>
20+
21+
<rule ref="Generic.Files.LineLength">
22+
<properties>
23+
<property name="lineLimit" value="140"/>
24+
</properties>
25+
</rule>
26+
27+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
28+
<exclude-pattern>*/examples/*</exclude-pattern>
29+
</rule>
30+
31+
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
32+
<exclude-pattern>*.stub.php</exclude-pattern>
33+
</rule>
34+
</ruleset>

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "jbboehr/perfidious",
3+
"description": "",
4+
"type": "library",
5+
"license": "AGPL-3.0-or-later",
6+
"authors": [
7+
{
8+
"name": "John Boehr",
9+
"email": "jbboehr@gmail.com"
10+
}
11+
],
12+
"require-dev": {
13+
"phpstan/phpstan": "^1.10",
14+
"squizlabs/php_codesniffer": "^3.9"
15+
},
16+
"suggest": {
17+
"ext-perfidious": "*"
18+
}
19+
}

composer.lock

Lines changed: 161 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/all-events.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
], $rest_index);
1414

1515
$show = $opts['show'] ?? 'present';
16+
$show = is_string($show) ? $show : 'present';
1617

1718
if (array_key_exists('help', $opts)) {
1819
fprintf(STDERR, "Usage: " . $argv[0] . PHP_EOL);

examples/estimate-overhead.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,27 @@
5151
$stddev = stddev($data);
5252
$min = min($data);
5353
$max = max($data);
54-
printf("%s\n samples=%d\n mean=%g\n variance=%g\n stddev=%g\n min=%g\n max=%g\n", $k, count($data), $mean, $variance, $stddev, $min, $max);
54+
printf(
55+
"%s\n samples=%d\n mean=%g\n variance=%g\n stddev=%g\n min=%g\n max=%g\n",
56+
$k,
57+
count($data),
58+
$mean,
59+
$variance,
60+
$stddev,
61+
$min,
62+
$max
63+
);
5564
}
5665

5766
/**
5867
* @see https://github.com/phpbench/phpbench/blob/master/lib/Math/Statistics.php
5968
* @license https://github.com/phpbench/phpbench/blob/master/LICENSE
6069
*/
6170

62-
function variance(array $values, bool $sample = false)
71+
/**
72+
* @param list<int|float> $values
73+
*/
74+
function variance(array $values, bool $sample = false): int|float
6375
{
6476
$average = mean($values);
6577
$sum = 0;
@@ -78,14 +90,20 @@ function variance(array $values, bool $sample = false)
7890
return $variance;
7991
}
8092

93+
/**
94+
* @param list<int|float> $values
95+
*/
8196
function stddev(array $values, bool $sample = false): float
8297
{
8398
$variance = variance($values, $sample);
8499

85100
return \sqrt($variance);
86101
}
87102

88-
function mean(array $values)
103+
/**
104+
* @param list<int|float> $values
105+
*/
106+
function mean(array $values): int|float
89107
{
90108
if (empty($values)) {
91109
return 0;

examples/sieve.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222

2323
$count = $opts['count'] ?? 2000000;
24+
$count = is_numeric($opts['count']) ? (int) $opts['count'] : 2000000;
2425

2526
$handle = open($pos_args);
2627
$handle->enable();
@@ -33,7 +34,10 @@
3334
var_dump($primes);
3435
var_dump($stats);
3536

36-
function sieve(int $n)
37+
/**
38+
* @return list<int>
39+
*/
40+
function sieve(int $n): array
3741
{
3842
$lut = array_fill(0, $n, null);
3943
for ($i = 2; $i < $n; $i++) {

examples/sieve2.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
}
2222

2323
$count = $opts['count'] ?? 2000000;
24+
$count = is_numeric($opts['count']) ? (int) $opts['count'] : 2000000;
2425

2526
$handle = open($pos_args);
2627
$handle->enable();
@@ -33,9 +34,12 @@
3334
var_dump($primes);
3435
var_dump($stats);
3536

37+
/**
38+
* @return list<int>
39+
*/
3640
function sieve(int $n): array
3741
{
38-
$n2 = ceil($n / 8);
42+
$n2 = (int) ceil($n / 8);
3943
$lut = str_repeat("\0", $n2);
4044
for ($i = 2; $i < $n; $i++) {
4145
for ($j = $i + $i; $j < $n; $j += $i) {

examples/three-sw-clock.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
var_dump($handle->readArray());
1010
sleep(1);
1111
}
12-

0 commit comments

Comments
 (0)