Skip to content

Commit 6c72fc0

Browse files
authored
Using PHPStan 2 (#20)
1 parent b743d05 commit 6c72fc0

File tree

8 files changed

+30
-21
lines changed

8 files changed

+30
-21
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
"require-dev": {
4545
"friendsofphp/php-cs-fixer": "^3.65.0",
4646
"httpwg/structured-field-tests": "*@dev",
47-
"phpstan/phpstan": "^1.12.12",
48-
"phpstan/phpstan-strict-rules": "^1.6.1",
49-
"phpstan/phpstan-phpunit": "^1.4.1",
50-
"phpstan/phpstan-deprecation-rules": "^1.2.1",
47+
"phpstan/phpstan": "^2.0.3",
48+
"phpstan/phpstan-strict-rules": "^2.0",
49+
"phpstan/phpstan-phpunit": "^2.0.1",
50+
"phpstan/phpstan-deprecation-rules": "^2.0.1",
5151
"phpunit/phpunit": "^10.5.38 || ^11.5.0",
5252
"symfony/var-dumper": "^6.4.15 || ^v7.2.0",
5353
"bakame/aide-base32": "dev-main",

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ includes:
44
- vendor/phpstan/phpstan-phpunit/rules.neon
55
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
66
parameters:
7-
level: max
7+
level: 9
88
paths:
99
- src
1010
- tests

src/Dictionary.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
152152
}
153153

154154
if (!in_array(count($pair), [1, 2], true)) {
155-
throw new SyntaxError('The pair first member is the item value; its second member is the item parameters.');
155+
throw new SyntaxError('The pair first member represents its value; the second member is its associated parameters.');
156156
}
157157

158158
return is_iterable($pair[0]) ? InnerList::fromPair($pair) : Item::fromPair($pair);
@@ -527,10 +527,13 @@ public function add(
527527
*/
528528
private function newInstance(array $members): self
529529
{
530-
return match (true) {
531-
$members == $this->members => $this,
532-
default => new self($members),
533-
};
530+
foreach ($members as $offset => $member) {
531+
if (!isset($this->members[$offset]) || !$this->members[$offset]->equals($member)) {
532+
return new self($members);
533+
}
534+
}
535+
536+
return $this;
534537
}
535538

536539
/**
@@ -707,7 +710,7 @@ public function replace(int $index, array $pair): self
707710
$pairs = iterator_to_array($this->getIterator());
708711

709712
return match (true) {
710-
$pairs[$offset] == $pair => $this,
713+
$pairs[$offset][0] === $pair[0] && $pairs[$offset][1]->equals($pair[1]) => $this,
711714
default => self::fromPairs(array_replace($pairs, [$offset => $pair])),
712715
};
713716
}

src/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public function withValue(DateTimeInterface|Bytes|Token|DisplayString|string|int
459459
$this->value instanceof Bytes,
460460
$this->value instanceof Token,
461461
$this->value instanceof DisplayString => $this->value->equals($value),
462-
$this->value instanceof DateTimeInterface && $value instanceof DateTimeInterface => $value == $this->value,
462+
$this->value instanceof DateTimeInterface && $value instanceof DateTimeInterface => $value->getTimestamp() === $this->value->getTimestamp(),
463463
default => $value === $this->value,
464464
};
465465

src/Parameters.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use function array_key_exists;
1919
use function array_keys;
2020
use function array_map;
21+
use function array_replace;
2122
use function count;
2223
use function implode;
2324
use function is_int;
@@ -523,10 +524,13 @@ public function add(
523524
*/
524525
private function newInstance(array $members): self
525526
{
526-
return match(true) {
527-
$members == $this->members => $this,
528-
default => new self($members),
529-
};
527+
foreach ($members as $offset => $member) {
528+
if (!isset($this->members[$offset]) || !$this->members[$offset]->equals($member)) {
529+
return new self($members);
530+
}
531+
}
532+
533+
return $this;
530534
}
531535

532536
private function remove(string|int ...$offsets): self
@@ -652,7 +656,7 @@ public function replace(int $index, array $pair): self
652656
$pairs = iterator_to_array($this);
653657

654658
return match (true) {
655-
$pairs[$offset] == $pair => $this,
659+
$pairs[$offset][0] === $pair[0] && $pairs[$offset][1]->equals($pair[1]) => $this,
656660
default => self::fromPairs(array_replace($pairs, [$offset => $pair])),
657661
};
658662
}
@@ -666,7 +670,7 @@ public function mergeAssociative(StructuredFieldProvider|iterable ...$others): s
666670
foreach ($others as $other) {
667671
if ($other instanceof StructuredFieldProvider) {
668672
$other = $other->toStructuredField();
669-
if (!is_iterable($other)) {
673+
if (!$other instanceof Dictionary && !$other instanceof Parameters) {
670674
throw new InvalidArgument('The "'.$other::class.'" instance can not be used for creating a .'.self::class.' structured field.');
671675
}
672676
}

src/StructuredFieldProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
/**
1111
* @phpstan-type SfType Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool
1212
* @phpstan-type SfTypeInput SfType|DateTimeInterface
13-
* @phpstan-type SfDataType Dictionary|InnerList|Item|OuterList|Parameters
13+
* @phpstan-type SfList InnerList|OuterList
14+
* @phpstan-type SfOrderedMap Dictionary|Parameters
15+
* @phpstan-type SfDataType SfList|SfOrderedMap|Item
1416
* @phpstan-type SfItemInput SfTypeInput|SfDataType|StructuredFieldProvider
1517
* @phpstan-type SfMemberInput iterable<SfItemInput>|SfItemInput
1618
* @phpstan-type SfParameterInput iterable<array{0:string, 1?:SfItemInput}>

src/Validation/ParametersValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function validate(Parameters|Stringable|string $parameters): Result
108108
}
109109
}
110110

111-
if ([] == $this->filterConstraints && null === $this->criteria) {
111+
if ([] === $this->filterConstraints && null === $this->criteria) {
112112
$violations->add(ErrorCode::ParametersMissingConstraints->value, new Violation('The parameters constraints are missing.'));
113113
}
114114

tests/ItemTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function it_updates_item(Bytes|Token|DisplayString|DateTimeInterface|stri
9292
}
9393

9494
/**
95-
* @return iterable<string, array{value:SfItemInput, expected:string}>>
95+
* @return iterable<string, array{value:SfItemInput, expected:string}>
9696
*/
9797
public static function provideFrom1stArgument(): iterable
9898
{

0 commit comments

Comments
 (0)