Skip to content

Commit

Permalink
Using PHPStan 2 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod authored Dec 7, 2024
1 parent b743d05 commit 6c72fc0
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.65.0",
"httpwg/structured-field-tests": "*@dev",
"phpstan/phpstan": "^1.12.12",
"phpstan/phpstan-strict-rules": "^1.6.1",
"phpstan/phpstan-phpunit": "^1.4.1",
"phpstan/phpstan-deprecation-rules": "^1.2.1",
"phpstan/phpstan": "^2.0.3",
"phpstan/phpstan-strict-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0.1",
"phpstan/phpstan-deprecation-rules": "^2.0.1",
"phpunit/phpunit": "^10.5.38 || ^11.5.0",
"symfony/var-dumper": "^6.4.15 || ^v7.2.0",
"bakame/aide-base32": "dev-main",
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ includes:
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
parameters:
level: max
level: 9
paths:
- src
- tests
Expand Down
15 changes: 9 additions & 6 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function fromPairs(StructuredFieldProvider|Dictionary|Parameters|i
}

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

return is_iterable($pair[0]) ? InnerList::fromPair($pair) : Item::fromPair($pair);
Expand Down Expand Up @@ -527,10 +527,13 @@ public function add(
*/
private function newInstance(array $members): self
{
return match (true) {
$members == $this->members => $this,
default => new self($members),
};
foreach ($members as $offset => $member) {
if (!isset($this->members[$offset]) || !$this->members[$offset]->equals($member)) {
return new self($members);
}
}

return $this;
}

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

return match (true) {
$pairs[$offset] == $pair => $this,
$pairs[$offset][0] === $pair[0] && $pairs[$offset][1]->equals($pair[1]) => $this,
default => self::fromPairs(array_replace($pairs, [$offset => $pair])),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public function withValue(DateTimeInterface|Bytes|Token|DisplayString|string|int
$this->value instanceof Bytes,
$this->value instanceof Token,
$this->value instanceof DisplayString => $this->value->equals($value),
$this->value instanceof DateTimeInterface && $value instanceof DateTimeInterface => $value == $this->value,
$this->value instanceof DateTimeInterface && $value instanceof DateTimeInterface => $value->getTimestamp() === $this->value->getTimestamp(),
default => $value === $this->value,
};

Expand Down
16 changes: 10 additions & 6 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_replace;
use function count;
use function implode;
use function is_int;
Expand Down Expand Up @@ -523,10 +524,13 @@ public function add(
*/
private function newInstance(array $members): self
{
return match(true) {
$members == $this->members => $this,
default => new self($members),
};
foreach ($members as $offset => $member) {
if (!isset($this->members[$offset]) || !$this->members[$offset]->equals($member)) {
return new self($members);
}
}

return $this;
}

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

return match (true) {
$pairs[$offset] == $pair => $this,
$pairs[$offset][0] === $pair[0] && $pairs[$offset][1]->equals($pair[1]) => $this,
default => self::fromPairs(array_replace($pairs, [$offset => $pair])),
};
}
Expand All @@ -666,7 +670,7 @@ public function mergeAssociative(StructuredFieldProvider|iterable ...$others): s
foreach ($others as $other) {
if ($other instanceof StructuredFieldProvider) {
$other = $other->toStructuredField();
if (!is_iterable($other)) {
if (!$other instanceof Dictionary && !$other instanceof Parameters) {
throw new InvalidArgument('The "'.$other::class.'" instance can not be used for creating a .'.self::class.' structured field.');
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/StructuredFieldProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
/**
* @phpstan-type SfType Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool
* @phpstan-type SfTypeInput SfType|DateTimeInterface
* @phpstan-type SfDataType Dictionary|InnerList|Item|OuterList|Parameters
* @phpstan-type SfList InnerList|OuterList
* @phpstan-type SfOrderedMap Dictionary|Parameters
* @phpstan-type SfDataType SfList|SfOrderedMap|Item
* @phpstan-type SfItemInput SfTypeInput|SfDataType|StructuredFieldProvider
* @phpstan-type SfMemberInput iterable<SfItemInput>|SfItemInput
* @phpstan-type SfParameterInput iterable<array{0:string, 1?:SfItemInput}>
Expand Down
2 changes: 1 addition & 1 deletion src/Validation/ParametersValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function validate(Parameters|Stringable|string $parameters): Result
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function it_updates_item(Bytes|Token|DisplayString|DateTimeInterface|stri
}

/**
* @return iterable<string, array{value:SfItemInput, expected:string}>>
* @return iterable<string, array{value:SfItemInput, expected:string}>
*/
public static function provideFrom1stArgument(): iterable
{
Expand Down

0 comments on commit 6c72fc0

Please sign in to comment.