Skip to content

Commit 754ef85

Browse files
committed
Reduce number of PHPStan errors
1 parent 8da6092 commit 754ef85

16 files changed

+133
-539
lines changed

phpstan-baseline.neon

Lines changed: 3 additions & 405 deletions
Large diffs are not rendered by default.

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
*/

src/Support/Facades/PostalCodes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @method static bool fails(string $countryCode, string ...$postalCodes)
1111
* @method static bool passes(string $countryCode, string ...$postalCodes)
12-
* @method static void override(array|string $countryCode, string|null $pattern = null)
12+
* @method static void override(array<string, string>|string $countryCode, string|null $pattern = null)
1313
* @method static bool supports(string $countryCode)
1414
* @see \Axlon\PostalCodeValidation\PostalCodeValidator
1515
*/

src/Support/PostalCodeExamples.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class PostalCodeExamples
99
/**
1010
* The postal code examples.
1111
*
12-
* @var array|null
12+
* @var array<string, string>|null
1313
*/
1414
protected $examples;
1515

src/ValidationServiceProvider.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,14 @@ private static function registerRules(Factory $validator): void
3939
$validator->replacer('postal_code_for', 'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@replace');
4040
$validator->replacer('postal_code_with', 'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@replace');
4141

42-
if (method_exists($validator, 'extendDependent')) {
43-
$validator->extendDependent(
44-
'postal_code_for',
45-
'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate',
46-
);
47-
48-
$validator->extendDependent(
49-
'postal_code_with',
50-
'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate',
51-
);
52-
} else {
53-
$validator->extend('postal_code_for', 'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate');
54-
$validator->extend('postal_code_with', 'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate');
55-
}
42+
$validator->extendDependent(
43+
'postal_code_for',
44+
'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate',
45+
);
46+
47+
$validator->extendDependent(
48+
'postal_code_with',
49+
'Axlon\PostalCodeValidation\Extensions\PostalCodeFor@validate',
50+
);
5651
}
5752
}

tests/Integration/FacadeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ final class FacadeTest extends TestCase
1515
*/
1616
public function testFacadesProxiesPatternMatcher(): void
1717
{
18-
$this->assertSame($this->app->make('postal_codes'), PostalCodes::getFacadeRoot());
18+
self::assertSame($this->app?->make('postal_codes'), PostalCodes::getFacadeRoot());
1919
}
2020
}

tests/Integration/PostalCodeForTest.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Axlon\PostalCodeValidation\Tests\Integration;
66

7+
use Illuminate\Support\Facades\Validator;
78
use InvalidArgumentException;
89

910
final class PostalCodeForTest extends TestCase
@@ -15,13 +16,13 @@ final class PostalCodeForTest extends TestCase
1516
*/
1617
public function testValidationFailsInvalidCountry(): void
1718
{
18-
$validator = $this->app->make('validator')->make(
19+
$validator = Validator::make(
1920
['postal_code' => '1234 AB', 'country' => 'not-a-country'],
2021
['postal_code' => 'postal_code_for:country'],
2122
);
2223

23-
$this->assertFalse($validator->passes());
24-
$this->assertContains('validation.postal_code_for', $validator->errors()->all());
24+
self::assertFalse($validator->passes());
25+
self::assertContains('validation.postal_code_for', $validator->errors()->all());
2526
}
2627

2728
/**
@@ -31,13 +32,13 @@ public function testValidationFailsInvalidCountry(): void
3132
*/
3233
public function testValidationFailsInvalidPostalCode(): void
3334
{
34-
$validator = $this->app->make('validator')->make(
35+
$validator = Validator::make(
3536
['postal_code' => 'not-a-postal-code', 'country' => 'NL'],
3637
['postal_code' => 'postal_code_for:country'],
3738
);
3839

39-
$this->assertFalse($validator->passes());
40-
$this->assertContains('validation.postal_code_for', $validator->errors()->all());
40+
self::assertFalse($validator->passes());
41+
self::assertContains('validation.postal_code_for', $validator->errors()->all());
4142
}
4243

4344
/**
@@ -48,24 +49,24 @@ public function testValidationFailsInvalidPostalCode(): void
4849
*/
4950
public function testValidationFailsNullPostalCode(): void
5051
{
51-
$validator = $this->app->make('validator')->make(
52+
$validator = Validator::make(
5253
['postal_code' => null, 'country' => 'DE'],
5354
['postal_code' => 'postal_code_for:country'],
5455
);
5556

56-
$this->assertFalse($validator->passes());
57-
$this->assertContains('validation.postal_code_for', $validator->errors()->all());
57+
self::assertFalse($validator->passes());
58+
self::assertContains('validation.postal_code_for', $validator->errors()->all());
5859
}
5960

6061
public function testValidationPassesIfAllFieldsAreMissing(): void
6162
{
62-
$validator = $this->app->make('validator')->make(
63+
$validator = Validator::make(
6364
['postal_code' => '1234 AB'],
6465
['postal_code' => 'postal_code_for:country'],
6566
);
6667

67-
$this->assertTrue($validator->passes());
68-
$this->assertEmpty($validator->errors()->all());
68+
self::assertTrue($validator->passes());
69+
self::assertEmpty($validator->errors()->all());
6970
}
7071

7172
/**
@@ -75,13 +76,13 @@ public function testValidationPassesIfAllFieldsAreMissing(): void
7576
*/
7677
public function testValidationIgnoresMissingFields(): void
7778
{
78-
$validator = $this->app->make('validator')->make(
79+
$validator = Validator::make(
7980
['postal_code' => '1234 AB', 'empty' => '', 'null' => null, 'country' => 'NL'],
8081
['postal_code' => 'postal_code_for:empty,missing,null,country'],
8182
);
8283

83-
$this->assertTrue($validator->passes());
84-
$this->assertEmpty($validator->errors()->all());
84+
self::assertTrue($validator->passes());
85+
self::assertEmpty($validator->errors()->all());
8586
}
8687

8788
/**
@@ -91,13 +92,13 @@ public function testValidationIgnoresMissingFields(): void
9192
*/
9293
public function testValidationPassesValidPostalCode(): void
9394
{
94-
$validator = $this->app->make('validator')->make(
95+
$validator = Validator::make(
9596
['postal_code' => '1234 AB', 'country' => 'NL'],
9697
['postal_code' => 'postal_code_for:country'],
9798
);
9899

99-
$this->assertTrue($validator->passes());
100-
$this->assertEmpty($validator->errors()->all());
100+
self::assertTrue($validator->passes());
101+
self::assertEmpty($validator->errors()->all());
101102
}
102103

103104
/**
@@ -107,7 +108,7 @@ public function testValidationPassesValidPostalCode(): void
107108
*/
108109
public function testValidationThrowsWithoutParameters(): void
109110
{
110-
$validator = $this->app->make('validator')->make(
111+
$validator = Validator::make(
111112
['postal_code' => '1234 AB'],
112113
['postal_code' => 'postal_code_for'],
113114
);

0 commit comments

Comments
 (0)