Skip to content

Commit 609e302

Browse files
committed
Reorganise directory structure, and improve class naming
1 parent ebd8e9b commit 609e302

16 files changed

+86
-84
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ return [
2727

2828
## Features
2929

30-
- [Auto-Register Custom Doctrine Types](#auto-register-custom-doctrine-types)
31-
- [Auto-Register Carbon Doctrine Types](#auto-register-carbon-datetime-types)
30+
- [Auto-Register Custom Doctrine Type Mappings](#auto-register-custom-doctrine-types)
31+
- [Auto-Register Carbon Doctrine Type Mappings](#auto-register-carbon-datetime-types)
3232

3333
### Auto-Register Custom Doctrine Types
3434

@@ -44,12 +44,12 @@ headsnet_doctrine_tools:
4444
- 'src/Infra/Persistence/DBAL/Types'
4545
```
4646
47-
Then add the `#[CustomType]` attribute to the custom type class:
47+
Then add the `#[DoctrineTypeMapping]` attribute to the custom type class:
4848

4949
```php
5050
use Doctrine\DBAL\Types\Type;
5151
52-
#[CustomType]
52+
#[DoctrineTypeMapping]
5353
final class ReservationIdType extends Type
5454
{
5555
// defines "reservation_id" type
@@ -59,11 +59,11 @@ final class ReservationIdType extends Type
5959
This will register a custom type based on the class name - in this case the custom column type will be called
6060
`reservation_id`.
6161

62-
To customise the type name, specify it in the `#[CustomType]` attribute. The following will register a type
62+
To customise the type name, specify it in the `#[DoctrineTypeMapping]` attribute. The following will register a type
6363
called `my_reservation_id`.
6464

6565
```php
66-
#[CustomType(name: 'my_reservation_id')]
66+
#[DoctrineTypeMapping(name: 'my_reservation_id')]
6767
final class ReservationIdType extends Type
6868
{
6969
// customised name "my_reservation_id" type

ecs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
55
use PhpCsFixer\Fixer\Operator\NotOperatorWithSuccessorSpaceFixer;
66
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
7+
use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer;
78
use Symplify\EasyCodingStandard\Config\ECSConfig;
89
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
910

@@ -16,6 +17,7 @@
1617
$ecsConfig->skip([
1718
BlankLineAfterOpeningTagFixer::class,
1819
NotOperatorWithSuccessorSpaceFixer::class,
20+
MethodChainingIndentationFixer::class
1921
]);
2022

2123
$ecsConfig->rules([

src/CustomTypes/CustomType.php renamed to src/Attribute/DoctrineTypeMapping.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\CustomTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Attribute;
55

66
use Attribute;
77

88
#[Attribute(Attribute::TARGET_CLASS)]
9-
final class CustomType
9+
final class DoctrineTypeMapping
1010
{
1111
public string $name;
1212

src/HeadsnetDoctrineToolsBundle.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Headsnet\DoctrineToolsBundle;
44

5-
use Headsnet\DoctrineToolsBundle\CarbonTypes\CarbonTypesCompilerPass;
6-
use Headsnet\DoctrineToolsBundle\CustomTypes\CustomTypesCompilerPass;
5+
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
6+
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingsCompilerPass;
77
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
99
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@@ -15,22 +15,19 @@ public function configure(DefinitionConfigurator $definition): void
1515
{
1616
$definition->rootNode()
1717
->children()
18-
->arrayNode('custom_types')
19-
->children()
20-
->arrayNode('scan_dirs')
21-
->scalarPrototype()->end()
22-
->end()
23-
->end()
24-
->end() // End custom_types
25-
->arrayNode('carbon_types')
26-
->canBeDisabled()
27-
->children()
28-
->booleanNode('enabled')
29-
->defaultTrue()->end()
30-
->booleanNode('replace')
31-
->defaultTrue()->end()
32-
->end()
33-
->end() // End carbon_types
18+
->arrayNode('custom_types')
19+
->children()
20+
->arrayNode('scan_dirs')
21+
->scalarPrototype()->end()
22+
->end()
23+
->end()
24+
->end() // End custom_types
25+
->arrayNode('carbon_types')
26+
->canBeDisabled()
27+
->children()
28+
->booleanNode('replace')->defaultTrue()->end()
29+
->end()
30+
->end() // End carbon_types
3431
;
3532
}
3633

@@ -54,11 +51,11 @@ public function build(ContainerBuilder $container): void
5451
parent::build($container);
5552

5653
$container->addCompilerPass(
57-
new CustomTypesCompilerPass()
54+
new DoctrineTypeMappingsCompilerPass()
5855
);
5956

6057
$container->addCompilerPass(
61-
new CarbonTypesCompilerPass()
58+
new CarbonTypeMappingsCompilerPass()
6259
);
6360
}
6461
}

src/CarbonTypes/CarbonTypesCompilerPass.php renamed to src/Mapping/CarbonTypeMappingsCompilerPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\CarbonTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Mapping;
55

66
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
77
use Symfony\Component\DependencyInjection\ContainerBuilder;
88

99
/**
1010
* Automatically registers the custom Doctrine types provided by the Carbon library.
1111
*/
12-
final class CarbonTypesCompilerPass implements CompilerPassInterface
12+
final class CarbonTypeMappingsCompilerPass implements CompilerPassInterface
1313
{
1414
private const TYPE_DEFINITION_PARAMETER = 'doctrine.dbal.connection_factory.types';
1515

src/CustomTypes/CustomTypeNamer.php renamed to src/Mapping/DoctrineTypeMappingNamer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\CustomTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Mapping;
55

66
use Doctrine\DBAL\Types\Type;
7+
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
78
use ReflectionClass;
89

910
/**
1011
* If the #[CustomType] attribute has a name specified, use it.
1112
*
1213
* Otherwise derive it from the class name - e.g. "CarbonDateType" => "carbon_date"
1314
*/
14-
final class CustomTypeNamer
15+
final class DoctrineTypeMappingNamer
1516
{
1617
/**
1718
* @param ReflectionClass<Type> $reflection
1819
*/
1920
public static function getTypeName(ReflectionClass $reflection): string
2021
{
21-
$attribute = $reflection->getAttributes(CustomType::class)[0];
22+
$attribute = $reflection->getAttributes(DoctrineTypeMapping::class)[0];
2223

2324
$attributeArgs = $attribute->getArguments();
2425

src/CustomTypes/CustomTypesCompilerPass.php renamed to src/Mapping/DoctrineTypeMappingsCompilerPass.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\CustomTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Mapping;
55

66
use Doctrine\DBAL\Types\Type;
77
use Generator;
8+
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
89
use League\ConstructFinder\ConstructFinder;
910
use ReflectionClass;
1011
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -15,7 +16,7 @@
1516
*
1617
* This saves having to specify them all individually in the Doctrine configuration which is tedious.
1718
*/
18-
final class CustomTypesCompilerPass implements CompilerPassInterface
19+
final class DoctrineTypeMappingsCompilerPass implements CompilerPassInterface
1920
{
2021
private const TYPE_DEFINITION_PARAMETER = 'doctrine.dbal.connection_factory.types';
2122

@@ -72,9 +73,9 @@ private function findTypesInApplication(array $scanDirs): iterable
7273
}
7374

7475
// Only register types that have the #[CustomType] attribute
75-
if ($reflection->getAttributes(CustomType::class)) {
76+
if ($reflection->getAttributes(DoctrineTypeMapping::class)) {
7677
yield [
77-
'name' => CustomTypeNamer::getTypeName($reflection),
78+
'name' => DoctrineTypeMappingNamer::getTypeName($reflection),
7879
'class' => $className,
7980
];
8081
}

tests/CustomTypes/CustomTypeTest.php renamed to tests/Attribute/DoctrineTypeMappingTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\Tests\CustomTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Tests\Attribute;
55

6-
use Headsnet\DoctrineToolsBundle\CustomTypes\CustomType;
6+
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineTypeMapping;
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\Test;
99
use PHPUnit\Framework\TestCase;
1010

11-
#[CoversClass(CustomType::class)]
12-
class CustomTypeTest extends TestCase
11+
#[CoversClass(DoctrineTypeMapping::class)]
12+
class DoctrineTypeMappingTest extends TestCase
1313
{
1414
#[Test]
1515
public function name_can_be_specified(): void
1616
{
17-
$sut = new CustomType('custom_name');
17+
$sut = new DoctrineTypeMapping('custom_name');
1818

1919
$this->assertEquals('custom_name', $sut->name);
2020
}
2121

2222
#[Test]
2323
public function name_is_normalised(): void
2424
{
25-
$sut = new CustomType('some-custom Name');
25+
$sut = new DoctrineTypeMapping('some-custom Name');
2626

2727
$this->assertEquals('some_custom_name', $sut->name);
2828
}

tests/HeadsnetDoctrineToolsBundleTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33

44
namespace Headsnet\DoctrineToolsBundle\Tests;
55

6-
use Headsnet\DoctrineToolsBundle\CarbonTypes\CarbonTypesCompilerPass;
7-
use Headsnet\DoctrineToolsBundle\CustomTypes\CustomTypesCompilerPass;
86
use Headsnet\DoctrineToolsBundle\HeadsnetDoctrineToolsBundle;
7+
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
8+
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingsCompilerPass;
9+
use Headsnet\DoctrineToolsBundle\Types\DoctrineTypesCompilerPass;
910
use Nyholm\BundleTest\TestKernel;
1011
use PHPUnit\Framework\Attributes\CoversClass;
1112
use PHPUnit\Framework\Attributes\Test;
1213
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1314
use Symfony\Component\HttpKernel\KernelInterface;
1415

1516
#[CoversClass(HeadsnetDoctrineToolsBundle::class)]
16-
#[CoversClass(CarbonTypesCompilerPass::class)]
17-
#[CoversClass(CustomTypesCompilerPass::class)]
17+
#[CoversClass(CarbonTypeMappingsCompilerPass::class)]
18+
#[CoversClass(DoctrineTypeMappingsCompilerPass::class)]
1819
class HeadsnetDoctrineToolsBundleTest extends KernelTestCase
1920
{
2021
protected static function getKernelClass(): string

tests/CarbonTypes/RegisterCarbonTypesCompilerPassTest.php renamed to tests/Mapping/CarbonTypeMappingsCompilerPassTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\Tests\CarbonTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Tests\Mapping;
55

66
use Carbon\Doctrine\DateTimeImmutableType;
77
use Carbon\Doctrine\DateTimeType;
8-
use Headsnet\DoctrineToolsBundle\CarbonTypes\CarbonTypesCompilerPass;
8+
use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\Test;
1111
use PHPUnit\Framework\TestCase;
1212
use Symfony\Component\DependencyInjection\ContainerBuilder;
1313

14-
#[CoversClass(CarbonTypesCompilerPass::class)]
15-
class RegisterCarbonTypesCompilerPassTest extends TestCase
14+
#[CoversClass(CarbonTypeMappingsCompilerPass::class)]
15+
class CarbonTypeMappingsCompilerPassTest extends TestCase
1616
{
1717
#[Test]
1818
public function carbon_types_are_registered(): void
@@ -21,7 +21,7 @@ public function carbon_types_are_registered(): void
2121
$container->setParameter('doctrine.dbal.connection_factory.types', []);
2222
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', true);
2323
$container->setParameter('headsnet_doctrine_tools.carbon_types.replace', true);
24-
$sut = new CarbonTypesCompilerPass();
24+
$sut = new CarbonTypeMappingsCompilerPass();
2525

2626
$sut->process($container);
2727

@@ -44,7 +44,7 @@ public function carbon_types_are_registered_separately(): void
4444
$container->setParameter('doctrine.dbal.connection_factory.types', []);
4545
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', true);
4646
$container->setParameter('headsnet_doctrine_tools.carbon_types.replace', false);
47-
$sut = new CarbonTypesCompilerPass();
47+
$sut = new CarbonTypeMappingsCompilerPass();
4848

4949
$sut->process($container);
5050

@@ -66,7 +66,7 @@ public function if_disabled_then_register_nothing(): void
6666
$container = new ContainerBuilder();
6767
$container->setParameter('doctrine.dbal.connection_factory.types', []);
6868
$container->setParameter('headsnet_doctrine_tools.carbon_types.enabled', false);
69-
$sut = new CarbonTypesCompilerPass();
69+
$sut = new CarbonTypeMappingsCompilerPass();
7070

7171
$sut->process($container);
7272

tests/CustomTypes/CustomTypeNamerTest.php renamed to tests/Mapping/DoctrineTypeMappingNamerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Headsnet\DoctrineToolsBundle\Tests\CustomTypes;
4+
namespace Headsnet\DoctrineToolsBundle\Tests\Mapping;
55

6-
use Headsnet\DoctrineToolsBundle\CustomTypes\CustomTypeNamer;
7-
use Headsnet\DoctrineToolsBundle\Tests\CustomTypes\Fixtures\DummyCustomType;
8-
use Headsnet\DoctrineToolsBundle\Tests\CustomTypes\Fixtures\DummyCustomTypeWithName;
6+
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingNamer;
7+
use Headsnet\DoctrineToolsBundle\Tests\Mapping\Fixtures\DummyCustomType;
8+
use Headsnet\DoctrineToolsBundle\Tests\Mapping\Fixtures\DummyCustomTypeWithName;
99
use PHPUnit\Framework\Attributes\CoversClass;
1010
use PHPUnit\Framework\Attributes\Test;
1111
use PHPUnit\Framework\TestCase;
1212
use ReflectionClass;
1313

14-
#[CoversClass(CustomTypeNamer::class)]
15-
class CustomTypeNamerTest extends TestCase
14+
#[CoversClass(DoctrineTypeMappingNamer::class)]
15+
class DoctrineTypeMappingNamerTest extends TestCase
1616
{
1717
#[Test]
1818
public function default_name_derived_from_class(): void
1919
{
2020
$reflection = new ReflectionClass(new DummyCustomType());
2121

22-
$sut = CustomTypeNamer::getTypeName($reflection);
22+
$sut = DoctrineTypeMappingNamer::getTypeName($reflection);
2323

2424
$this->assertEquals('dummy_custom', $sut);
2525
}
@@ -29,7 +29,7 @@ public function name_can_be_specified(): void
2929
{
3030
$reflection = new ReflectionClass(new DummyCustomTypeWithName());
3131

32-
$sut = CustomTypeNamer::getTypeName($reflection);
32+
$sut = DoctrineTypeMappingNamer::getTypeName($reflection);
3333

3434
$this->assertEquals('my_custom_name', $sut);
3535
}

0 commit comments

Comments
 (0)