Skip to content

Commit

Permalink
Refactoring. Fix nullableTimestamps type
Browse files Browse the repository at this point in the history
  • Loading branch information
albertoarena committed Nov 2, 2024
1 parent c944f56 commit 981a812
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Console/Commands/MakeEventSourcingDomainCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function loadProperties(): void
$migration = (new Migration($this->settings->migration));
foreach ($migration->properties() as $property) {
$this->settings->modelProperties->add($property);
if ($property->type->toString() === 'Carbon') {
if ($property->type->type === 'Carbon') {
$this->settings->useCarbon = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Blueprint/Concerns/HasBlueprintColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function columnTypeToBuiltInType(string $type): string
protected function carbonToBuiltInType(string $type): string
{
return match ($type) {
'Carbon' => 'date:Y-m-d H:i:s',
'Carbon', '?Carbon' => 'date:Y-m-d H:i:s',
'Carbon:Y-m-d' => 'date:Y-m-d',
'Carbon:H:i:s' => 'date:H:i:s',
default => $type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ interface BlueprintUnsupportedInterface
'uuidMorphs',
'ulid',
];

public const IGNORED = [...self::SKIPPED_METHODS, ...self::UNSUPPORTED_COLUMN_TYPES];
}
2 changes: 1 addition & 1 deletion src/Domain/PhpParser/Concerns/HasMethodNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait HasMethodNode
{
protected function enterMethodNode(Node $node, string $method, EnterNode $enterNode): ?Node
{
// Check if Schema::up() method exists
// Check if method exists
if ($node instanceof Node\Stmt\Class_) {
$currentMethod = $node->getMethod($method);
if (! $currentMethod) {
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/PhpParser/Models/MigrationCreateProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
new MigrationCreatePropertyType($type);

if (! $this->name) {
$this->name = $this->type->toString();
$this->name = $this->type->type;
$this->type->setAsBuiltInType();
}
}
Expand Down
21 changes: 8 additions & 13 deletions src/Domain/PhpParser/Models/MigrationCreatePropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,31 @@
namespace Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Models;

use Albertoarena\LaravelEventSourcingGenerator\Domain\Blueprint\Concerns\HasBlueprintColumnType;
use Albertoarena\LaravelEventSourcingGenerator\Domain\Blueprint\Contracts\BlueprintUnsupportedInterface;
use Aldemeery\Onion\Onion;
use Illuminate\Support\Str;

class MigrationCreatePropertyType
{
use HasBlueprintColumnType;

public readonly bool $nullable;
public bool $nullable;

public function __construct(
public string $type,
bool $nullable = false,
) {
$this->nullable = false;
if ($nullable) {
$this->nullable = $nullable;
} else {
// Check if built-in type is nullable (e.g. nullableTimestamps)
if (Str::startsWith($this->columnTypeToBuiltInType($type), '?')) {
$this->nullable = true;
}
// Check if type starts with ? (nullable)
else {
$this->nullable = Str::startsWith($type, '?');
if ($this->nullable) {
$this->type = Str::substr($this->type, 1);
}
}
}
}

public function toString(): string
{
return $this->type;
}

public function setAsBuiltInType(): void
{
$this->type = $this->columnTypeToBuiltInType($this->type);
Expand Down Expand Up @@ -67,4 +57,9 @@ public function toProjection(): string
fn ($type) => Str::replaceFirst('?', '', $type),
]))->peel($this->type);
}

public function isIgnored(): bool
{
return in_array($this->type, array_merge(BlueprintUnsupportedInterface::IGNORED));
}
}
12 changes: 2 additions & 10 deletions src/Domain/PhpParser/Traversers/BlueprintClassNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Traversers;

use Albertoarena\LaravelEventSourcingGenerator\Domain\Blueprint\Contracts\BlueprintUnsupportedInterface;
use Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Concerns\HasSchemaUpNode;
use Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Models\EnterNode;
use Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Models\MigrationCreateProperty;
Expand All @@ -14,16 +13,9 @@ class BlueprintClassNodeVisitor extends NodeVisitorAbstract
{
use HasSchemaUpNode;

protected array $ignored;

public function __construct(
protected array &$properties
) {
$this->ignored = array_merge(
BlueprintUnsupportedInterface::SKIPPED_METHODS,
BlueprintUnsupportedInterface::UNSUPPORTED_COLUMN_TYPES
);
}
) {}

/**
* @throws UpdateMigrationIsNotSupportedException
Expand All @@ -47,7 +39,7 @@ function (Node $node) {
// Collect properties from Schema::up method
if ($node->expr instanceof Node\Expr\MethodCall) {
$property = MigrationCreateProperty::createFromExprMethodCall($node->expr);
if (! in_array($property->type->toString(), $this->ignored)) {
if (! $property->type->isIgnored()) {
$this->properties[$property->name] = $property;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Tests\Unit\Domain\PhpParser\Models;

use Albertoarena\LaravelEventSourcingGenerator\Domain\PhpParser\Models\MigrationCreatePropertyType;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

class MigrationCreatePropertyTypeTest extends TestCase
{
#[Test]
public function can_create_property_type_from_string()
{
$obj = new MigrationCreatePropertyType('string');
$this->assertEquals('string', $obj->type);
$this->assertFalse($obj->nullable);
$this->assertEquals('string', $obj->toBuiltInType());
$this->assertEquals('string', $obj->toNormalisedBuiltInType());
$this->assertEquals('string', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_nullable_type()
{
$obj = new MigrationCreatePropertyType('?string');
$this->assertEquals('?string', $obj->type);
$this->assertTrue($obj->nullable);
$this->assertEquals('string', $obj->toBuiltInType());
$this->assertEquals('string', $obj->toNormalisedBuiltInType());
$this->assertEquals('string', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_string_with_nullable()
{
$obj = new MigrationCreatePropertyType('string', true);
$this->assertEquals('string', $obj->type);
$this->assertTrue($obj->nullable);
$this->assertEquals('string', $obj->toBuiltInType());
$this->assertEquals('string', $obj->toNormalisedBuiltInType());
$this->assertEquals('string', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_nullable_string_with_nullable()
{
$obj = new MigrationCreatePropertyType('?string', true);
$this->assertEquals('?string', $obj->type);
$this->assertTrue($obj->nullable);
$this->assertEquals('string', $obj->toBuiltInType());
$this->assertEquals('string', $obj->toNormalisedBuiltInType());
$this->assertEquals('string', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_nullable_custom_type()
{
$obj = new MigrationCreatePropertyType('?custom');
$this->assertEquals('?custom', $obj->type);
$this->assertTrue($obj->nullable);
$this->assertEquals('custom', $obj->toBuiltInType());
$this->assertEquals('custom', $obj->toNormalisedBuiltInType());
$this->assertEquals('custom', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_date()
{
$obj = new MigrationCreatePropertyType('date');
$this->assertEquals('date', $obj->type);
$this->assertFalse($obj->nullable);
$this->assertEquals('Carbon:Y-m-d', $obj->toBuiltInType());
$this->assertEquals('Carbon', $obj->toNormalisedBuiltInType());
$this->assertEquals('date:Y-m-d', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_time()
{
$obj = new MigrationCreatePropertyType('time');
$this->assertEquals('time', $obj->type);
$this->assertFalse($obj->nullable);
$this->assertEquals('Carbon:H:i:s', $obj->toBuiltInType());
$this->assertEquals('Carbon', $obj->toNormalisedBuiltInType());
$this->assertEquals('date:H:i:s', $obj->toProjection());
}

#[Test]
public function can_create_property_type_from_nullableTimestamps()
{
$obj = new MigrationCreatePropertyType('nullableTimestamps');
$this->assertEquals('nullableTimestamps', $obj->type);
$this->assertTrue($obj->nullable);
$this->assertEquals('Carbon', $obj->toBuiltInType());
$this->assertEquals('Carbon', $obj->toNormalisedBuiltInType());
$this->assertEquals('date:Y-m-d H:i:s', $obj->toProjection());
}
}

0 comments on commit 981a812

Please sign in to comment.