Skip to content

Commit

Permalink
Amend previous with running code
Browse files Browse the repository at this point in the history
Added phpstan to the list of things I need to run before a push.
  • Loading branch information
mcurland committed Apr 12, 2023
1 parent ac4c9fd commit d523f52
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
17 changes: 8 additions & 9 deletions src/Driver/IBMDB2/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use Throwable;

use function assert;
use function db2_bind_param;
Expand All @@ -30,7 +31,7 @@
use const DB2_LONG;
use const DB2_PARAM_FILE;
use const DB2_PARAM_IN;
use const SEEK_CUR;
use const SEEK_SET;

final class Statement implements StatementInterface
{
Expand Down Expand Up @@ -214,25 +215,23 @@ private function createTemporaryFile()
*/
private function copyStreamToStream($source, $target): void
{
$resettable = false;
$resetTo = false;
try {
if (stream_get_meta_data($resource)['seekable']) {
$resettable = true;
$position = ftell($resource);
if (stream_get_meta_data($source)['seekable']) {
$resetTo = ftell($source);
}
} catch (Throwable $e) {
// Swallow
}

$bytesCopied = @stream_copy_to_stream($source, $target);
if ($bytesCopied === false) {
if (@stream_copy_to_stream($source, $target) === false) {
throw CannotCopyStreamToStream::new(error_get_last());
}

if (! $resettable) {
if ($resetTo === false) {
return;
}

fseek($source, -$bytesCopied, SEEK_CUR);
fseek($source, $resetTo, SEEK_SET);
}
}
5 changes: 3 additions & 2 deletions src/Driver/Mysqli/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\Deprecations\Deprecation;
use mysqli_sql_exception;
use mysqli_stmt;
use Throwable;

use function array_fill;
use function assert;
Expand Down Expand Up @@ -215,7 +216,7 @@ private function bindTypedParameters(): void
private function sendLongData(array $streams): void
{
foreach ($streams as $paramNr => $stream) {
$resetTo = -1;
$resetTo = false;
try {
if (stream_get_meta_data($stream)['seekable']) {
$resetTo = ftell($stream);
Expand All @@ -237,7 +238,7 @@ private function sendLongData(array $streams): void
}
}
} finally {
if ($resetTo !== -1) {
if ($resetTo !== false) {
fseek($stream, $resetTo, SEEK_SET);
}
}
Expand Down
18 changes: 6 additions & 12 deletions src/Driver/PDO/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,40 +169,34 @@ private function trackParamResource($resource): void
* restored to after the statement is executed. Call immediately
* before execute (not during bindValue) to get the most accurate offset.
*
* @returns int[]|null Return offsets to restore if needed.
* @return int[]|null Return offsets to restore if needed.
*/
private function getResourceOffsets()
{
$resources = &$this->paramResources;
if ($resources === null) {
return;
return null;
}

$count = count($resources);
$resourceOffsets = null;
$deref = [];
for ($i = 0; $i < $count; ++$i) {
$resource = $resources[$i];
$position = -1;
$position = false;
try {
if (stream_get_meta_data($resource)['seekable']) {
$position = ftell($resource);
}
} catch (Throwable $e) {
// Swallow
} finally {
if ($position === -1) {
if ($position === false) {
if ($resourceOffsets !== null) {
$resourceOffsets[] = $position;
$resourceOffsets[] = -1;
}
} else {
if ($resourceOffsets === null) {
$resourceOffsets = [];
if ($i !== 0) {
array_fill(0, $i, -1);
}
}

$resourceOffsets ??= $i !== 0 ? array_fill(0, $i, -1) : [];
$resourceOffsets[] = $position;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Driver/PgSQL/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\ParameterType;
use Doctrine\Deprecations\Deprecation;
use PgSql\Connection as PgSqlConnection;
use Throwable;
use TypeError;

use function assert;
Expand Down Expand Up @@ -158,7 +159,7 @@ public function execute($params = null): Result
case ParameterType::LARGE_OBJECT:
$isResource = $value !== null && is_resource($value);
$resource = $value;
$resetTo = -1;
$resetTo = false;
if ($isResource) {
try {
if (stream_get_meta_data($resource)['seekable']) {
Expand All @@ -174,7 +175,7 @@ public function execute($params = null): Result
$isResource ? stream_get_contents($value) : $value,
);

if ($resetTo !== -1) {
if ($resetTo !== false) {
fseek($resource, $resetTo, SEEK_SET);
}

Expand Down

0 comments on commit d523f52

Please sign in to comment.