Skip to content

Commit 4515c33

Browse files
authored
Refactoring (#163)
1 parent e73ca51 commit 4515c33

18 files changed

+101
-62
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: CI
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
composer-validate:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v1
10+
11+
- name: Validate composer.json
12+
run: composer validate
13+
14+
composer-install:
15+
needs: composer-validate
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v1
19+
20+
- name: Install dependencies
21+
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction

DBAL/Types/AbstractEnumType.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,28 @@ public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $pla
9191
$values = \implode(
9292
', ',
9393
\array_map(
94-
static function ($value) {
94+
static function (string $value) {
9595
return "'{$value}'";
9696
},
9797
static::getValues()
9898
)
9999
);
100100

101-
if ($platform instanceof SqlitePlatform) {
102-
return \sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
103-
}
101+
switch (true) {
102+
case $platform instanceof SqlitePlatform:
103+
$sqlDeclaration = \sprintf('TEXT CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
104+
105+
break;
106+
case $platform instanceof PostgreSqlPlatform:
107+
case $platform instanceof SQLServerPlatform:
108+
$sqlDeclaration = \sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
104109

105-
if ($platform instanceof PostgreSqlPlatform || $platform instanceof SQLServerPlatform) {
106-
return \sprintf('VARCHAR(255) CHECK(%s IN (%s))', $fieldDeclaration['name'], $values);
110+
break;
111+
default:
112+
$sqlDeclaration = \sprintf('ENUM(%s)', $values);
107113
}
108114

109-
return \sprintf('ENUM(%s)', $values);
115+
return $sqlDeclaration;
110116
}
111117

112118
/**

Form/EnumTypeGuesser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Fresh\DoctrineEnumBundle\Form;
1414

15-
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\Persistence\ManagerRegistry;
1616
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
1717
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException;
1818
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
@@ -29,7 +29,7 @@
2929
class EnumTypeGuesser extends DoctrineOrmTypeGuesser
3030
{
3131
/** @var string[] */
32-
protected $registeredEnumTypes = [];
32+
private $registeredEnumTypes = [];
3333

3434
/**
3535
* @param ManagerRegistry $registry

FreshDoctrineEnumBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Fresh\DoctrineEnumBundle;
1212

13-
use Doctrine\Common\Persistence\ManagerRegistry;
13+
use Doctrine\Persistence\ManagerRegistry;
1414
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
1616
use Symfony\Component\HttpKernel\Bundle\Bundle;

Tests/DBAL/Types/AbstractEnumTypeTest.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function setUpBeforeClass(): void
4343
Type::addType('NumericType', NumericType::class);
4444
}
4545

46-
public function setUp(): void
46+
protected function setUp(): void
4747
{
4848
$this->type = Type::getType('BasketballPositionType');
4949
}
@@ -61,29 +61,27 @@ public function testGetSqlDeclaration(array $fieldDeclaration, AbstractPlatform
6161
self::assertEquals($expected, $this->type->getSqlDeclaration($fieldDeclaration, $platform));
6262
}
6363

64-
public function platformProvider(): array
65-
{
66-
return [
67-
[
68-
['name' => 'position'],
69-
new MySqlPlatform(),
70-
"ENUM('PG', 'SG', 'SF', 'PF', 'C')",
71-
],
72-
[
73-
['name' => 'position'],
74-
new SqlitePlatform(),
75-
"TEXT CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
76-
],
77-
[
78-
['name' => 'position'],
79-
new PostgreSqlPlatform(),
80-
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
81-
],
82-
[
83-
['name' => 'position'],
84-
new SQLServerPlatform(),
85-
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
86-
],
64+
public static function platformProvider(): iterable
65+
{
66+
yield [
67+
['name' => 'position'],
68+
new MySqlPlatform(),
69+
"ENUM('PG', 'SG', 'SF', 'PF', 'C')",
70+
];
71+
yield [
72+
['name' => 'position'],
73+
new SqlitePlatform(),
74+
"TEXT CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
75+
];
76+
yield [
77+
['name' => 'position'],
78+
new PostgreSqlPlatform(),
79+
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
80+
];
81+
yield [
82+
['name' => 'position'],
83+
new SQLServerPlatform(),
84+
"VARCHAR(255) CHECK(position IN ('PG', 'SG', 'SF', 'PF', 'C'))",
8785
];
8886
}
8987

Tests/Fixtures/DBAL/Types/BasketballPositionType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class BasketballPositionType extends AbstractEnumType
2727
public const POWER_FORWARD = 'PF';
2828
public const CENTER = 'C';
2929

30+
/** @var string */
3031
protected $name = 'BasketballPositionType';
3132

3233
protected static $choices = [

Tests/Fixtures/DBAL/Types/InheritedType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020
final class InheritedType extends AbstractParentType
2121
{
22+
/** @var string */
2223
protected $name = 'InheritedType';
2324

2425
/**

Tests/Fixtures/DBAL/Types/MapLocationType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ final class MapLocationType extends AbstractEnumType
3131
public const SOUTH_WEST = 'SW';
3232
public const SOUTH_EAST = 'SE';
3333

34+
/** @var string */
3435
protected $name = 'MapLocationType';
3536

3637
protected static $choices = [

Tests/Fixtures/DBAL/Types/NumericType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class NumericType extends AbstractEnumType
2727
public const THREE = 3;
2828
public const FOUR = 4;
2929

30+
/** @var string */
3031
protected $name = 'NumericType';
3132

3233
protected static $choices = [

Tests/Form/EnumTypeGuesserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
namespace Fresh\DoctrineEnumBundle\Tests\Form;
1414

15-
use Doctrine\Common\Persistence\ManagerRegistry;
1615
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16+
use Doctrine\Persistence\ManagerRegistry;
1717
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException;
1818
use Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser;
1919
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;

Tests/FreshDoctrineEnumBundleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
namespace Fresh\DoctrineEnumBundle\Tests;
1414

15-
use Doctrine\Common\Persistence\ManagerRegistry;
1615
use Doctrine\DBAL\Connection;
1716
use Doctrine\DBAL\Platforms\AbstractPlatform;
17+
use Doctrine\Persistence\ManagerRegistry;
1818
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
1919
use Fresh\DoctrineEnumBundle\FreshDoctrineEnumBundle;
2020
use PHPUnit\Framework\MockObject\MockObject;

Tests/Twig/Extension/EnumConstantExtensionTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class EnumConstantExtensionTest extends TestCase
3333
/** @var EnumConstantTwigExtension */
3434
private $enumConstantExtension;
3535

36-
public function setUp(): void
36+
protected function setUp(): void
3737
{
3838
$this->enumConstantExtension = new EnumConstantTwigExtension([
3939
'BasketballPositionType' => ['class' => BasketballPositionType::class],
@@ -66,15 +66,13 @@ public function testGetEnumConstant(string $expectedValueOfConstant, string $enu
6666
);
6767
}
6868

69-
public function dataProviderForGetEnumConstantTest(): array
69+
public function dataProviderForGetEnumConstantTest(): iterable
7070
{
71-
return [
72-
['PG', 'POINT_GUARD', 'BasketballPositionType'],
73-
['PG', 'POINT_GUARD', null],
74-
['C', 'CENTER', 'BasketballPositionType'],
75-
['C', 'CENTER', 'MapLocationType'],
76-
['3', 'THREE', 'NumericType'],
77-
];
71+
yield ['PG', 'POINT_GUARD', 'BasketballPositionType'];
72+
yield ['PG', 'POINT_GUARD', null];
73+
yield ['C', 'CENTER', 'BasketballPositionType'];
74+
yield ['C', 'CENTER', 'MapLocationType'];
75+
yield ['3', 'THREE', 'NumericType'];
7876
}
7977

8078
public function testEnumTypeIsNotRegisteredException(): void

Tests/Twig/Extension/EnumValuesAsArrayExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class EnumValuesAsArrayExtensionTest extends TestCase
3030
/** @var EnumValuesAsArrayTwigExtension */
3131
private $enumValuesAsArrayTwigExtension;
3232

33-
public function setUp(): void
33+
protected function setUp(): void
3434
{
3535
$this->enumValuesAsArrayTwigExtension = new EnumValuesAsArrayTwigExtension([
3636
'BasketballPositionType' => ['class' => BasketballPositionType::class],

Tests/Twig/Extension/ReadableEnumValueExtensionTest.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class ReadableEnumValueExtensionTest extends TestCase
3232
/** @var ReadableEnumValueTwigExtension */
3333
private $readableEnumValueExtension;
3434

35-
public function setUp(): void
35+
protected function setUp(): void
3636
{
3737
$this->readableEnumValueExtension = new ReadableEnumValueTwigExtension([
3838
'BasketballPositionType' => ['class' => BasketballPositionType::class],
@@ -53,9 +53,7 @@ public function testGetFilters(): void
5353
);
5454
}
5555

56-
/**
57-
* @dataProvider dataProviderForGetReadableEnumValueTest
58-
*/
56+
/** @dataProvider dataProviderForGetReadableEnumValueTest */
5957
public function testGetReadableEnumValue(?string $expectedReadableValue, ?string $enumValue, ?string $enumType): void
6058
{
6159
self::assertEquals(
@@ -64,15 +62,13 @@ public function testGetReadableEnumValue(?string $expectedReadableValue, ?string
6462
);
6563
}
6664

67-
public function dataProviderForGetReadableEnumValueTest(): array
65+
public function dataProviderForGetReadableEnumValueTest(): iterable
6866
{
69-
return [
70-
['Point Guard', BasketballPositionType::POINT_GUARD, 'BasketballPositionType'],
71-
['Point Guard', BasketballPositionType::POINT_GUARD, null],
72-
['Center', BasketballPositionType::CENTER, 'BasketballPositionType'],
73-
['Center', MapLocationType::CENTER, 'MapLocationType'],
74-
[null, null, 'MapLocationType'],
75-
];
67+
yield ['Point Guard', BasketballPositionType::POINT_GUARD, 'BasketballPositionType'];
68+
yield ['Point Guard', BasketballPositionType::POINT_GUARD, null];
69+
yield ['Center', BasketballPositionType::CENTER, 'BasketballPositionType'];
70+
yield ['Center', MapLocationType::CENTER, 'MapLocationType'];
71+
yield [null, null, 'MapLocationType'];
7672
}
7773

7874
public function testEnumTypeIsNotRegisteredException(): void

Tests/Validator/EnumValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class EnumValidatorTest extends TestCase
3535
/** @var ExecutionContext|MockObject */
3636
private $context;
3737

38-
public function setUp(): void
38+
protected function setUp(): void
3939
{
4040
$this->enumValidator = new EnumValidator();
4141
$this->context = $this->createMock(ExecutionContext::class);

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"require": {
2525
"php": ">=7.2.5",
26-
"doctrine/doctrine-bundle": "^2.0",
2726
"doctrine/common": "^2.11",
27+
"doctrine/doctrine-bundle": "^2.0",
2828
"doctrine/orm": "^2.7",
2929
"symfony/config": "^5.0",
3030
"symfony/dependency-injection": "^5.0",
@@ -36,10 +36,13 @@
3636
},
3737
"require-dev": {
3838
"escapestudios/symfony2-coding-standard": "^3.10",
39+
"johnkary/phpunit-speedtrap": "^3.1",
3940
"phpstan/phpstan": "^0.12",
4041
"phpstan/phpstan-doctrine":"^0.12",
4142
"phpstan/phpstan-phpunit": "^0.12",
43+
"phpstan/phpstan-symfony": "^0.12",
4244
"phpunit/phpunit": "^8.5",
45+
"slam/phpstan-extensions": "^4.0",
4346
"symfony/form": "^5.0",
4447
"symfony/phpunit-bridge": "^5.0",
4548
"symfony/yaml": "^5.0"
@@ -57,6 +60,9 @@
5760
"Fresh\\DoctrineEnumBundle\\Tests\\": "Tests/"
5861
}
5962
},
63+
"config": {
64+
"sort-packages": true
65+
},
6066
"extra": {
6167
"branch-alias": {
6268
"dev-master": "7.x-dev"

phpstan.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ includes:
22
- 'vendor/phpstan/phpstan-phpunit/extension.neon'
33
- 'vendor/phpstan/phpstan-phpunit/rules.neon'
44
- 'vendor/phpstan/phpstan-doctrine/extension.neon'
5+
- 'vendor/phpstan/phpstan-symfony/extension.neon'
6+
- 'vendor/slam/phpstan-extensions/conf/slam-rules.neon'
57
parameters:
6-
level: 7
8+
level: 8
79
excludes_analyse:
810
- '%rootDir%/../../../Tests/*'
911
- '%rootDir%/../../../vendor/*'

phpunit.xml.dist

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

33
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.4/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.5/phpunit.xsd"
55
colors="true"
66
bootstrap="vendor/autoload.php"
77
>
8+
<php>
9+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
10+
</php>
11+
12+
<listeners>
13+
<listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
14+
</listeners>
15+
816
<testsuites>
917
<testsuite name="DoctrineEnumBundle Test Suite">
1018
<directory>./Tests</directory>

0 commit comments

Comments
 (0)