Skip to content

Commit 337cb90

Browse files
committed
Set up PHPStan
1 parent 9e594c6 commit 337cb90

21 files changed

+211
-135
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ tests/ export-ignore
77
.gitattributes export-ignore
88
.gitignore export-ignore
99
phpcs.xml.dist export-ignore
10+
phpstan.neon.dist export-ignore
1011
phpunit.xml.dist export-ignore

.github/workflows/phpstan.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: PHPStan
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
phpstan:
9+
runs-on: ubuntu-latest
10+
name: PHPStan
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: 8.4
18+
- name: Install Composer dependencies
19+
run: composer install --no-interaction --no-progress --no-scripts
20+
- name: Run PHPStan
21+
run: ./vendor/bin/phpstan --no-progress

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
vendor/
44
composer.lock
55
phpcs.xml
6+
phpstan.neon
67
phpunit.xml

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
"require-dev": {
2525
"guzzlehttp/guzzle": "^7.8",
2626
"orchestra/testbench": "^8.34 || ^9.12 || ^10.1",
27+
"phpstan/extension-installer": "^1.4",
28+
"phpstan/phpstan": "^2.1",
29+
"phpstan/phpstan-deprecation-rules": "^2.0",
30+
"phpstan/phpstan-strict-rules": "^2.0",
2731
"phpunit/phpunit": "^10.5 || ^11.5",
2832
"slevomat/coding-standard": "^8.16",
2933
"symfony/var-exporter": "^6.4 || ^7.0"
@@ -40,7 +44,8 @@
4044
},
4145
"config": {
4246
"allow-plugins": {
43-
"dealerdirect/phpcodesniffer-composer-installer": true
47+
"dealerdirect/phpcodesniffer-composer-installer": true,
48+
"phpstan/extension-installer": true
4449
},
4550
"sort-packages": true
4651
},

phpstan-baseline.neon

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Parameter \#1 \$countryCode of method Axlon\\PostalCodeValidation\\PostalCodeValidator\:\:passes\(\) expects string, mixed given\.$#'
5+
identifier: argument.type
6+
count: 1
7+
path: src/Extensions/PostalCodeFor.php
8+
9+
-
10+
message: '#^Parameter \#1 \$countryCode of method Axlon\\PostalCodeValidation\\Support\\PostalCodeExamples\:\:get\(\) expects string, mixed given\.$#'
11+
identifier: argument.type
12+
count: 1
13+
path: src/Extensions/PostalCodeFor.php
14+
15+
-
16+
message: '#^Cannot access offset string on mixed\.$#'
17+
identifier: offsetAccess.nonOffsetAccessible
18+
count: 1
19+
path: src/Support/PostalCodeExamples.php
20+
21+
-
22+
message: '#^Method Axlon\\PostalCodeValidation\\Support\\PostalCodeExamples\:\:get\(\) should return string\|null but returns mixed\.$#'
23+
identifier: return.type
24+
count: 1
25+
path: src/Support/PostalCodeExamples.php
26+
27+
-
28+
message: '#^Property Axlon\\PostalCodeValidation\\Support\\PostalCodeExamples\:\:\$examples \(array\<string, string\>\|null\) does not accept mixed\.$#'
29+
identifier: assign.propertyType
30+
count: 1
31+
path: src/Support/PostalCodeExamples.php
32+
33+
-
34+
message: '#^Parameter \#1 \$patterns of class Axlon\\PostalCodeValidation\\PostalCodeValidator constructor expects array\<string, string\|null\>, mixed given\.$#'
35+
identifier: argument.type
36+
count: 1
37+
path: src/ValidationServiceProvider.php
38+
39+
-
40+
message: '#^Parameter \#1 \$patterns of class Axlon\\PostalCodeValidation\\PostalCodeValidator constructor expects array\<string, string\|null\>, mixed given\.$#'
41+
identifier: argument.type
42+
count: 1
43+
path: tests/Unit/PostalCodeValidatorTest.php

phpstan.neon.dist

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
- phpstan-baseline.neon
4+
5+
parameters:
6+
level: 10
7+
paths:
8+
- src
9+
- tests

src/Extensions/PostalCode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function replace(string $message, string $attribute, string $rule, array
7979
*/
8080
public function validate(string $attribute, ?string $value, array $parameters): bool
8181
{
82-
if (empty($parameters)) {
82+
if ($parameters === []) {
8383
throw new InvalidArgumentException('Validation rule postal_code requires at least 1 parameter.');
8484
}
8585

src/Extensions/PostalCodeFor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function replace(
9292
*/
9393
public function validate(string $attribute, ?string $value, array $parameters, Validator $validator): bool
9494
{
95-
if (empty($parameters)) {
95+
if ($parameters === []) {
9696
throw new InvalidArgumentException('Validation rule postal_code_with requires at least 1 parameter.');
9797
}
9898

src/PostalCodeValidator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ final class PostalCodeValidator
1616
/**
1717
* The matching patterns.
1818
*
19-
* @var array
19+
* @var array<string, string|null>
2020
*/
2121
protected $patterns;
2222

2323
/**
2424
* The matching pattern overrides.
2525
*
26-
* @var array
26+
* @var array<string, string>
2727
*/
2828
protected $patternOverrides;
2929

3030
/**
3131
* Create a new postal code matcher.
3232
*
33-
* @param array $patterns
33+
* @param array<string, string|null> $patterns
3434
* @return void
3535
*/
3636
public function __construct(array $patterns)
@@ -54,18 +54,18 @@ public function fails(string $countryCode, ?string ...$postalCodes): bool
5454
/**
5555
* Override pattern matching for the given country.
5656
*
57-
* @param array|string $countryCode
57+
* @param array<string, string>|string $countryCode
5858
* @param string|null $pattern
5959
* @return void
6060
*/
61-
public function override($countryCode, ?string $pattern = null): void
61+
public function override(array|string $countryCode, ?string $pattern = null): void
6262
{
6363
if (is_array($countryCode)) {
6464
$this->patternOverrides = array_merge(
6565
$this->patternOverrides,
6666
array_change_key_case($countryCode, CASE_UPPER),
6767
);
68-
} else {
68+
} elseif (is_string($pattern)) {
6969
$this->patternOverrides[strtoupper($countryCode)] = $pattern;
7070
}
7171
}

src/Rules/PostalCode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ final class PostalCode
1616
/**
1717
* The rule parameters.
1818
*
19-
* @var string[]
19+
* @var array<string>
2020
*/
2121
protected $parameters;
2222

2323
/**
2424
* Create a new postal code validation rule.
2525
*
26-
* @param array $parameters
26+
* @param array<string> $parameters
2727
* @param bool $dependant
2828
* @return void
2929
*/

0 commit comments

Comments
 (0)