Skip to content

Commit

Permalink
refactored manipulation method compilers into a single one
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkulhan committed May 29, 2024
1 parent 399b000 commit cf9027f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 114 deletions.
5 changes: 5 additions & 0 deletions data-access-kit/src/Repository/Attribute/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
#[Attribute(Attribute::TARGET_METHOD)]
class Update
{
public function __construct(
public readonly ?array $columns = null,
)
{
}
}
4 changes: 2 additions & 2 deletions data-access-kit/src/Repository/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function __construct(
$this->registerMethodCompiler(Delegate::class, new DelegateMethodCompiler());
$this->registerMethodCompiler(Insert::class, $manipulationMethodCompiler = new ManipulationMethodCompiler());
$this->registerMethodCompiler(Upsert::class, $manipulationMethodCompiler);
$this->registerMethodCompiler(Update::class, new UpdateMethodCompiler());
$this->registerMethodCompiler(Delete::class, new DeleteMethodCompiler());
$this->registerMethodCompiler(Update::class, $manipulationMethodCompiler);
$this->registerMethodCompiler(Delete::class, $manipulationMethodCompiler);
}

/**
Expand Down
56 changes: 0 additions & 56 deletions data-access-kit/src/Repository/Method/DeleteMethodCompiler.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace DataAccessKit\Repository\Method;

use DataAccessKit\Repository\Attribute\Delete;
use DataAccessKit\Repository\Attribute\Insert;
use DataAccessKit\Repository\Attribute\Update;
use DataAccessKit\Repository\Attribute\Upsert;
use DataAccessKit\Repository\Compiler;
use DataAccessKit\Repository\Exception\CompilerException;
Expand All @@ -11,13 +13,12 @@
use DataAccessKit\Repository\ResultMethod;
use ReflectionNamedType;
use function get_class;
use function in_array;
use function property_exists;
use function sprintf;
use function ucfirst;

/**
* @implements MethodCompilerInterface<Insert|Upsert>
* @implements MethodCompilerInterface<Insert|Upsert|Update|Delete>
*/
class ManipulationMethodCompiler implements MethodCompilerInterface
{
Expand All @@ -28,6 +29,8 @@ public function compile(Result $result, ResultMethod $method, $attribute): void
$persistenceMethod = match (true) {
$attribute instanceof Insert => "insert",
$attribute instanceof Upsert => "upsert",
$attribute instanceof Update => "update",
$attribute instanceof Delete => "delete",
default => throw new CompilerException(sprintf(
"Unexpected attribute of type [%s].",
get_class($attribute),
Expand All @@ -53,13 +56,19 @@ public function compile(Result $result, ResultMethod $method, $attribute): void
}

$rp = $method->reflection->getParameters()[0];
if (!$rp->getType() instanceof ReflectionNamedType || !in_array($rp->getType()->getName(), ["array", $result->repository->class], true)) {
if (!$rp->getType() instanceof ReflectionNamedType ||
!(
$rp->getType()->getName() === $result->repository->class ||
($rp->getType()->getName() === "array" && !$attribute instanceof Update)
)
) {
throw new CompilerException(sprintf(
"%s method [%s::%s] must have exactly one parameter with type [%s] or array.",
"%s method [%s::%s] must have exactly one parameter with type [%s]%s.",
ucfirst($persistenceMethod),
$result->reflection->getName(),
$method->reflection->getName(),
$result->repository->class,
$attribute instanceof Update ? "" : " or array",
));
}

Expand Down
52 changes: 0 additions & 52 deletions data-access-kit/src/Repository/Method/UpdateMethodCompiler.php

This file was deleted.

2 changes: 2 additions & 0 deletions data-access-kit/test/Repository/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
use Spatie\Snapshots\MatchesSnapshots;
use function dirname;
use function lcfirst;
use function sprintf;
use function str_replace;
use function strrpos;
use function substr;
Expand Down Expand Up @@ -138,6 +139,7 @@ public function testCompileError(string $interfaceName): void
{
try {
$this->compiler->compile($this->compiler->prepare($interfaceName));
$this->fail(sprintf("Expected [%s] to be thrown.", CompilerException::class));
} catch (CompilerException $e) {
$this->assertMatchesSnapshot($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace DataAccessKit\Repository\Fixture;

use DataAccessKit\Repository\Attribute\Repository;
use DataAccessKit\Repository\Attribute\Update;

#[Repository(Foo::class)]
interface UpdateRepositoryInterface
{
public function update(Foo $foo): void;
#[Update(["title"])]
public function updateTitleOnly(Foo $foo): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ final class UpdateRepository implements UpdateRepositoryInterface
{
$this->persistence->update($foo);
}

public function updateTitleOnly(
Foo $foo,
): void
{
$this->persistence->update($foo, ['title']);
}
}

0 comments on commit cf9027f

Please sign in to comment.