Skip to content

Commit

Permalink
Test stubs and examples with phpstan and phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jbboehr committed Apr 6, 2024
1 parent 406f57d commit 0e7dede
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 21 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,47 @@ permissions:
contents: read

jobs:
lint:
runs-on: ubuntu-latest
name: "Lint | PHP ${{ matrix.php-version }}"
strategy:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.3"
steps:
- uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: composer:v2
extensions: json, opcache

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress --ignore-platform-req=php+ --ignore-platform-req=ext-perfidious

- name: phpcs
run: php vendor/bin/phpcs

- name: phpstan
run: php vendor/bin/phpstan analyze

test:
runs-on: ubuntu-latest
name: "Test | PHP ${{ matrix.php-version }}"
Expand Down
34 changes: 34 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<file>perfidious.stub.php</file>
<file>examples</file>

<exclude-pattern>*/.direnv/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<arg name="basepath" value="."/>
<arg name="colors"/>
<arg name="parallel" value="4"/>
<arg name="encoding" value="utf-8"/>
<arg name="tab-width" value="4"/>
<arg name="extensions" value="php" />

<rule ref="PSR12">
<exclude name="PSR12.Files.FileHeader" />
<exclude name="PSR12.Files.OpenTag" />
</rule>

<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="140"/>
</properties>
</rule>

<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>*/examples/*</exclude-pattern>
</rule>

<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>*.stub.php</exclude-pattern>
</rule>
</ruleset>
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "jbboehr/perfidious",
"description": "",
"type": "library",
"license": "AGPL-3.0-or-later",
"authors": [
{
"name": "John Boehr",
"email": "jbboehr@gmail.com"
}
],
"require-dev": {
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^3.9"
},
"suggest": {
"ext-perfidious": "*"
}
}
161 changes: 161 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/all-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
], $rest_index);

$show = $opts['show'] ?? 'present';
$show = is_string($show) ? $show : 'present';

if (array_key_exists('help', $opts)) {
fprintf(STDERR, "Usage: " . $argv[0] . PHP_EOL);
Expand Down
24 changes: 21 additions & 3 deletions examples/estimate-overhead.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,27 @@
$stddev = stddev($data);
$min = min($data);
$max = max($data);
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);
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
);
}

/**
* @see https://github.com/phpbench/phpbench/blob/master/lib/Math/Statistics.php
* @license https://github.com/phpbench/phpbench/blob/master/LICENSE
*/

function variance(array $values, bool $sample = false)
/**
* @param list<int|float> $values
*/
function variance(array $values, bool $sample = false): int|float
{
$average = mean($values);
$sum = 0;
Expand All @@ -78,14 +90,20 @@ function variance(array $values, bool $sample = false)
return $variance;
}

/**
* @param list<int|float> $values
*/
function stddev(array $values, bool $sample = false): float
{
$variance = variance($values, $sample);

return \sqrt($variance);
}

function mean(array $values)
/**
* @param list<int|float> $values
*/
function mean(array $values): int|float
{
if (empty($values)) {
return 0;
Expand Down
6 changes: 5 additions & 1 deletion examples/sieve.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}

$count = $opts['count'] ?? 2000000;
$count = is_numeric($opts['count']) ? (int) $opts['count'] : 2000000;

$handle = open($pos_args);
$handle->enable();
Expand All @@ -33,7 +34,10 @@
var_dump($primes);
var_dump($stats);

function sieve(int $n)
/**
* @return list<int>
*/
function sieve(int $n): array
{
$lut = array_fill(0, $n, null);
for ($i = 2; $i < $n; $i++) {
Expand Down
6 changes: 5 additions & 1 deletion examples/sieve2.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
}

$count = $opts['count'] ?? 2000000;
$count = is_numeric($opts['count']) ? (int) $opts['count'] : 2000000;

$handle = open($pos_args);
$handle->enable();
Expand All @@ -33,9 +34,12 @@
var_dump($primes);
var_dump($stats);

/**
* @return list<int>
*/
function sieve(int $n): array
{
$n2 = ceil($n / 8);
$n2 = (int) ceil($n / 8);
$lut = str_repeat("\0", $n2);
for ($i = 2; $i < $n; $i++) {
for ($j = $i + $i; $j < $n; $j += $i) {
Expand Down
1 change: 0 additions & 1 deletion examples/three-sw-clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
var_dump($handle->readArray());
sleep(1);
}

Loading

0 comments on commit 0e7dede

Please sign in to comment.