Skip to content

Commit 3a16fdc

Browse files
JeroenyJean85ste93cry
authored
Fix deprecation: PDOStatement::bindParam(): Passing null to $maxLength (#586)
* Fix deprecation: Deprecated: PDOStatement::bindParam(): Passing null to parameter #4 ($maxLength) of type int is deprecated in ./vendor/doctrine/dbal/src/Driver/PDO/Statement.php on line 83 * add testBindParamWithoutLength add changelog * Update tests/Tracing/Doctrine/DBAL/TracingStatementForV3Test.php Co-authored-by: Alessandro Lai <[email protected]> * Update tests/Tracing/Doctrine/DBAL/TracingStatementForV3Test.php Co-authored-by: Alessandro Lai <[email protected]> * Improve the tests for the `TracingStatementForV*::bindParam()` method Co-authored-by: Alessandro Lai <[email protected]> Co-authored-by: Stefano Arlandini <[email protected]>
1 parent 0c0b33c commit 3a16fdc

File tree

6 files changed

+92
-4
lines changed

6 files changed

+92
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix deprecation notice thrown when instrumenting the `PDOStatement::bindParam()` method and passing `$length = null` (#586)
6+
57
## 4.2.6 (2022-01-10)
68

79
- Add support for `symfony/cache-contracts` package version `3.x` (#588)

phpstan-baseline.neon

+10
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,16 @@ parameters:
440440
count: 1
441441
path: tests/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnectionTest.php
442442

443+
-
444+
message: "#^Access to an undefined property PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Sentry\\\\SentryBundle\\\\Tests\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingStatementForV2Stub\\:\\:\\$bindParamCallArgsCount\\.$#"
445+
count: 1
446+
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
447+
448+
-
449+
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\\.$#"
450+
count: 1
451+
path: tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php
452+
443453
-
444454
message: "#^Trying to mock an undefined method closeCursor\\(\\) on class Doctrine\\\\DBAL\\\\Driver\\\\Statement\\.$#"
445455
count: 1

src/Tracing/Doctrine/DBAL/TracingStatementForV2.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
108108
*/
109109
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
110110
{
111-
return $this->decoratedStatement->bindParam($param, $variable, $type, $length);
111+
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
112112
}
113113

114114
/**

src/Tracing/Doctrine/DBAL/TracingStatementForV3.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): bool
2727
*/
2828
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
2929
{
30-
return $this->decoratedStatement->bindParam($param, $variable, $type, $length);
30+
return $this->decoratedStatement->bindParam($param, $variable, $type, ...\array_slice(\func_get_args(), 3));
3131
}
3232

3333
/**

tests/Tracing/Doctrine/DBAL/TracingStatementForV2Test.php

+40-1
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,21 @@ public function testBindParam(): void
123123

124124
$this->decoratedStatement->expects($this->once())
125125
->method('bindParam')
126-
->with('foo', $variable, ParameterType::INTEGER)
126+
->with('foo', $variable, ParameterType::INTEGER, 10)
127127
->willReturn(true);
128128

129+
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
130+
}
131+
132+
public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
133+
{
134+
$variable = 'bar';
135+
$decoratedStatement = $this->createPartialMock(TracingStatementForV2Stub::class, array_diff(get_class_methods(TracingStatementForV2Stub::class), ['bindParam']));
136+
137+
$this->statement = new TracingStatementForV2($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
138+
129139
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
140+
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
130141
}
131142

132143
public function testErrorCode(): void
@@ -196,3 +207,31 @@ public function testRowCount(): void
196207
$this->assertSame(10, $this->statement->rowCount());
197208
}
198209
}
210+
211+
if (!interface_exists(Statement::class)) {
212+
abstract class TracingStatementForV2Stub
213+
{
214+
}
215+
} else {
216+
/**
217+
* @phpstan-implements \IteratorAggregate<mixed, mixed>
218+
*/
219+
abstract class TracingStatementForV2Stub implements \IteratorAggregate, Statement
220+
{
221+
/**
222+
* @var int
223+
*/
224+
public $bindParamCallArgsCount = 0;
225+
226+
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
227+
{
228+
// Since PHPUnit forcefully calls the mocked methods with all
229+
// parameters, regardless of whether they were originally passed
230+
// in an explicit manner, we can't use a mock to assert the number
231+
// of args used in the call to the function
232+
$this->bindParamCallArgsCount = \func_num_args();
233+
234+
return true;
235+
}
236+
}
237+
}

tests/Tracing/Doctrine/DBAL/TracingStatementForV3Test.php

+38-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,47 @@ public function testBindParam(): void
6161

6262
$this->decoratedStatement->expects($this->once())
6363
->method('bindParam')
64-
->with('foo', $variable, ParameterType::INTEGER)
64+
->with('foo', $variable, ParameterType::INTEGER, 10)
6565
->willReturn(true);
6666

67+
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER, 10));
68+
}
69+
70+
public function testBindParamForwardsLengthParamOnlyWhenExplicitlySet(): void
71+
{
72+
$variable = 'bar';
73+
$decoratedStatement = new class() implements Statement {
74+
/**
75+
* @var int
76+
*/
77+
public $bindParamCallArgsCount = 0;
78+
79+
public function bindValue($param, $value, $type = ParameterType::STRING): bool
80+
{
81+
throw new \BadMethodCallException();
82+
}
83+
84+
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null): bool
85+
{
86+
// Since PHPUnit forcefully calls the mocked methods with all
87+
// parameters, regardless of whether they were originally passed
88+
// in an explicit manner, we can't use a mock to assert the number
89+
// of args used in the call to the function
90+
$this->bindParamCallArgsCount = \func_num_args();
91+
92+
return true;
93+
}
94+
95+
public function execute($params = null): Result
96+
{
97+
throw new \BadMethodCallException();
98+
}
99+
};
100+
101+
$this->statement = new TracingStatementForV3($this->hub, $decoratedStatement, 'SELECT 1', ['db.system' => 'sqlite']);
102+
67103
$this->assertTrue($this->statement->bindParam('foo', $variable, ParameterType::INTEGER));
104+
$this->assertSame(3, $decoratedStatement->bindParamCallArgsCount);
68105
}
69106

70107
public function testExecute(): void

0 commit comments

Comments
 (0)