Skip to content

Commit

Permalink
Upgraded phpstan to v2 and psalm to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
sad-spirit committed Feb 2, 2025
1 parent ba1a851 commit a90fb16
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 47 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

Package manual is now published on [Read the Docs](https://pg-wrapper.readthedocs.io)

### Added
* `converters\EnumConverter` class: converts values of Postgres `ENUM` type to PHP's string-backed enum and back.
* It is now possible to pass classname of `types\Range` / `types\MultiRange` subclass to
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"fig/log-test": "^1.1",
"phpunit/phpunit": "^10.0",
"psr/cache": "^3.0",
"phpstan/phpstan": "^1.12",
"vimeo/psalm": "^5.26"
"phpstan/phpstan": "^2.0",
"vimeo/psalm": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 13 additions & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ parameters:
count: 1
path: src/sad_spirit/pg_wrapper/converters/CachedTypeOIDMapper.php

# well duh
-
message: "#^Parameter \\#1 \\$connection of function pg_connection_status expects resource, Pgsql\\\\Connection given\\.$#"
message: "#^Offset 'foo' on sad_spirit\\\\pg_wrapper\\\\(tests\\\\)?types\\\\[a-zA-Z]+ in isset\\(\\) does not exist\\.$#"
paths:
- src/sad_spirit/pg_wrapper/Connection.php
- tests/ConnectionTest.php
reportUnmatched: false
- tests\types\PointListTest.php
- tests\types\MultiRangeTest.php

# well duh
-
message: "#^Offset 'foo' on sad_spirit\\\\pg_wrapper\\\\(tests\\\\)?types\\\\[a-zA-Z]+ in isset\\(\\) does not exist\\.$#"
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert::assert(True|False)\\(\\) with false#"
paths:
- tests\ConnectionTransactionsTest.php
- tests\types\PointListTest.php
- tests\types\MultiRangeTest.php
- tests\types\MultiRangeTest.php

# New false positives in phpstan 2, probably it is trying to be "smart" with methods named isWhatever()?
-
message: "#^Call to method sad_spirit\\\\pg_wrapper\\\\converters\\\\TypeOIDMapper::is(Range|MultiRange|Domain)TypeOID\\(\\) with int\\|string and null will always evaluate to false\\.$#"
count: 3
path: src/sad_spirit/pg_wrapper/converters/DefaultTypeConverterFactory.php
16 changes: 0 additions & 16 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@
<file name="tests/converters/IntervalTest.php" />
</errorLevel>
</InaccessibleProperty>

<!-- Ignore these in tests, whatever we access is guaranteed to be not null -->
<PossiblyNullArrayAccess>
<errorLevel type="suppress">
<!-- https://github.com/vimeo/psalm/issues/7221 -->
<file name="src/sad_spirit/pg_wrapper/converters/datetime/IntervalConverter.php"/>
<directory name="tests"/>
</errorLevel>
</PossiblyNullArrayAccess>
<PossiblyNullArgument>
<errorLevel type="suppress">
<!-- https://github.com/vimeo/psalm/issues/7221 -->
<file name="src/sad_spirit/pg_wrapper/converters/datetime/IntervalConverter.php"/>
<directory name="tests"/>
</errorLevel>
</PossiblyNullArgument>
</issueHandlers>

<stubs>
Expand Down
18 changes: 16 additions & 2 deletions src/sad_spirit/pg_wrapper/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,14 @@ public function fetchColumn(int|string $fieldIndex): array
$result = [];
$native = $this->getNative();
for ($i = 0; $i < $this->numRows; $i++) {
$result[] = $this->converters[$fieldIndex]->input(\pg_fetch_result($native, $i, $fieldIndex));
if (false === $field = \pg_fetch_result($native, $i, $fieldIndex)) {
throw new exceptions\RuntimeException(\sprintf(
"Failed to fetch field %d in row %d of result set",
$fieldIndex,
$i
));
}
$result[] = $this->converters[$fieldIndex]->input($field);
}
return $result;
}
Expand Down Expand Up @@ -410,7 +417,14 @@ public function iterateColumn(int|string $fieldIndex): \Traversable
$native = $this->getNative();

for ($i = 0; $i < $this->numRows; $i++) {
yield $this->converters[$fieldIndex]->input(\pg_fetch_result($native, $i, $fieldIndex));
if (false === $field = \pg_fetch_result($native, $i, $fieldIndex)) {
throw new exceptions\RuntimeException(\sprintf(
"Failed to fetch field %d in row %d of result set",
$fieldIndex,
$i
));
}
yield $this->converters[$fieldIndex]->input($field);
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/sad_spirit/pg_wrapper/converters/BaseNumericConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ public function setAllowNonDecimalLiteralsAndUnderscores(bool $allow): void
*/
public function allowNonDecimalLiteralsAndUnderscores(): bool
{
return (bool)(
$this->allowNonDecimal
?? ($this->allowNonDecimal = $this->getAllowNonDecimalLiteralsFromConnection())
);
return (bool) $this->allowNonDecimal ??= $this->getAllowNonDecimalLiteralsFromConnection();
}

/**
Expand All @@ -70,9 +67,9 @@ protected function getAllowNonDecimalLiteralsFromConnection(): ?bool
}

return \version_compare(
\pg_parameter_status($this->connection->getNative(), 'server_version'),
'15.999',
'>='
\pg_parameter_status($this->connection->getNative(), 'server_version') ?: '1',
'15',
'>'
);
}
}
6 changes: 4 additions & 2 deletions src/sad_spirit/pg_wrapper/converters/ByteaConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ protected function outputNotNull(mixed $value): string
{
if (!\is_string($value)) {
throw TypeConversionException::unexpectedValue($this, 'output', 'string', $value);
} elseif (false === $encoded = \unpack('H*', $value)) {
// Unlikely to happen
throw new TypeConversionException("Failed to encode binary string");
}

[, $encoded] = \unpack('H*', $value);
return '\x' . $encoded;
return '\x' . $encoded[1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public function getOIDMapper(): TypeOIDMapper
*
* @param class-string<TypeConverter>|callable|TypeConverter $converter
* @param string|string[] $type
* @throws InvalidArgumentException
*/
public function registerConverter(
callable|TypeConverter|string $converter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function countPoints(string $native): int
* @param int $count number of points to parse
* @param bool $allowSquare whether square brackets [] are allowed around points
* @param bool|null $squareDelimiter whether square brackets were actually used
* @param-out bool $squareDelimiter
* @return Point[]
* @throws TypeConversionException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PathConverter extends BaseGeometricConverter
protected function parseInput(string $native, int &$pos): Path
{
$points = $this->parsePoints($native, $pos, $this->countPoints($native), true, $usedSquare);
return new Path($usedSquare ?? false, ...$points);
return new Path($usedSquare, ...$points);
}

protected function outputNotNull(mixed $value): string
Expand Down
6 changes: 1 addition & 5 deletions src/sad_spirit/pg_wrapper/types/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ public function jsonSerialize(): array
*/
public static function createFromArray(array $input): static
{
if (\is_bool(\reset($input))) {
$open = \array_shift($input);
} else {
$open = false;
}
$open = \is_bool(\reset($input)) && \array_shift($input);

return new self($open, ...self::createPointArray($input));
}
Expand Down
6 changes: 3 additions & 3 deletions tests/DefaultTypeConverterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function testMultiRangeTypeConverterFromMetadata(): void
$connection = new Connection(TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING, false);
$connection->setTypeConverterFactory($this->factory);
$serverVersion = \pg_parameter_status($connection->getNative(), 'server_version');
if (\version_compare($serverVersion, '14-beta', '<')) {
if (!\version_compare($serverVersion ?: '1', '13', '>')) {
$this::markTestSkipped('Postgres version 14 is required for multirange support');
}

Expand Down Expand Up @@ -434,7 +434,7 @@ public function testConnectionAwareSubConverterOfMultiRangeShouldBeConfigured():
$connection = new Connection(TESTS_SAD_SPIRIT_PG_WRAPPER_CONNECTION_STRING);
$connection->setTypeConverterFactory($this->factory);
$serverVersion = \pg_parameter_status($connection->getNative(), 'server_version');
if (\version_compare($serverVersion, '14-beta', '<')) {
if (!\version_compare($serverVersion ?: '1', '13', '>')) {
$this::markTestSkipped('Postgres version 14 is required for multirange support');
}

Expand Down Expand Up @@ -538,7 +538,7 @@ public function testConfigureIntegerConverterFromConnection(): void
$converter = $this->factory->getConverterForTypeSpecification('integer');

$this->assertSame(
\version_compare($serverVersion, '16-beta', '>='),
\version_compare($serverVersion ?: '1', '15', '>'),
$converter->allowNonDecimalLiteralsAndUnderscores()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/types/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testSerializesToJson(ArrayRepresentable $object): void
{
$this::assertEquals(
$object,
$object::createFromArray(\json_decode(\json_encode($object), true))
$object::createFromArray(\json_decode(\json_encode($object, \JSON_THROW_ON_ERROR), true))
);
}

Expand Down

0 comments on commit a90fb16

Please sign in to comment.