Skip to content

Commit

Permalink
added set of methods to check member and value against a collection o…
Browse files Browse the repository at this point in the history
…f items
  • Loading branch information
thunderer committed Jan 22, 2024
1 parent 89c4ceb commit 783afe9
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 39 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
runs-on: '${{ matrix.os }}'
strategy:
matrix:
php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
os: ['ubuntu-latest']
failure: [false]
include:
- { php: '8.2', os: 'ubuntu-latest', failure: true } # '8.2' means 'nightly'
- { php: 'nightly', os: 'ubuntu-latest', failure: true }
steps:
- name: 'Checkout'
uses: 'actions/checkout@v2'
Expand All @@ -37,5 +37,5 @@ jobs:
run: 'php vendor/bin/psalm --no-cache --shepherd'
continue-on-error: '${{ matrix.failure }}'
- name: 'Infection'
run: 'php vendor/bin/infection -j2 --min-msi=97'
run: 'php vendor/bin/infection -j2 --min-msi=95'
continue-on-error: '${{ matrix.failure }}'
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ travis:
PHP_VERSION=7.3 make travis-job
PHP_VERSION=7.4 make travis-job
PHP_VERSION=8.0 make travis-job
PHP_VERSION=8.1 make travis-job
PHP_VERSION=8.2 make travis-job
PHP_VERSION=8.3 make travis-job
travis-job:
${PHP} composer update
${PHP} php -v
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
},
"require-dev": {
"ext-json": "*",
"phpunit/phpunit": ">=6.0",
"vimeo/psalm": ">=3.14",
"doctrine/dbal": "^2.13",
"doctrine/orm": "^2.9",
"phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0|^10.0",
"vimeo/psalm": "^3.14|^4.0|^5.0",
"doctrine/dbal": "^2.13|^3.0",
"doctrine/orm": "^2.9|^3.0",
"infection/infection": ">=0.13"
},
"autoload": {
Expand Down
13 changes: 8 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ x-php: &php
XDEBUG_MODE: coverage

services:
php-7.1: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.1.3 } } }
php-7.2: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.2 } } }
php-7.3: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.3 } } }
php-7.4: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.4 } } }
php-8.0: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 8.0 } } }
php-7.1: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.1.3, XDEBUG_VERSION: xdebug-2.9.8 } } }
php-7.2: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.2, XDEBUG_VERSION: xdebug-3.1.6 } } }
php-7.3: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.3, XDEBUG_VERSION: xdebug-3.1.6 } } }
php-7.4: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 7.4, XDEBUG_VERSION: xdebug-3.1.6 } } }
php-8.0: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 8.0, XDEBUG_VERSION: xdebug } } }
php-8.1: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 8.1, XDEBUG_VERSION: xdebug } } }
php-8.2: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 8.2, XDEBUG_VERSION: xdebug } } }
php-8.3: { <<: *php, build: { context: docker/php, args: { PHP_VERSION: 8.3, XDEBUG_VERSION: xdebug } } }
3 changes: 2 additions & 1 deletion docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ARG PHP_VERSION=7.4
FROM php:$PHP_VERSION
ARG XDEBUG_VERSION=xdebug

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === file_get_contents('https://composer.github.io/installer.sig')) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
Expand All @@ -8,4 +9,4 @@ RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& mv composer.phar /usr/local/bin/composer

RUN apt update && apt install -y libzip-dev && docker-php-ext-install zip
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN pecl install ${XDEBUG_VERSION} && docker-php-ext-enable xdebug
22 changes: 11 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
bootstrap = "vendor/autoload.php"
colors="true"
stopOnFailure="false"
backupGlobals="false"
processIsolation="false"
backupStaticProperties="true"
bootstrap="vendor/autoload.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd">

<testsuites>
<testsuite name="Platenum">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>src</directory>
</include>
</source>

<coverage>
<report>
<clover outputFile="coverage.xml" />
<html outputDirectory="coverage" lowUpperBound="50" highLowerBound="90" />
Expand Down
19 changes: 19 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,23 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UnusedClosureParam errorLevel="error">
<errorLevel type="suppress">
<file name="src/Doctrine/PlatenumDoctrineType.php" />
</errorLevel>
</UnusedClosureParam>
<UnusedPsalmSuppress errorLevel="error">
<errorLevel type="suppress">
<file name="src/Enum/AttributeEnumTrait.php" />
</errorLevel>
</UnusedPsalmSuppress>
<DeprecatedMethod errorLevel="error">
<errorLevel type="suppress">
<file name="src/Doctrine/PlatenumDoctrineType.php" />
</errorLevel>
</DeprecatedMethod>
</issueHandlers>

</psalm>
4 changes: 3 additions & 1 deletion src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private function computeClassPath(string $fqcn): string
private function generateClassCode(string $type, string $fqcn, string $keys): string
{
$namespace = $fqcn;
$lastSlash = strrpos($namespace, '\\');
$lastSlash = (int)strrpos($namespace, '\\');
$class = substr($namespace, $lastSlash ? $lastSlash + 1 : 0);
$namespace = $lastSlash ? substr($namespace, 0, $lastSlash) : 'X';

Expand All @@ -123,6 +123,7 @@ private function generateClassCode(string $type, string $fqcn, string $keys): st
for($i = 0; $i < $count; $i++) {
$key = $matches['key'][$i];
$value = $matches['value'][$i] ?: $index++;
/** @psalm-suppress TypeDoesNotContainType */
if(false === ctype_digit((string)$value)) {
$value = '\''.$value.'\'';
}
Expand All @@ -146,6 +147,7 @@ private function generateClassCode(string $type, string $fqcn, string $keys): st
'<MEMBERS>' => $values[$type]['members'],
];

/** @psalm-suppress InvalidArgument */
return str_replace(array_keys($replaces), array_values($replaces), $values[$type]['template']);
}

Expand Down
16 changes: 7 additions & 9 deletions src/Doctrine/PlatenumDoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ public static function registerInteger(string $alias, string $class): void
$toInteger = function($value): int {
return (int)$value;

Check warning on line 29 in src/Doctrine/PlatenumDoctrineType.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "CastInt": --- Original +++ New @@ @@ { /** @psalm-suppress MissingClosureParamType */ $toInteger = function ($value) : int { - return (int) $value; + return $value; }; $sql = function (array $declaration, AbstractPlatform $platform) : string { return $platform->getIntegerTypeDeclarationSQL([]);

Check warning on line 29 in src/Doctrine/PlatenumDoctrineType.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "CastInt": --- Original +++ New @@ @@ { /** @psalm-suppress MissingClosureParamType */ $toInteger = function ($value) : int { - return (int) $value; + return $value; }; $sql = function (array $declaration, AbstractPlatform $platform) : string { return $platform->getIntegerTypeDeclarationSQL([]);

Check warning on line 29 in src/Doctrine/PlatenumDoctrineType.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "CastInt": --- Original +++ New @@ @@ { /** @psalm-suppress MissingClosureParamType */ $toInteger = function ($value) : int { - return (int) $value; + return $value; }; $sql = function (array $declaration, AbstractPlatform $platform) : string { return $platform->getIntegerTypeDeclarationSQL([]);

Check warning on line 29 in src/Doctrine/PlatenumDoctrineType.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "CastInt": --- Original +++ New @@ @@ { /** @psalm-suppress MissingClosureParamType */ $toInteger = function ($value) : int { - return (int) $value; + return $value; }; $sql = function (array $declaration, AbstractPlatform $platform) : string { return $platform->getIntegerTypeDeclarationSQL([]);
};
/** @psalm-suppress UnusedClosureParam */
$sql = function(array $declaration, AbstractPlatform $platform): string {
return $platform->getIntegerTypeDeclarationSQL([]);
};

static::registerCallback($alias, $class, $toInteger, $sql);
self::registerCallback($alias, $class, $toInteger, $sql);
}

/**
Expand All @@ -46,12 +45,11 @@ public static function registerString(string $alias, string $class): void
$toString = function($value): string {
return (string)$value;
};
/** @psalm-suppress UnusedClosureParam */
$sql = function(array $declaration, AbstractPlatform $platform): string {
return $platform->getVarcharTypeDeclarationSQL([]);
};

static::registerCallback($alias, $class, $toString, $sql);
self::registerCallback($alias, $class, $toString, $sql);
}

/**
Expand All @@ -62,17 +60,17 @@ public static function registerString(string $alias, string $class): void
*/
private static function registerCallback(string $alias, string $class, callable $callback, callable $sql): void
{
if(static::hasType($alias)) {
if(self::hasType($alias)) {
throw new \LogicException(sprintf('Alias `%s` was already registered in PlatenumDoctrineType.', $class));
}
if(false === in_array(EnumTrait::class, static::allTraitsOf($class), true)) {
if(false === in_array(EnumTrait::class, self::allTraitsOf($class), true)) {
throw new \LogicException(sprintf('PlatenumDoctrineType allows only Platenum enumerations, `%s` given.', $class));
}

static::addType($alias, static::class);
self::addType($alias, self::class);

/** @var static $type */
$type = static::getType($alias);
$type = self::getType($alias);
$type->platenumAlias = $alias;
$type->platenumClass = $class;
$type->platenumCallback = $callback;
Expand Down Expand Up @@ -119,7 +117,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
}
if(false === is_object($value)) {
$message = 'Impossible situation: `%s` allows to register only Platenum types, `%s` given.';
throw new \LogicException(sprintf($message, static::class, gettype($value)));
throw new \LogicException(sprintf($message, self::class, gettype($value)));
}

/** @psalm-suppress MixedMethodCall */
Expand Down
50 changes: 49 additions & 1 deletion src/Enum/EnumTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait EnumTrait
protected static $instances = [];

/** @param int|string $value */
final private function __construct(string $member, $value)
/* final */ private function __construct(string $member, $value)
{
$this->member = $member;
$this->value = $value;
Expand Down Expand Up @@ -99,6 +99,20 @@ final public function fromInstance(&$enum): void
$enum = static::fromEnum($enum);
}

/**
* @psalm-suppress UnusedForeachValue
* @return list<static>
*/
final public static function getInstances(): array
{
static::resolveMembers();

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }

Check warning on line 108 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ */ public static final function getInstances() : array { - static::resolveMembers(); + foreach (static::$members[static::class] as $member => $value) { static::fromMember($member); }
foreach(static::$members[static::class] as $member => $value) {

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);

Check warning on line 109 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ public static final function getInstances() : array { static::resolveMembers(); - foreach (static::$members[static::class] as $member => $value) { + foreach (array() as $member => $value) { static::fromMember($member); } return array_values(static::$instances[static::class]);
static::fromMember($member);

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.0, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.1, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.2, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (8.3, ubuntu-latest, false)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }

Check warning on line 110 in src/Enum/EnumTrait.php

View workflow job for this annotation

GitHub Actions / test (nightly, ubuntu-latest, true)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { static::resolveMembers(); foreach (static::$members[static::class] as $member => $value) { - static::fromMember($member); + } return array_values(static::$instances[static::class]); }
}

return array_values(static::$instances[static::class]);
}

/* --- EXCEPTIONS --- */

/** @psalm-suppress UnusedParam */
Expand Down Expand Up @@ -174,6 +188,22 @@ final public static function valueExists($value): bool
return \in_array($value, static::$members[static::class], true);
}

/** @param list<string> $members */
final public static function oneOfMembersExists(array $members): bool
{
static::resolveMembers();

return [] !== array_intersect(array_keys(static::$members[static::class]), $members);
}

/** @param list<int|string> $values */
final public static function oneOfValuesExists(array $values): bool
{
static::resolveMembers();

return [] !== array_intersect(static::$members[static::class], $values);
}

final public function hasMember(string $members): bool
{
return $members === $this->member;
Expand All @@ -185,6 +215,24 @@ final public function hasValue($value): bool
return $value === $this->value;
}

/** @param list<string> $members */
final public function hasOneOfMembers(array $members): bool
{
return in_array($this->member, $members, true);
}

/** @param list<int|string> $values */
final public function hasOneOfValues(array $values): bool
{
return in_array($this->value, $values, true);
}

/** @param list<static> $enums */
final public function isOneOfInstances(array $enums): bool
{
return in_array($this, $enums, true);
}

/* --- INFO --- */

/** @return int|string */
Expand Down
7 changes: 4 additions & 3 deletions tests/DoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Thunder\Platenum\Tests;

use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\MySQL80Platform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function testDoctrineType(): void
PlatenumDoctrineType::registerInteger('intEnum0', DoctrineIntEnum::class);
$intType = PlatenumDoctrineType::getType('intEnum0');

$platform = new MySqlPlatform();
$platform = new MySQL80Platform();
$this->assertTrue($intType->requiresSQLCommentHint($platform));
$this->assertSame('intEnum0', $intType->getName());
$this->assertSame('INT', $intType->getSQLDeclaration([], $platform));
Expand Down Expand Up @@ -84,13 +85,13 @@ public function testImpossibleDatabaseConversionWithUnsupportedValue(): void
{
PlatenumDoctrineType::registerString('impossibleEnumConvert', DoctrineIntEnum::class);
$this->expectException(\LogicException::class);
PlatenumDoctrineType::getType('impossibleEnumConvert')->convertToDatabaseValue('not an object', new MySqlPlatform());
PlatenumDoctrineType::getType('impossibleEnumConvert')->convertToDatabaseValue('not an object', new MySql80Platform());
}

public function testImpossibleValueConversionCast(): void
{
PlatenumDoctrineType::registerString('impossibleEnumCast', DoctrineIntEnum::class);
$result = PlatenumDoctrineType::getType('impossibleEnumCast')->convertToDatabaseValue(DoctrineIntEnum::FIRST(), new MySqlPlatform());
$result = PlatenumDoctrineType::getType('impossibleEnumCast')->convertToDatabaseValue(DoctrineIntEnum::FIRST(), new MySql80Platform());
$this->assertSame('1', $result, $result);
}

Expand Down
Loading

0 comments on commit 783afe9

Please sign in to comment.