Skip to content

Commit

Permalink
chore: 🧹 static analysis and redundant <8.1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
zanbaldwin committed Dec 4, 2024
1 parent 19e427f commit dae600f
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 247 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
php:
- '8.3'
- '8.4'
steps:
- uses: 'actions/checkout@v2'
- uses: 'shivammathur/setup-php@v2'
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- uses: 'actions/checkout@v2'
- uses: 'shivammathur/setup-php@v2'
Expand All @@ -34,6 +35,7 @@ jobs:
- '8.1'
- '8.2'
- '8.3'
- '8.4'
steps:
- uses: 'actions/checkout@v2'
- uses: 'shivammathur/setup-php@v2'
Expand All @@ -42,4 +44,4 @@ jobs:
- uses: 'ramsey/composer-install@v2'
with:
dependency-versions: 'highest'
- run: './vendor/bin/phpunit --bootstrap="tests/bootstrap.php" --test-suffix="Test.php" tests'
- run: './vendor/bin/phpunit --bootstrap="vendor/autoload.php" --test-suffix="Test.php" tests'
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ Please refer to the [`darsyn/ip`][ip] project for complete documentation.

## Compatibility

This library has extensive test coverage using PHPUnit on PHP versions: `8.1`, `8.2`, and `8.3`.
This library has extensive test coverage using PHPUnit on PHP versions: `8.1`,
`8.2`, `8.3` and `8.4`.

Static analysis is performed with PHPStan at `max` level on PHP `8.3`, using
Static analysis is performed with PHPStan at `max` level on PHP `8.4`, using
core, bleeding edge, and deprecation rules.

The Doctrine features are compatible with Doctrine DBAL `^4`. Types
are provided for `Ipv4`, `IPv6`, and `Multi` IP types from the parent
Types are provided for `Ipv4`, `IPv6`, and `Multi` IP types from the parent
[`darsyn/ip`][ip] project.
- Versions `5.*.*` are for Doctrine DBAL `^2.3 || ^3.0` compatibility (PHP `5.6`
and greater).
- Versions `6.*.*` are for Doctrine DBAL `^4` (PHP `8.1` and greater).

## Code of Conduct

Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ parameters:
level: 'max'
paths: [ 'src', 'tests' ]
checkFunctionNameCase: true
checkGenericClassInNonGenericObjectType: true
reportUnmatchedIgnoredErrors: true
treatPhpDocTypesAsCertain: false
parallel:
Expand Down
38 changes: 10 additions & 28 deletions src/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@
abstract class AbstractType extends Type
{
const NAME = 'ip';
/** @var int */
const IP_LENGTH = 16;

/**
* @psalm-return class-string
* @return string
*/
abstract protected function getIpClass();
abstract protected function getIpClass(): string;

/**
* @param string $ip
* @throws \Darsyn\IP\Exception\InvalidIpAddressException
* @throws \Darsyn\IP\Exception\WrongVersionException
* @return \Darsyn\IP\IpInterface
*/
abstract protected function createIpObject($ip);
abstract protected function createIpObject(string $ip): IpInterface;

/**
* {@inheritdoc}
Expand All @@ -50,7 +48,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st
* @throws \Doctrine\DBAL\Types\ConversionException
* @return \Darsyn\IP\IpInterface|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): mixed
{
/** @var string|resource|\Darsyn\IP\IpInterface|null $value */
// PostgreSQL will return the binary data as a resource instead of a string (like MySQL).
Expand All @@ -66,14 +64,16 @@ public function convertToPHPValue($value, AbstractPlatform $platform): mixed
if (empty($value)) {
return null;
}
if (\is_object($value) && \is_a($value, $this->getIpClass(), false)) {
/** @var \Darsyn\IP\IpInterface $value */
if (\is_object($value)) {
if (!\is_a($value, $this->getIpClass(), false)) {
throw ValueNotConvertible::new($value, $this->getIpClass());
}
return $value;
}
try {
return $this->createIpObject($value);
} catch (IpException $e) {
throw ValueNotConvertible::new($value, static::NAME, null, $e);
throw ValueNotConvertible::new($value, $this->getIpClass(), null, $e);
}
}

Expand All @@ -82,7 +82,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): mixed
* @throws \Doctrine\DBAL\Types\ConversionException
* @return string|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
{
if (empty($value)) {
return null;
Expand Down Expand Up @@ -124,15 +124,6 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): mixe
return $value->getBinary();
}

/**
* {@inheritdoc}
* @return string
*/
public function getName()
{
return self::NAME;
}

/**
* {@inheritdoc}
* @return ParameterType
Expand All @@ -141,13 +132,4 @@ public function getBindingType(): ParameterType
{
return ParameterType::LARGE_OBJECT;
}

/**
* {@inheritdoc}
* @return bool
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
}
5 changes: 3 additions & 2 deletions src/IPv4Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Darsyn\IP\Doctrine;

use Darsyn\IP\IpInterface;
use Darsyn\IP\Version\IPv4 as IP;

/**
Expand All @@ -14,15 +15,15 @@ class IPv4Type extends AbstractType
/**
* {@inheritDoc}
*/
protected function getIpClass()
protected function getIpClass(): string
{
return IP::class;
}

/**
* {@inheritDoc}
*/
protected function createIpObject($ip)
protected function createIpObject(string $ip): IpInterface
{
return IP::factory($ip);
}
Expand Down
5 changes: 3 additions & 2 deletions src/IPv6Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Darsyn\IP\Doctrine;

use Darsyn\IP\IpInterface;
use Darsyn\IP\Version\IPv6 as IP;

/**
Expand All @@ -12,15 +13,15 @@ class IPv6Type extends AbstractType
/**
* {@inheritDoc}
*/
protected function getIpClass()
protected function getIpClass(): string
{
return IP::class;
}

/**
* {@inheritDoc}
*/
protected function createIpObject($ip)
protected function createIpObject(string $ip): IpInterface
{
return IP::factory($ip);
}
Expand Down
5 changes: 3 additions & 2 deletions src/MultiType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Darsyn\IP\Doctrine;

use Darsyn\IP\IpInterface;
use Darsyn\IP\Version\Multi as IP;

/**
Expand All @@ -12,15 +13,15 @@ class MultiType extends AbstractType
/**
* {@inheritDoc}
*/
protected function getIpClass()
protected function getIpClass(): string
{
return IP::class;
}

/**
* {@inheritDoc}
*/
protected function createIpObject($ip)
protected function createIpObject(string $ip): IpInterface
{
return IP::factory($ip);
}
Expand Down
Loading

0 comments on commit dae600f

Please sign in to comment.