Skip to content
Merged
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
29 changes: 20 additions & 9 deletions src/Adapter/Profiler/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@

use PhpDb\Adapter\Exception;
use PhpDb\Adapter\Exception\InvalidArgumentException;
use PhpDb\Adapter\ParameterContainer;
use PhpDb\Adapter\StatementContainerInterface;

use function end;
use function is_string;
use function microtime;

/**
* @phpstan-type ProfileShape array{
* sql: string,
* parameters: ParameterContainer|null,
* start: float,
* end: float|null,
* elapse: float|null,
* }
* @phpstan-type ProfilesShape ProfileShape[]
*/
class Profiler implements ProfilerInterface
{
/** @var array */
/** @var ProfilesShape */
protected $profiles = [];

/** @var int */
Expand All @@ -33,18 +44,15 @@ public function profilerStart(string|StatementContainerInterface $target): Profi
'end' => null,
'elapse' => null,
];
if ($target instanceof StatementContainerInterface) {

if (is_string($target)) {
$profileInformation['sql'] = $target;
} else {
$profileInformation['sql'] = $target->getSql();
$container = $target->getParameterContainer();
if ($container !== null) {
$profileInformation['parameters'] = clone $container;
}
} elseif (is_string($target)) {
$profileInformation['sql'] = $target;
} else {
throw new Exception\InvalidArgumentException(
__FUNCTION__ . ' takes either a StatementContainer or a string'
);
}

$this->profiles[$this->currentIndex] = $profileInformation;
Expand All @@ -70,13 +78,16 @@ public function profilerFinish(): ProfilerInterface
}

/**
* @return array|null
* @return ProfileShape|null
*/
public function getLastProfile(): ?array
{
return end($this->profiles);
}

/**
* @return ProfilesShape
*/
public function getProfiles(): array
{
return $this->profiles;
Expand Down