From 5c63c5a991c5c6394911a12f4266af2a72249fe3 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 14:53:04 +0100 Subject: [PATCH 01/10] update to cover all PDO's fetch styles --- src/Adapter/Driver/Pdo/Result.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index ec4cdc6528..256bcc3416 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -107,7 +107,7 @@ public function isBuffered() */ public function setFetchMode($fetchMode) { - if ($fetchMode < 1 || $fetchMode > 10) { + if ($fetchMode < 1 || $fetchMode > 12 || $fetchMode === \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE) { throw new Exception\InvalidArgumentException( 'The fetch mode must be one of the PDO::FETCH_* constants.' ); From 2cb1056ab2ed9525617e9f7b652d6c86bcc2cc52 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 15:01:44 +0100 Subject: [PATCH 02/10] correctly check the range now --- src/Adapter/Driver/Pdo/Result.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index 256bcc3416..67da052030 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -107,7 +107,7 @@ public function isBuffered() */ public function setFetchMode($fetchMode) { - if ($fetchMode < 1 || $fetchMode > 12 || $fetchMode === \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE) { + if (! (($fetchMode >= 1 && $fetchMode <= 12) || $fetchMode === \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE)) { throw new Exception\InvalidArgumentException( 'The fetch mode must be one of the PDO::FETCH_* constants.' ); From cff8760c516f85ccd82f06227e639cc211cac818 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 15:05:11 +0100 Subject: [PATCH 03/10] updated the changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8cbecf9fe..7601cbcc6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse ### Changed -- Nothing. +- Broader PDO fetch style check. ### Deprecated From 48e3f906c81f93dc8116f7fac7854a0be2012d44 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 15:08:08 +0100 Subject: [PATCH 04/10] added unit test to prove change is proper --- test/unit/Adapter/Driver/Pdo/ResultTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/unit/Adapter/Driver/Pdo/ResultTest.php b/test/unit/Adapter/Driver/Pdo/ResultTest.php index d806000dc1..7000a0a8ac 100644 --- a/test/unit/Adapter/Driver/Pdo/ResultTest.php +++ b/test/unit/Adapter/Driver/Pdo/ResultTest.php @@ -69,4 +69,24 @@ public function testFetchModeAnonymousObject() self::assertEquals(5, $result->getFetchMode()); self::assertInstanceOf('stdClass', $result->current()); } + + /** + * Tests whether the fetch mode has a broader range + */ + public function testFetchModeRange() + { + $stub = $this->getMockBuilder('PDOStatement')->getMock(); + $stub->expects($this->any()) + ->method('fetch') + ->will($this->returnCallback(function () { + return new stdClass; + })); + + $result = new Result(); + $result->initialize($stub, null); + $result->setFetchMode(\PDO::FETCH_NAMED); + + self::assertEquals(11, $result->getFetchMode()); + self::assertInstanceOf('stdClass', $result->current()); + } } From fa2ecc0caeb1914641ec4401f4e92de03e2cf3bf Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 15:12:26 +0100 Subject: [PATCH 05/10] updated other test to support new range --- test/unit/Adapter/Driver/Pdo/ResultTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/Adapter/Driver/Pdo/ResultTest.php b/test/unit/Adapter/Driver/Pdo/ResultTest.php index 7000a0a8ac..405211ec58 100644 --- a/test/unit/Adapter/Driver/Pdo/ResultTest.php +++ b/test/unit/Adapter/Driver/Pdo/ResultTest.php @@ -47,7 +47,7 @@ public function testFetchModeException() $result = new Result(); $this->expectException('\Zend\Db\Adapter\Exception\InvalidArgumentException'); - $result->setFetchMode(11); + $result->setFetchMode(13); } /** From 8fe1def21d7d1ac63f80d2006f5c316cb87bfd12 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Dec 2017 15:19:16 +0100 Subject: [PATCH 06/10] whoops, I weirded out on the condition... --- src/Adapter/Driver/Pdo/Result.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index 67da052030..225bb6b919 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -107,7 +107,7 @@ public function isBuffered() */ public function setFetchMode($fetchMode) { - if (! (($fetchMode >= 1 && $fetchMode <= 12) || $fetchMode === \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE)) { + if (($fetchMode < 1 || $fetchMode > 12) && $fetchMode !== \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE) { throw new Exception\InvalidArgumentException( 'The fetch mode must be one of the PDO::FETCH_* constants.' ); From 3cfb25a0a0e39d0dc3c994992f34de56cbaf2e58 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Thu, 9 Aug 2018 09:43:40 +0200 Subject: [PATCH 07/10] changed the valid mode set according to PR feedback this however in my opinion is not a completely valid set as `FETCH_PROPS_LATE` and `FETCH_CLASSTYPE` should only be used in combination with `FETCH_CLASS` --- src/Adapter/Driver/Pdo/Result.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index 225bb6b919..c185a369db 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -30,6 +30,28 @@ class Result implements Iterator, ResultInterface */ protected $fetchMode = \PDO::FETCH_ASSOC; + /** + * @var array + */ + private const VALID_FETCH_MODES = [ + \PDO::FETCH_LAZY, // 1 + \PDO::FETCH_ASSOC, // 2 + \PDO::FETCH_NUM, // 3 + \PDO::FETCH_BOTH, // 4 + \PDO::FETCH_OBJ, // 5 + \PDO::FETCH_BOUND, // 6 +// \PDO::FETCH_COLUMN, // 7 + \PDO::FETCH_CLASS, // 8 + \PDO::FETCH_INTO, // 9 + \PDO::FETCH_FUNC, // 10 + \PDO::FETCH_NAMED, // 11 + \PDO::FETCH_KEY_PAIR, // 12 + \PDO::FETCH_PROPS_LATE, // Extra option for \PDO::FETCH_CLASS +// \PDO::FETCH_SERIALIZE,// Seems to have been removed +// \PDO::FETCH_UNIQUE, // Option for fetchAll + \PDO::FETCH_CLASSTYPE, // Extra option for \PDO::FETCH_CLASS + ]; + /** * @var PDOStatement */ @@ -107,7 +129,7 @@ public function isBuffered() */ public function setFetchMode($fetchMode) { - if (($fetchMode < 1 || $fetchMode > 12) && $fetchMode !== \PDO::FETCH_CLASS | \PDO::FETCH_PROPS_LATE) { + if (! in_array($fetchMode, self::VALID_FETCH_MODES)) { throw new Exception\InvalidArgumentException( 'The fetch mode must be one of the PDO::FETCH_* constants.' ); From 5e95d00fb8552f4136c0a9443139dc5c0f9b12ee Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Fri, 21 Jun 2019 10:00:53 +0200 Subject: [PATCH 08/10] enabled strict mode for `in_array` --- src/Adapter/Driver/Pdo/Result.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index c185a369db..b389fb1405 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -129,7 +129,7 @@ public function isBuffered() */ public function setFetchMode($fetchMode) { - if (! in_array($fetchMode, self::VALID_FETCH_MODES)) { + if (! in_array($fetchMode, self::VALID_FETCH_MODES, true)) { throw new Exception\InvalidArgumentException( 'The fetch mode must be one of the PDO::FETCH_* constants.' ); From 6b97535f644fd724fa1af893957dfb2ca32739a6 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Fri, 21 Jun 2019 10:02:45 +0200 Subject: [PATCH 09/10] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7601cbcc6b..5c44f5668c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ All notable changes to this project will be documented in this file, in reverse ### Changed -- Broader PDO fetch style check. +- [#296](https://github.com/zendframework/zend-db/pull/296) Broader PDO fetch style check. ### Deprecated From c8e6541204ae04b215bcb511de5bba9cbccfb3b0 Mon Sep 17 00:00:00 2001 From: Chris Kruining Date: Thu, 4 Jul 2019 18:27:48 +0200 Subject: [PATCH 10/10] Update Result.php --- src/Adapter/Driver/Pdo/Result.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapter/Driver/Pdo/Result.php b/src/Adapter/Driver/Pdo/Result.php index b389fb1405..d047a37871 100644 --- a/src/Adapter/Driver/Pdo/Result.php +++ b/src/Adapter/Driver/Pdo/Result.php @@ -33,7 +33,7 @@ class Result implements Iterator, ResultInterface /** * @var array */ - private const VALID_FETCH_MODES = [ + const VALID_FETCH_MODES = [ \PDO::FETCH_LAZY, // 1 \PDO::FETCH_ASSOC, // 2 \PDO::FETCH_NUM, // 3