Skip to content

Fix deprecation: PDOStatement::bindParam(): Passing null to $maxLength #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 14, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix deprecation notice thrown when instrumenting the `PDOStatement::bindParam()` method and passing `$length = null` (#586)

## 4.2.6 (2022-01-10)

- Add support for `symfony/cache-contracts` package version `3.x` (#588)
Expand Down
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ parameters:
count: 1
path: tests/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnectionTest.php

-
message: "#^Access to an undefined property PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub\\:\\:\\$bindParamCallArgsCount\\.$#"
count: 1
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php

-
message: "#^Parameter \\#2 \\$decoratedStatement of class Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2 constructor expects Doctrine\\\\DBAL\\\\Driver\\\\Statement, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub given\\.$#"
count: 1
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php

-
message: "#^Trying to mock an undefined method closeCursor\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Statement\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion src/Tracing/Doctrine/DBAL/TracingStatementForV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
{
return $this->decoratedStatement->bindParam($param, $variable, $type, $length);
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Tracing/Doctrine/DBAL/TracingStatementForV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
*/
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
{
return $this->decoratedStatement->bindParam($param, $variable, $type, $length);
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
}

/**
Expand Down
41 changes: 40 additions & 1 deletion tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,21 @@ public function testBindParam(): void

$this->decoratedStatement->expects($this->once())
->method('bindParam')
->with('foo', $variable, ParameterType::INTEGER)
->with('foo', $variable, ParameterType::INTEGER, 10)
->willReturn(true);

$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
}

public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
{
$variable = 'bar';
$decoratedStatement = $this->createPartialMock(TracingStatementForV2Stub::class, array_diff(get_class_methods(TracingStatementForV2Stub::class), ['bindParam']));

$this->statement = new TracingStatementForV2($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);

$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
}

public function testErrorCode(): void
Expand Down Expand Up @@ -195,3 +206,31 @@ public function testRowCount(): void
$this->assertSame(10, $this->statement->rowCount());
}
}

if (!interface_exists(Statement::class)) {
abstract class TracingStatementForV2Stub
{
}
} else {
/**
* @phpstan-implements \IteratorAggregate<mixed, mixed>
*/
abstract class TracingStatementForV2Stub implements \IteratorAggregate, Statement
{
/**
* @var int
*/
public $bindParamCallArgsCount = 0;

public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
{
// Since PHPUnit forcefully calls the mocked methods with all
// parameters, regardless of whether they were originally passed
// in an explicit manner, we can't use a mock to assert the number
// of args used in the call to the function
$this->bindParamCallArgsCount = \func_num_args();

return true;
}
}
}
39 changes: 38 additions & 1 deletion tests/Tracing/Doctrine/DBAL/TracingStatementForV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,47 @@ public function testBindParam(): void

$this->decoratedStatement->expects($this->once())
->method('bindParam')
->with('foo', $variable, ParameterType::INTEGER)
->with('foo', $variable, ParameterType::INTEGER, 10)
->willReturn(true);

$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
}

public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
{
$variable = 'bar';
$decoratedStatement = new class() implements Statement {
/**
* @var int
*/
public $bindParamCallArgsCount = 0;

public function bindValue($param, $value, $type = ParameterType::STRING): bool
{
throw new \BadMethodCallException();
}

public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
{
// Since PHPUnit forcefully calls the mocked methods with all
// parameters, regardless of whether they were originally passed
// in an explicit manner, we can't use a mock to assert the number
// of args used in the call to the function
$this->bindParamCallArgsCount = \func_num_args();

return true;
}

public function execute($params = null): Result
{
throw new \BadMethodCallException();
}
};

$this->statement = new TracingStatementForV3($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);

$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
}

public function testExecute(): void
Expand Down