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
164 changes: 1 addition & 163 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
@@ -1,131 +1,5 @@
variant = "loose"

[[issues]]
file = "src/Adapter/AdapterInterface.php"
code = "missing-return-type"
message = "Function `getHelpers` is missing a return type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/AbstractConnection.php"
code = "missing-property-type"
message = "Property `$resource` is missing a type hint."
count = 1

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

[[issues]]
file = "src/Adapter/Driver/ConnectionInterface.php"
code = "missing-return-type"
message = "Function `getResource` is missing a return type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/AbstractPdo.php"
code = "overly-wide-return-type"
message = 'Declared return type `PhpDb\Adapter\Driver\ResultInterface|null` for `getresultprototype` has unused branches: `null`.'
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "incompatible-return-type"
message = 'Return type `mixed` of `PhpDb\Adapter\Driver\Pdo\Result::next()` is incompatible with parent return type `void` of `Iterator::next()`'
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-parameter-type"
message = "Parameter `$generatedValue` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$currentComplete` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$currentData` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$fetchMode` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$generatedValue` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$options` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$position` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/Pdo/Result.php"
code = "missing-property-type"
message = "Property `$resource` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Driver/StatementInterface.php"
code = "missing-return-type"
message = "Function `getResource` is missing a return type hint."
count = 1

[[issues]]
file = "src/Adapter/ParameterContainer.php"
code = "incompatible-parameter-name"
message = 'Parameter #1 of `PhpDb\Adapter\ParameterContainer::offsetexists()` is named `$name` but parent `ArrayAccess::offsetexists()` names it `$offset`'
count = 1

[[issues]]
file = "src/Adapter/ParameterContainer.php"
code = "incompatible-parameter-name"
message = 'Parameter #1 of `PhpDb\Adapter\ParameterContainer::offsetget()` is named `$name` but parent `ArrayAccess::offsetget()` names it `$offset`'
count = 1

[[issues]]
file = "src/Adapter/ParameterContainer.php"
code = "incompatible-parameter-name"
message = 'Parameter #1 of `PhpDb\Adapter\ParameterContainer::offsetset()` is named `$name` but parent `ArrayAccess::offsetset()` names it `$offset`'
count = 1

[[issues]]
file = "src/Adapter/ParameterContainer.php"
code = "incompatible-parameter-name"
message = 'Parameter #1 of `PhpDb\Adapter\ParameterContainer::offsetunset()` is named `$name` but parent `ArrayAccess::offsetunset()` names it `$offset`'
count = 1

[[issues]]
file = "src/Adapter/Profiler/Profiler.php"
code = "missing-property-type"
message = "Property `$currentIndex` is missing a type hint."
count = 1

[[issues]]
file = "src/Adapter/Profiler/Profiler.php"
code = "missing-property-type"
message = "Property `$profiles` is missing a type hint."
count = 1

[[issues]]
file = "src/ConfigProvider.php"
code = "imprecise-type"
Expand Down Expand Up @@ -156,12 +30,6 @@ code = "imprecise-type"
message = "Type `array` in return type of `getConfig` is imprecise, equivalent to `array<array-key, mixed>`."
count = 1

[[issues]]
file = "src/Container/AbstractAdapterInterfaceFactory.php"
code = "incompatible-parameter-type"
message = 'Parameter `$container` of `PhpDb\Container\AbstractAdapterInterfaceFactory::__invoke()` expects type `Psr\Container\ContainerInterface&Laminas\ServiceManager\ServiceManager` but parent `Laminas\ServiceManager\Factory\FactoryInterface::__invoke()` expects type `Psr\Container\ContainerInterface`'
count = 1

[[issues]]
file = "src/Container/AbstractAdapterInterfaceFactory.php"
code = "invalid-return-statement"
Expand Down Expand Up @@ -240,12 +108,6 @@ code = "unhandled-thrown-type"
message = 'Potentially unhandled exception `Psr\Container\NotFoundExceptionInterface` in `PhpDb\Container\AbstractAdapterInterfaceFactory::getConfig`.'
count = 1

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

[[issues]]
file = "src/Container/AdapterInterfaceDelegator.php"
code = "imprecise-type"
Expand Down Expand Up @@ -291,7 +153,7 @@ count = 2
[[issues]]
file = "src/Container/AdapterInterfaceFactory.php"
code = "possibly-null-argument"
message = 'Argument #1 of method `Laminas\ServiceManager\ServiceManager::build` is possibly `null`, but parameter type `class-string<PhpDb\Adapter\Driver\DriverInterface>|class-string<PhpDb\Adapter\Driver\PdoDriverInterface>|string` does not accept it.'
message = 'Argument #1 of method `Laminas\ServiceManager\ServiceManager::build` is possibly `null`, but parameter type `class-string<PhpDb\Adapter\Driver\DriverInterface<object|resource, object|resource, object|resource>>|class-string<PhpDb\Adapter\Driver\PdoDriverInterface>|string` does not accept it.'
count = 1

[[issues]]
Expand Down Expand Up @@ -342,30 +204,12 @@ code = "incompatible-return-type"
message = 'Return type `int|null` of `PhpDb\ResultSet\AbstractResultSet::count()` is incompatible with parent return type `int` of `Countable::count()`'
count = 1

[[issues]]
file = "src/ResultSet/AbstractResultSet.php"
code = "invalid-type-cast"
message = "Casting `mixed` to `array`."
count = 1

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

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

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

[[issues]]
file = "src/ResultSet/HydratingResultSet.php"
code = "redundant-docblock-type"
Expand Down Expand Up @@ -720,12 +564,6 @@ 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"
Expand Down
33 changes: 33 additions & 0 deletions docs/book/adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,39 @@ delegates to this method rather than duplicating the clone-and-initialize
logic itself; call it yourself when you want a `ResultSet` from
`executeQuery()`'s raw `Driver\ResultInterface`.

### Native Resource Types

The native handles that the driver objects pass around are declared as docblock
generics, so each driver can state its real types instead of a generic
`resource`. Each template defaults to `object|resource`, so code that uses the
interfaces without type arguments keeps the broad type.

| Interface | Template | Typed members |
|-----------------------|------------------------------------------|----------------------------------------------------------|
| `ConnectionInterface` | `TConnection` | `getResource()` |
| `StatementInterface` | `TStatement` | `getResource()` |
| `ResultInterface` | `TResult` | `getResource()` |
| `DriverInterface` | `TConnection`, `TStatement`, `TResult` | `getConnection()`, `createStatement()`, `createResult()` |

A driver package declares its types with `@implements`:

```php title="Declaring a Driver's Native Types"
/** @implements DriverInterface<mysqli, mysqli_stmt, mysqli|mysqli_result|mysqli_stmt> */
final class Driver implements DriverInterface
{
// ...
}

/** @implements StatementInterface<mysqli_stmt> */
final class Statement implements StatementInterface
{
// ...
}
```

The PDO drivers get `PDO`, `PDOStatement` and `PDOStatement` through
`PdoDriverInterface` and `PdoConnectionInterface`.

## Using The Platform Object

The `Platform` object provides an API to assist in crafting queries in a way
Expand Down
2 changes: 1 addition & 1 deletion lint-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ count = 1
file = "src/ResultSet/AbstractResultSet.php"
code = "halstead"
message = "Method has a high halstead difficulty"
count = 3
count = 2

[[issues]]
file = "src/ResultSet/AbstractResultSet.php"
Expand Down
1 change: 1 addition & 0 deletions mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ignore = [
{ code = "invalid-enum-case-value", in = ["src/Sql/ArgumentType.php"] },
]
class-initializers = [
"PhpDb\\Adapter\\Driver\\Pdo\\Result::initialize",
"PhpDb\\Adapter\\Driver\\Pdo\\Statement::initialize",
"PhpDb\\Adapter\\Driver\\Pdo\\Statement::setDriver",
"PhpDb\\ResultSet\\AbstractResultSet::initialize",
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getDriver(): Driver\DriverInterface
* @mago-expect lint:prefer-first-class-callable
*/
#[Override]
public function getHelpers()
public function getHelpers(): array
{
$functions = [];
$platform = $this->platform;
Expand Down
5 changes: 3 additions & 2 deletions src/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpDb\Adapter;

use Closure;
use PhpDb\ResultSet;

/**
Expand Down Expand Up @@ -51,9 +52,9 @@ public function getDriver(): Driver\DriverInterface;

/**
* @todo 0.3.x track down this usage!!!
* @return array
* @return list<Closure(string): string>
*/
public function getHelpers();
public function getHelpers(): array;

public function getPlatform(): Platform\PlatformInterface;

Expand Down
19 changes: 15 additions & 4 deletions src/Adapter/Driver/AbstractConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
/**
* @api
*
* @template TConnection of object|resource = object|resource The native connection handle
*
* @implements ConnectionInterface<TConnection>
*
* @psalm-import-type ConnectionParameters from ConnectionInterface
*/
abstract class AbstractConnection implements ConnectionInterface, ProfilerAwareInterface
Expand All @@ -27,8 +31,15 @@ abstract class AbstractConnection implements ConnectionInterface, ProfilerAwareI

protected ?ProfilerInterface $profiler = null;

/** Extending classes must be covariant. */
protected $resource;
/**
* The driver's native connection handle, or null while disconnected. It is typed mixed
* because a native extension may hand back a resource, which has no declarable type, and
* property types are invariant, so a driver narrows it where it reads it, not by
* redeclaring it.
*
* @var TConnection|null
*/
protected mixed $resource = null;

#[Override]
public function disconnect(): ConnectionInterface
Expand Down Expand Up @@ -63,10 +74,10 @@ public function getProfiler(): ?ProfilerInterface
}

/**
* @return resource|object|null
* @return TConnection|null
*/
#[Override]
public function getResource()
public function getResource(): mixed
{
if (! $this->isConnected()) {
$this->connect();
Expand Down
6 changes: 4 additions & 2 deletions src/Adapter/Driver/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/**
* @api
*
* @template TConnection of object|resource = object|resource The native connection handle
*
* @psalm-type ConnectionParameters = array<string, mixed>
*/
interface ConnectionInterface extends SchemaAwareInterface
Expand Down Expand Up @@ -36,9 +38,9 @@ public function getLastGeneratedValue(?string $name = null): string|int|false|nu
/**
* Get resource
*
* @return resource|object|null
* @return TConnection|null
*/
public function getResource();
public function getResource(): mixed;

/** Checks whether the connection is in transaction state. */
public function inTransaction(): bool;
Expand Down
Loading
Loading