Skip to content

Commit 6b5e8e7

Browse files
Chore: adds phpstan support and scanning (#21)
1 parent e3c4e2f commit 6b5e8e7

9 files changed

Lines changed: 271 additions & 16 deletions

File tree

.github/workflows/phpstan.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: 'PHPStan Static Analysis'
2+
3+
on:
4+
push:
5+
branches: [ main, develop ]
6+
pull_request:
7+
branches: [ main, develop ]
8+
9+
jobs:
10+
phpstan:
11+
name: PHPStan
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 1
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: '7.2'
24+
coverage: none
25+
tools: composer:v2
26+
27+
- name: Get Composer Cache Directory
28+
id: composer-cache
29+
run: |
30+
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
31+
32+
- name: Cache Composer dependencies
33+
uses: actions/cache@v4
34+
with:
35+
path: ${{ steps.composer-cache.outputs.dir }}
36+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
37+
restore-keys: |
38+
${{ runner.os }}-composer-
39+
40+
- name: Install dependencies
41+
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction
42+
43+
- name: Run PHPStan
44+
run: composer phpstan

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"lucatume/di52": "^3.0",
2424
"lucatume/wp-browser": "^3.0.14",
2525
"codeception/module-phpbrowser": "^1.0.0",
26-
"codeception/module-asserts": "^1.0.0"
26+
"codeception/module-asserts": "^1.0.0",
27+
"phpstan/phpstan": "^1.12",
28+
"szepeviktor/phpstan-wordpress": "^1.3"
2729
},
2830
"autoload": {
2931
"psr-4": {
@@ -45,5 +47,9 @@
4547
"type": "git",
4648
"url": "https://github.com/wordpress/wordpress-develop.git"
4749
}
48-
]
50+
],
51+
"scripts": {
52+
"phpstan": "phpstan analyse --memory-limit=512M",
53+
"phpstan:baseline": "phpstan analyse --generate-baseline=phpstan-baseline.neon --memory-limit=512M"
54+
}
4955
}

composer.lock

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

phpstan.neon

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
includes:
2+
- vendor/szepeviktor/phpstan-wordpress/extension.neon
3+
4+
parameters:
5+
# Level 5 is a good balance between strictness and practicality
6+
level: 5
7+
8+
# Paths to analyze
9+
paths:
10+
- src
11+
12+
# Exclude paths
13+
excludePaths:
14+
- vendor/*
15+
- _wordpress/*
16+
- _plugins/*
17+
18+
# Ignore specific errors if needed
19+
ignoreErrors:
20+
# Add specific error patterns to ignore here if needed
21+
22+
# PHP version (must be an integer)
23+
phpVersion: 70200
24+
25+
# Treat missing return types as mixed
26+
treatPhpDocTypesAsCertain: false

src/Config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static function setHookPrefix(string $prefix)
8282
/**
8383
* @since 1.0.0
8484
*
85+
* @return never
8586
* @throws ValidationExceptionInterface
8687
*/
8788
public static function throwValidationException()
@@ -118,6 +119,7 @@ public static function setValidationExceptionClass(string $validationExceptionCl
118119
/**
119120
* @since 1.0.0
120121
*
122+
* @return never
121123
* @throws InvalidArgumentException
122124
*/
123125
public static function throwInvalidArgumentException()
@@ -162,7 +164,7 @@ public static function initialize()
162164
return;
163165
}
164166

165-
if (empty(self::$container)) {
167+
if (self::$container === null) {
166168
throw new RuntimeException('A service container must be set before initializing the library');
167169
}
168170

src/Exceptions/Contracts/ValidationExceptionInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace StellarWP\Validation\Exceptions\Contracts;
66

7-
interface ValidationExceptionInterface
7+
use Throwable;
8+
9+
interface ValidationExceptionInterface extends Throwable
810
{
911

1012
}

src/Rules/Abstracts/ConditionalRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static function fromString(?string $options = null): ValidationRule
5858
$conditionSet->and($rule[0], '=', $rule[1]);
5959
}
6060

61+
// @phpstan-ignore-next-line
6162
return new static($conditionSet);
6263
}
6364

src/Rules/DateTime.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static function id(): string
3535
*/
3636
public static function fromString(?string $options = null): ValidationRule
3737
{
38+
// @phpstan-ignore-next-line
3839
return new static($options);
3940
}
4041

0 commit comments

Comments
 (0)