Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"php": "^8.4",
"symfony/framework-bundle": "^7.0",
"assoconnect/php-percent": "^1.1",
"doctrine/dbal": "^2.10|^3.0",
"doctrine/dbal": "^3.6|^4.0",
"symfony/serializer": "^7.0",
"assoconnect/validator-bundle": "^2.39.2"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ parameters:
- 'Symfony\Component\Validator\Exception\InvalidOptionsException'
- 'Symfony\Component\Validator\Exception\MissingOptionsException'

# DBAL 3 fallback branch, dead code when DBAL 4 is installed (^3.6|^4.0 are both supported)
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#Call to an undefined static method Doctrine\\DBAL\\Types\\ConversionException::conversionFailed#'

includes:
- vendor/assoconnect/php-quality-config/phpstan.extension.neon
30 changes: 23 additions & 7 deletions src/Doctrine/DBAL/Types/PercentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AssoConnect\PHPPercent\Percent;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Type;

class PercentType extends Type
Expand All @@ -33,7 +34,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
return $value->toInteger();
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', Percent::class]);
throw $this->createInvalidTypeException($value, ['null', Percent::class]);
}

public function convertToPHPValue($value, AbstractPlatform $platform): ?Percent
Expand All @@ -45,12 +46,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?Percent
try {
return new Percent(is_string($value) ? (int)$value : $value);
} catch (\Throwable $exception) {
throw ConversionException::conversionFailedInvalidType(
$value,
$this->getName(),
['null', 'integer'],
$exception
);
throw $this->createInvalidTypeException($value, ['null', 'integer'], $exception);
}
}

Expand All @@ -61,4 +57,24 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}

/**
* DBAL 4 replaced the ConversionException static factories with dedicated exception classes.
* The runtime conditional below can be inlined once DBAL 3 support is dropped.
* Excluded from coverage: only one branch can run for a given installed DBAL major.
*
* @codeCoverageIgnore
* @param string[] $possibleTypes
*/
private function createInvalidTypeException(
mixed $value,
array $possibleTypes,
?\Throwable $previous = null
): ConversionException {
if (class_exists(InvalidType::class)) {
return InvalidType::new($value, self::TYPE, $possibleTypes, $previous);
}

return ConversionException::conversionFailedInvalidType($value, self::TYPE, $possibleTypes, $previous);
}
}
13 changes: 5 additions & 8 deletions tests/Doctrine/DBAL/Types/PercentTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use stdClass;

class PercentTypeTest extends TestCase
{
/**
* @var AbstractPlatform|MockObject
*/
protected $platform;
protected AbstractPlatform&Stub $platform;

/**
* @var Type
Expand All @@ -30,15 +28,14 @@ class PercentTypeTest extends TestCase
*/
protected function setUp(): void
{
$this->platform = $this->getMockForAbstractClass(AbstractPlatform::class);
$this->platform = self::createStub(AbstractPlatform::class);
$this->type = new PercentType();
}

/**
* @param mixed $value
*
* @dataProvider invalidPHPValuesProvider
*/
#[DataProvider('invalidPHPValuesProvider')]
public function testInvalidTypeConversionToDatabaseValue($value): void
{
$this->expectException(ConversionException::class);
Expand Down
Loading