Skip to content

Commit 5e7e3f3

Browse files
committed
Address implicit casts
1 parent 35275da commit 5e7e3f3

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

src/Connection/MySqlConnection.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function delete(string $table, array $where): \PDOStatement
8585

8686
public function formatFields(array $fields, string $table = '', string $prefix = ''): string
8787
{
88-
if (!$fields) {
88+
if ($fields === []) {
8989
throw new \InvalidArgumentException('No fields provided for the query');
9090
}
9191

@@ -130,7 +130,7 @@ public function formatTable(string $table, string $alias = ''): string
130130
*/
131131
private function formatConditions(array $conditions, array & $parameters): string
132132
{
133-
if (!$conditions) {
133+
if ($conditions === []) {
134134
throw new \InvalidArgumentException('No conditions provided for the query');
135135
}
136136

@@ -160,7 +160,7 @@ private function formatClause(string $field, $value, array & $parameters): strin
160160
return $value !== null;
161161
});
162162

163-
if ($value) {
163+
if ($value !== []) {
164164
$placeholders = $this->formatParameters($value, $parameters);
165165
return "($escaped IN $placeholders OR $escaped IS NULL)";
166166
}
@@ -250,7 +250,7 @@ private function formatLimit(?int $limit, array & $parameters): string
250250
*/
251251
private function formatAssignments(array $values, array & $parameters): string
252252
{
253-
if (!$values) {
253+
if ($values === []) {
254254
throw new \InvalidArgumentException('No values provided for the query');
255255
}
256256

@@ -298,6 +298,10 @@ public function query(string $sql, array $parameters = []): \PDOStatement
298298
{
299299
$query = $this->getConnection()->prepare($sql);
300300

301+
if (! $query instanceof \PDOStatement) {
302+
throw new \UnexpectedValueException('Unexpected value returned by prepare query');
303+
}
304+
301305
foreach ($parameters as $name => $value) {
302306
$this->bindQueryParameter($query, \is_int($name) ? $name + 1 : $name, $value);
303307
}

src/Record.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function associate(string $name, Model $model): void
213213
throw new \InvalidArgumentException('The associated record belongs to incorrect schema');
214214
}
215215

216-
while ($keys) {
216+
while ($keys !== []) {
217217
$value = $record[array_pop($fields)];
218218

219219
if ($value === null) {

src/Relationship.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public function fillSingleRecord(Record $record, Record $referencedRecord): void
275275
$keys = $this->getFields();
276276
$fields = $this->getReferencedFields();
277277

278-
while ($keys) {
278+
while ($keys !== []) {
279279
if ((string) $record[array_pop($keys)] !== (string) $referencedRecord[array_pop($fields)]) {
280280
throw new \InvalidArgumentException('The provided records are not related');
281281
}

src/RelationshipFiller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function fillRelationships(array $records, array $relationships): void
102102
$loaded = empty($filled) ? [] : array_merge(... array_values($filled));
103103
$options = array_keys(array_diff_key($options, $filled));
104104

105-
if ($options) {
105+
if ($options !== []) {
106106
$result = $this->connection->select($parent->getFields(), $parent->getTable(), [$field => $options]);
107107
$result->setFetchMode(\PDO::FETCH_ASSOC);
108108

tests/tests/MySqlIntegrationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Simply\Database\Connection\Connection;
66
use Simply\Database\Connection\MySqlConnection;
7+
use Simply\Database\Connection\Provider\ConnectionProvider;
78
use Simply\Database\Connection\Provider\GenericConnectionProvider;
89
use Simply\Database\Connection\Provider\MySqlConnectionProvider;
910
use Simply\Database\Test\TestCase\IntegrationTestCase;
@@ -115,4 +116,21 @@ public function testDSNSupport(): void
115116
$connection = new MySqlConnectionProvider('/tmp/mysql.sock', 'database', 'username', 'password');
116117
$this->assertContains('unix_socket', $property->getValue($connection));
117118
}
119+
120+
public function testHandlingFalsePrepare()
121+
{
122+
$pdo = $this->getMockBuilder(\PDO::class)
123+
->disableOriginalConstructor()
124+
->getMock();
125+
126+
$pdo->expects($this->once())->method('prepare')->willReturn(false);
127+
128+
$connection = $this->createMock(ConnectionProvider::class);
129+
$connection->method('getConnection')->willReturn($pdo);
130+
131+
$database = new MySqlConnection($connection);
132+
133+
$this->expectException(\UnexpectedValueException::class);
134+
$database->query('SELECT * FROM `' . $this->personSchema->getTable() . '`');
135+
}
118136
}

0 commit comments

Comments
 (0)