Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 3 additions & 57 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,6 @@ code = "missing-api-or-internal"
message = 'Interface `PhpDb\RowGateway\RowGatewayInterface` is missing an `@api` or `@internal` annotation.'
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "mixed-assignment"
message = "Assigning `mixed` type to a variable may lead to unexpected behavior."
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "string-member-selector"
message = "This member selector uses a non-literal string type (`non-empty-string`); its specific value cannot be statically determined."
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "string-member-selector"
message = "This member selector uses a non-literal string type (`string`); its specific value cannot be statically determined."
count = 1

[[issues]]
file = "src/Sql/Insert.php"
code = "missing-return-type"
Expand All @@ -301,45 +283,9 @@ message = "Function `__unset` is missing a return type hint."
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "imprecise-type"
message = "Type `array` in property `$decorators` is imprecise, equivalent to `array<array-key, mixed>`."
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "invalid-property-assignment-value"
message = 'Invalid type for property `$subject`: expected `PhpDb\Sql\PreparableSqlInterface|PhpDb\Sql\SqlInterface`, but got `PhpDb\Sql\PreparableSqlInterface|PhpDb\Sql\SqlInterface|null`.'
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "missing-constructor"
message = 'Class `PhpDb\Sql\Platform\AbstractPlatform` has typed properties without default values but no constructor to initialize them.'
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "non-existent-method"
message = 'Method `getSqlString` does not exist on type `PhpDb\Sql\Platform\PlatformDecoratorInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "non-existent-method"
message = 'Method `getSqlString` does not exist on type `PhpDb\Sql\PreparableSqlInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "non-existent-method"
message = 'Method `prepareStatement` does not exist on type `PhpDb\Sql\Platform\PlatformDecoratorInterface`.'
count = 1

[[issues]]
file = "src/Sql/Platform/AbstractPlatform.php"
code = "non-existent-method"
message = 'Method `prepareStatement` does not exist on type `PhpDb\Sql\SqlInterface`.'
file = "src/Sql/Platform/Platform.php"
code = "class-must-be-final"
message = 'Class `PhpDb\Sql\Platform\Platform` should be declared `final`.'
count = 1

[[issues]]
Expand Down
18 changes: 0 additions & 18 deletions lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,6 @@ code = "no-negated-ternary"
message = "Negated ternary condition."
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "cyclomatic-complexity"
message = "Class has high complexity."
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "excessive-parameter-list"
message = "Parameter list is too long."
count = 1

[[issues]]
file = "src/Sql/AbstractSql.php"
code = "kan-defect"
message = "Class has a high kan defect score."
count = 1

[[issues]]
file = "src/Sql/Combine.php"
code = "cyclomatic-complexity"
Expand Down
15 changes: 15 additions & 0 deletions src/Sql/AbstractSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
* isIdentifier?: bool,
* }
* @psalm-import-type JoinSpecification from Join
*
* @mago-expect lint:kan-defect
* @mago-expect lint:cyclomatic-complexity
*/
abstract class AbstractSql implements SqlInterface
{
Expand Down Expand Up @@ -82,13 +85,17 @@
{
$adapterPlatform ??= new DefaultAdapterPlatform();

$this->resetRenderState();

Check warning on line 88 in src/Sql/AbstractSql.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ { $adapterPlatform ??= new DefaultAdapterPlatform(); - $this->resetRenderState(); + return $this->buildSqlString($adapterPlatform); }

return $this->buildSqlString($adapterPlatform);
}

/**
* Dispatches each specification to its process{Name}() hook, so the method name is built at runtime
*
* @throws Exception\RuntimeException
*
* @mago-expect analysis:string-member-selector
*/
protected function buildSqlString(
PlatformInterface $platform,
Expand Down Expand Up @@ -117,7 +124,7 @@
}
}

return rtrim(implode(' ', $sqls), characters: "\n ,");

Check warning on line 127 in src/Sql/AbstractSql.php

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "UnwrapRtrim": @@ @@ } } - return rtrim(implode(' ', $sqls), characters: "\n ,"); + return implode(' ', $sqls); } /**
}

/**
Expand Down Expand Up @@ -200,6 +207,12 @@
return str_replace($placeholders, $values, $specification);
}

/**
* Copies the decorated subject's state onto this decorator, so property names are only known at runtime
*
* @mago-expect analysis:string-member-selector
* @mago-expect analysis:mixed-assignment
*/
protected function localizeVariables(): void
{
if (! $this instanceof PlatformDecoratorInterface || null === $this->subject) {
Expand Down Expand Up @@ -292,6 +305,8 @@

/**
* @throws Exception\RuntimeException
*
* @mago-expect lint:excessive-parameter-list
*/
protected function processExpressionOrSelect(
ArgumentInterface $argument,
Expand Down
9 changes: 9 additions & 0 deletions src/Sql/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

class RuntimeException extends Exception\RuntimeException
{
final public const string DECORATOR_NOT_IMPLEMENTING =
'The decorator %s does not implement %s,'
. ' thus %s cannot be delegated to it';

final public const string REPLACEMENT_MISMATCH =
'The number of replacements in the expression'
. ' does not match the number of parameters';
Expand Down Expand Up @@ -38,6 +42,11 @@ class RuntimeException extends Exception\RuntimeException
'%s renders its expression string and does not accept'
. ' a specification override; use setExpression() instead';

public static function forDecoratorNotImplementing(string $decorator, string $interface, string $method): self
{
return new self(sprintf(self::DECORATOR_NOT_IMPLEMENTING, $decorator, $interface, $method));
}

public static function forReplacementMismatch(): self
{
return new self(self::REPLACEMENT_MISMATCH);
Expand Down
47 changes: 41 additions & 6 deletions src/Sql/Platform/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@

class AbstractPlatform implements PlatformDecoratorInterface, PreparableSqlInterface, SqlInterface
{
protected SqlInterface|PreparableSqlInterface $subject;
protected SqlInterface|PreparableSqlInterface|null $subject = null;

/**
* Decorators keyed by the subject type they decorate; Platform nests them under a platform name first
*
* @var array<string, PlatformDecoratorInterface|array<string, PlatformDecoratorInterface>>
*/
protected array $decorators = [];

/**
* @return array|PlatformDecoratorInterface[]
* @return array<string, PlatformDecoratorInterface>
*/
public function getDecorators(): array
{
/** @var array<string, PlatformDecoratorInterface> */
return $this->decorators;
}

Expand All @@ -38,14 +44,19 @@ public function getSqlString(?PlatformInterface $adapterPlatform = null): string
throw Exception\RuntimeException::forSubjectNotImplementing(SqlInterface::class, 'getSqlString()');
}

return $this->getTypeDecorator($this->subject)->getSqlString($adapterPlatform);
return $this->requireDecoratorImplementing(
$this->getTypeDecorator($this->subject),
SqlInterface::class,
'getSqlString()',
)->getSqlString($adapterPlatform);
}

public function getTypeDecorator(
PreparableSqlInterface|SqlInterface $subject,
): PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface {
/** @var PlatformDecoratorInterface $decorator */
foreach ($this->decorators as $type => $decorator) {
/** @var array<string, PlatformDecoratorInterface> $decorators */
$decorators = $this->decorators;
foreach ($decorators as $type => $decorator) {
/** @phpstan-ignore-next-line instanceof with string class name is valid */
if (! $subject instanceof $type) {
continue;
Expand Down Expand Up @@ -73,7 +84,11 @@ public function prepareStatement(
);
}

$this->getTypeDecorator($this->subject)->prepareStatement($adapter, $statementContainer);
$this->requireDecoratorImplementing(
$this->getTypeDecorator($this->subject),
PreparableSqlInterface::class,
'prepareStatement()',
)->prepareStatement($adapter, $statementContainer);

return $statementContainer;
}
Expand All @@ -93,4 +108,24 @@ public function setTypeDecorator(string $type, PlatformDecoratorInterface $decor
{
$this->decorators[$type] = $decorator;
}

/**
* Narrows the resolved decorator to the interface whose method is about to be delegated to it
*
* @template T of PreparableSqlInterface|SqlInterface
* @param class-string<T> $interface
* @return T
* @throws Exception\RuntimeException
*/
final protected function requireDecoratorImplementing(
PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface $decorator,
string $interface,
string $method,
): PreparableSqlInterface|SqlInterface {
if (! $decorator instanceof $interface) {
throw Exception\RuntimeException::forDecoratorNotImplementing($decorator::class, $interface, $method);
}

return $decorator;
}
}
49 changes: 34 additions & 15 deletions src/Sql/Platform/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public function __construct(PlatformInterface $platform)
$this->defaultPlatform = $platform;
}

/**
* @return array<string, PlatformDecoratorInterface>
*/
#[Override]
public function getDecorators(): array
{
$platformName = $this->resolvePlatformName($this->getDefaultPlatform());

return $this->decorators[$platformName];
return $this->decoratorsFor($this->resolvePlatformName($this->getDefaultPlatform()));
}

/**
Expand All @@ -64,27 +65,27 @@ public function getDecorators(): array
#[Override]
public function getSqlString(?PlatformInterface $adapterPlatform = null): string
{
if (! $this->subject instanceof SqlInterface) {
$subject = $this->subject;
if (! $subject instanceof SqlInterface) {
throw Exception\RuntimeException::forSubjectNotImplementing(SqlInterface::class, 'getSqlString()');
}

$adapterPlatform = $this->resolvePlatform($adapterPlatform);

return $this->getTypeDecorator($this->subject, $adapterPlatform)->getSqlString($adapterPlatform);
return $this->requireDecoratorImplementing(
$this->getTypeDecorator($subject, $adapterPlatform),
SqlInterface::class,
'getSqlString()',
)->getSqlString($adapterPlatform);
}

#[Override]
public function getTypeDecorator(
PreparableSqlInterface|SqlInterface $subject,
AdapterInterface|PlatformInterface|null $adapterOrPlatform = null,
): PlatformDecoratorInterface|PreparableSqlInterface|SqlInterface {
$platformName = $this->resolvePlatformName($adapterOrPlatform);

/** @var array<class-string, PlatformDecoratorInterface>|null $decorators */
$decorators = $this->decorators[$platformName] ?? null;
if (null === $decorators) {
return $subject;
}
/** @var array<class-string, PlatformDecoratorInterface> $decorators */
$decorators = $this->decoratorsFor($this->resolvePlatformName($adapterOrPlatform));

$decorator = $decorators[$subject::class] ?? null;
if (null !== $decorator) {
Expand Down Expand Up @@ -121,7 +122,11 @@ public function prepareStatement(
);
}

$this->getTypeDecorator($this->subject, $adapter)->prepareStatement($adapter, $statementContainer);
$this->requireDecoratorImplementing(
$this->getTypeDecorator($this->subject, $adapter),
PreparableSqlInterface::class,
'prepareStatement()',
)->prepareStatement($adapter, $statementContainer);

return $statementContainer;
}
Expand All @@ -132,8 +137,11 @@ public function setTypeDecorator(
PlatformDecoratorInterface $decorator,
AdapterInterface|PlatformInterface|null $adapterOrPlatform = null,
): void {
$platformName = $this->resolvePlatformName($adapterOrPlatform);
$this->decorators[$platformName][$type] = $decorator;
$platformName = $this->resolvePlatformName($adapterOrPlatform);

$decorators = $this->decoratorsFor($platformName);
$decorators[$type] = $decorator;
$this->decorators[$platformName] = $decorators;
}

protected function getDefaultPlatform(): PlatformInterface
Expand Down Expand Up @@ -172,4 +180,15 @@ protected function resolvePlatformName(PlatformInterface|AdapterInterface|null $

return $normalized;
}

/**
* The decorators registered for a platform, keyed by the subject type they decorate
*
* @return array<string, PlatformDecoratorInterface>
*/
private function decoratorsFor(string $platformName): array
{
/** @var array<string, PlatformDecoratorInterface> */
return $this->decorators[$platformName] ?? [];
}
}
3 changes: 3 additions & 0 deletions src/Sql/Platform/PlatformDecoratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PhpDb\Sql\PreparableSqlInterface;
use PhpDb\Sql\SqlInterface;

/**
* @api
*/
interface PlatformDecoratorInterface
{
public function setSubject(
Expand Down
7 changes: 7 additions & 0 deletions test/unit/Sql/Exception/RuntimeExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpDb\Exception\ExceptionInterface;
use PhpDb\Sql\Exception\RuntimeException;
use PhpDb\Sql\Expression;
use PhpDb\Sql\Select;
use PhpDb\Sql\SqlInterface;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -17,6 +18,7 @@
use function sprintf;

#[Group('unit')]
#[CoversMethod(RuntimeException::class, 'forDecoratorNotImplementing')]
#[CoversMethod(RuntimeException::class, 'forReplacementMismatch')]
#[CoversMethod(RuntimeException::class, 'forStringSpecificationRequired')]
#[CoversMethod(RuntimeException::class, 'forSubjectNotImplementing')]
Expand All @@ -31,6 +33,11 @@ final class RuntimeExceptionTest extends TestCase
public static function namedConstructorProvider(): array
{
return [
'decorator not implementing' => [
'forDecoratorNotImplementing',
[Select::class, SqlInterface::class, 'getSqlString()'],
RuntimeException::DECORATOR_NOT_IMPLEMENTING,
],
'replacement mismatch' => [
'forReplacementMismatch',
[],
Expand Down
Loading
Loading