Skip to content

Commit 535c0fd

Browse files
Adds support for PHPStan (#24)
1 parent 5011f49 commit 535c0fd

23 files changed

+319
-101
lines changed

.github/workflows/phpstan.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: 'PHPStan'
2+
on:
3+
push:
4+
jobs:
5+
phpstan:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- name: Checkout the repository
9+
uses: actions/checkout@v4
10+
with:
11+
fetch-depth: 1
12+
# ------------------------------------------------------------------------------
13+
# Prepare our composer cache directory
14+
# ------------------------------------------------------------------------------
15+
- name: Get Composer Cache Directory
16+
id: get-composer-cache-dir
17+
run: |
18+
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
19+
- uses: actions/cache@v4
20+
id: composer-cache
21+
with:
22+
path: ${{ steps.get-composer-cache-dir.outputs.dir }}
23+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
24+
restore-keys: |
25+
${{ runner.os }}-composer-
26+
# ------------------------------------------------------------------------------
27+
# Install dependencies
28+
# ------------------------------------------------------------------------------
29+
- name: Install dependencies
30+
run: composer install --ignore-platform-reqs --no-interaction
31+
# ------------------------------------------------------------------------------
32+
# Run PHPStan
33+
# ------------------------------------------------------------------------------
34+
- name: Run PHPStan
35+
run: composer phpstan

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"codeception/module-webdriver": "^1.0",
3434
"codeception/util-universalframework": "^1.0",
3535
"lucatume/wp-browser": "^3.0.14",
36-
"phpunit/phpunit": "~6.0"
36+
"phpunit/phpunit": "~6.0",
37+
"phpstan/phpstan": "^2.1"
3738
},
3839
"autoload": {
3940
"psr-4": {
@@ -44,5 +45,8 @@
4445
"psr-4": {
4546
"StellarWP\\Models\\Tests\\": "tests/_support/Helper/"
4647
}
48+
},
49+
"scripts": {
50+
"phpstan": "phpstan analyze --memory-limit=512M"
4751
}
4852
}

composer.lock

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

phpstan.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- src
5+
excludePaths:
6+
- tests
7+
treatPhpDocTypesAsCertain: false

src/Models/Config.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace StellarWP\Models;
44

55
use InvalidArgumentException;
6+
use Throwable;
67

78
class Config {
89
/**
@@ -11,7 +12,7 @@ class Config {
1112
protected static $hookPrefix;
1213

1314
/**
14-
* @var string
15+
* @var class-string<Throwable>
1516
*/
1617
protected static $invalidArgumentException = InvalidArgumentException::class;
1718

@@ -40,7 +41,7 @@ public static function getHookPrefix(): string {
4041
*
4142
* @since 1.0.0
4243
*
43-
* @return string
44+
* @return class-string<Throwable>
4445
*/
4546
public static function getInvalidArgumentException(): string {
4647
return static::$invalidArgumentException;
@@ -111,9 +112,12 @@ public static function setInvalidArgumentException( string $class ) {
111112
*
112113
* @param string $message
113114
*
114-
* @return void
115+
* @return never
116+
* @throws Throwable
115117
*/
116118
public static function throwInvalidArgumentException( string $message ): void {
117-
throw new static::$invalidArgumentException( $message );
119+
/** @var class-string<Throwable> $exceptionClass */
120+
$exceptionClass = static::$invalidArgumentException;
121+
throw new $exceptionClass( $message );
118122
}
119123
}

src/Models/Contracts/Arrayable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface Arrayable {
1111
*
1212
* @since 1.0.0
1313
*
14-
* @return array
14+
* @return array<string,mixed>
1515
*/
1616
public function toArray() : array;
1717
}

src/Models/Contracts/Model.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getAttribute( string $key );
4444
*
4545
* @since 1.0.0
4646
*
47-
* @return array
47+
* @return array<string,mixed>
4848
*/
4949
public function getDirty() : array;
5050

@@ -131,7 +131,7 @@ public static function isPropertyTypeValid( string $key, $value ) : bool;
131131
* @since 2.0.0 changed to static
132132
* @since 1.0.0
133133
*
134-
* @return int[]|string[]
134+
* @return list<string>
135135
*/
136136
public static function propertyKeys() : array;
137137

src/Models/Contracts/ModelBuildsFromData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ModelBuildsFromData {
1111
/**
1212
* @since 2.0.0
1313
*
14-
* @param array|object $data
14+
* @param array<string,mixed>|object $data
1515
*
1616
* @return Model
1717
*/

src/Models/Contracts/ModelCrud.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @since 1.0.0
99
*/
10-
interface ModelCrud {
10+
interface ModelCrud extends ModelBuildsFromData {
1111
/**
1212
* @since 1.0.0
1313
*
@@ -20,7 +20,7 @@ public static function find( $id );
2020
/**
2121
* @since 1.0.0
2222
*
23-
* @param array $attributes
23+
* @param array<string,mixed> $attributes
2424
*
2525
* @return Model
2626
*/
@@ -43,7 +43,7 @@ public function delete() : bool;
4343
/**
4444
* @since 1.0.0
4545
*
46-
* @return ModelQueryBuilder
46+
* @return ModelQueryBuilder<static>
4747
*/
4848
public static function query();
4949
}

src/Models/Contracts/ModelHasFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
namespace StellarWP\Models\Contracts;
44

55
use StellarWP\Models\ModelFactory;
6+
use StellarWP\Models\Contracts\ModelCrud;
67

78
/**
89
* @since 1.0.0
910
*/
10-
interface ModelHasFactory {
11+
interface ModelHasFactory extends ModelCrud {
1112
/**
1213
* @since 1.0.0
1314
*
14-
* @return ModelFactory
15+
* @return ModelFactory<static>
1516
*/
1617
public static function factory();
1718
}

0 commit comments

Comments
 (0)