Skip to content
Open
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
108 changes: 0 additions & 108 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -648,24 +648,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 @@ -678,102 +660,12 @@ code = "missing-return-type"
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`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "class-must-be-final"
message = 'Class `PhpDb\Sql\Platform\Platform` should be declared `final`.'
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "mixed-array-assignment"
message = "Unsafe array assignment on type `mixed`."
count = 1

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "mixed-return-statement"
message = 'Could not infer a precise return type for function `PhpDb\Sql\Platform\Platform::getDecorators`. Saw type `mixed`.'
count = 1

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

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

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

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

[[issues]]
file = "src/Sql/Platform/Platform.php"
code = "possibly-undefined-string-array-index"
message = "Possibly undefined array key `string` accessed on `array<array-key, mixed>`."
count = 1

[[issues]]
file = "src/Sql/Platform/PlatformDecoratorInterface.php"
code = "missing-api-or-internal"
message = 'Interface `PhpDb\Sql\Platform\PlatformDecoratorInterface` is missing an `@api` or `@internal` annotation.'
count = 1

[[issues]]
file = "src/Sql/Select.php"
code = "missing-return-type"
Expand Down
18 changes: 0 additions & 18 deletions lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,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 All @@ -125,7 +132,7 @@
* @param SqlParameters $parameters
* @throws Exception\RuntimeException
*/
protected function createSqlFromSpecificationAndParameters(array|string $specifications, array $parameters): string

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

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * @PARAM SqlParameters $parameters * @throws Exception\RuntimeException */ - protected function createSqlFromSpecificationAndParameters(array|string $specifications, array $parameters): string + private function createSqlFromSpecificationAndParameters(array|string $specifications, array $parameters): string { if (is_string($specifications)) { return vsprintf($specifications, $this->flattenParameterList($parameters));
{
if (is_string($specifications)) {
return vsprintf($specifications, $this->flattenParameterList($parameters));
Expand All @@ -147,11 +154,11 @@
* @param list<ArgumentInterface> $arguments
* @return list<ArgumentInterface>
*/
protected function flattenExpressionValues(array $arguments): array

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

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * @PARAM list<ArgumentInterface> $arguments * @return list<ArgumentInterface> */ - protected function flattenExpressionValues(array $arguments): array + private function flattenExpressionValues(array $arguments): array { $hasValues = false; foreach ($arguments as $argument) {
{
$hasValues = false;

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

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "FalseValue": @@ @@ */ protected function flattenExpressionValues(array $arguments): array { - $hasValues = false; + $hasValues = true; foreach ($arguments as $argument) { if (! $argument instanceof Values) { continue;
foreach ($arguments as $argument) {
if (! $argument instanceof Values) {

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

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "LogicalNot": @@ @@ { $hasValues = false; foreach ($arguments as $argument) { - if (! $argument instanceof Values) { + if ($argument instanceof Values) { continue; }

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

View workflow job for this annotation

GitHub Actions / qa / Mutation testing

Escaped Mutant for Mutator "InstanceOf_": @@ @@ { $hasValues = false; foreach ($arguments as $argument) { - if (! $argument instanceof Values) { + if ($argument instanceof Values) { continue; }
continue;
}

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;
}
}
Loading
Loading