Skip to content

Commit

Permalink
Remove unused variables and general tidy of code (#176)
Browse files Browse the repository at this point in the history
* Write tests for #115

* Implement type-safe fetch functionality, closes #115

* Use existing fetch functions

* Introduce `Fetchable` trait to share code over Database, QueryCollection

* Remove unused exceptions

* Rename toArray->asArray for consistency with other repos

* Tidy unused variables

* Tidy unused variables

* toArray->asArray change
  • Loading branch information
g105b authored Oct 31, 2019
1 parent c371fc7 commit 2039db4
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/Fetchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected function fetchAllTyped(
}

protected function castRow(string $type, Row $row) {
$assocArray = $row->toArray();
$assocArray = $row->asArray();
reset($assocArray);
$key = key($assocArray);
$value = $assocArray[$key];
Expand Down
4 changes: 0 additions & 4 deletions src/NoSuchTableException.php

This file was deleted.

36 changes: 17 additions & 19 deletions src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Gt\Database\Query;

use Gt\Database\Connection\Connection;
use Gt\Database\Connection\Driver;
use PDO;
use PDOException;
use PDOStatement;
Expand Down Expand Up @@ -186,23 +184,23 @@ protected function escapeSpecialBinding(

// TODO: In v2 we will properly parse the different parts of the special bindings.
// See https://github.com/PhpGt/Database/issues/117
switch($type) {
// [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
case "groupBy":
break;

// [ORDER BY {col_name | expr | position}
case "orderBy":
break;

// [LIMIT {[offset,] row_count | row_count OFFSET offset}]
case "limit":
break;

// [LIMIT {[offset,] row_count | row_count OFFSET offset}]
case "offset":
break;
}
// switch($type) {
// [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
// case "groupBy":
// break;
//
// [ORDER BY {col_name | expr | position}
// case "orderBy":
// break;
//
// [LIMIT {[offset,] row_count | row_count OFFSET offset}]
// case "limit":
// break;
//
// [LIMIT {[offset,] row_count | row_count OFFSET offset}]
// case "offset":
// break;
// }

return (string)$value;
}
Expand Down
4 changes: 0 additions & 4 deletions src/ReadOnlyArrayAccessException.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Result/EmptyResultSetException.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/Result/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* count and getLength
*/
class ResultSet implements Iterator, Countable {
/** @var \PDOStatement */
/** @var PDOStatement */
protected $statement;
/** @var \Gt\Database\Result\Row */
/** @var Row */
protected $current_row;
protected $row_index = null;
/** @var string */
Expand Down Expand Up @@ -78,11 +78,11 @@ protected function fetchUpToIteratorIndex() {
}
}

public function toArray($elementsToArray = false):array {
public function asArray($elementsToArray = true):array {
if($elementsToArray) {
$data = array_map(function($element) {
/** @var Row $element */
return $element->toArray();
return $element->asArray();
},
$this->fetchAll()
);
Expand Down
2 changes: 1 addition & 1 deletion src/Result/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getDateTime(string $columnName):?DateTime {
return $dateTime;
}

public function toArray():array {
public function asArray():array {
return $this->data;
}

Expand Down
19 changes: 19 additions & 0 deletions test/unit/Result/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ public function testFetchAll() {
}
}

public function testAsArray() {
$resultSet = new ResultSet($this->getStatementMock());
$array = $resultSet->asArray();
foreach($array as $i => $rowArray) {
self::assertIsArray($rowArray);
self::assertArrayHasKey("id", $rowArray);
self::assertArrayHasKey("name", $rowArray);
self::assertIsInt($rowArray["id"]);
}
}

public function testAsArrayNoMap() {
$resultSet = new ResultSet($this->getStatementMock());
$array = $resultSet->asArray(false);
foreach($array as $i => $row) {
self::assertInstanceOf(Row::class, $row);
}
}

private function getStatementMock():PDOStatement {
$statement = $this->createMock(PDOStatement::class);
$statement->method("fetch")
Expand Down
12 changes: 5 additions & 7 deletions test/unit/Result/RowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace unit\Result;

use DateTime;
use Gt\Database\Result\NoSuchColumnException;
use Gt\Database\Result\Row;
use PHPUnit\Framework\TestCase;

Expand All @@ -12,12 +11,12 @@ class RowTest extends TestCase {
public function testFieldAccess(array $data) {
$row = new Row($data);

foreach($data as $key => $value) {
if(is_float($data[$key])) {
self::assertEqualsWithDelta($data[$key], $value, 0.0001);
foreach($row as $key => $value) {
if(is_float($row->$key)) {
self::assertEqualsWithDelta($row->$key, $value, 0.0001);
}
else {
self::assertEquals($data[$key], $value);
self::assertEquals($row->$key, $value);
}
}
}
Expand All @@ -34,7 +33,7 @@ public function testIsNotSet() {

public function testGetNonExistentProperty() {
$row = new Row(["col1" => "item"]);
self::assertNull($row->doink);
self::assertNull($row->get("doink"));
}

public function testEmpty() {
Expand Down Expand Up @@ -120,7 +119,6 @@ public function data_getTestRow():array {
$data = [];

$columns = ["id", "name", "example", "exampleFloat", "exampleDateTime", "exampleBool"];
$rows = [];
$rowNum = rand(2, 50);
for($i = 0; $i < $rowNum; $i++) {
$row = [];
Expand Down

0 comments on commit 2039db4

Please sign in to comment.