Skip to content

Commit

Permalink
Address implicit casts
Browse files Browse the repository at this point in the history
  • Loading branch information
Riimu committed Oct 8, 2018
1 parent 35275da commit 5e7e3f3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/Connection/MySqlConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function delete(string $table, array $where): \PDOStatement

public function formatFields(array $fields, string $table = '', string $prefix = ''): string
{
if (!$fields) {
if ($fields === []) {
throw new \InvalidArgumentException('No fields provided for the query');
}

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

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

if ($value) {
if ($value !== []) {
$placeholders = $this->formatParameters($value, $parameters);
return "($escaped IN $placeholders OR $escaped IS NULL)";
}
Expand Down Expand Up @@ -250,7 +250,7 @@ private function formatLimit(?int $limit, array & $parameters): string
*/
private function formatAssignments(array $values, array & $parameters): string
{
if (!$values) {
if ($values === []) {
throw new \InvalidArgumentException('No values provided for the query');
}

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

if (! $query instanceof \PDOStatement) {
throw new \UnexpectedValueException('Unexpected value returned by prepare query');
}

foreach ($parameters as $name => $value) {
$this->bindQueryParameter($query, \is_int($name) ? $name + 1 : $name, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function associate(string $name, Model $model): void
throw new \InvalidArgumentException('The associated record belongs to incorrect schema');
}

while ($keys) {
while ($keys !== []) {
$value = $record[array_pop($fields)];

if ($value === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function fillSingleRecord(Record $record, Record $referencedRecord): void
$keys = $this->getFields();
$fields = $this->getReferencedFields();

while ($keys) {
while ($keys !== []) {
if ((string) $record[array_pop($keys)] !== (string) $referencedRecord[array_pop($fields)]) {
throw new \InvalidArgumentException('The provided records are not related');
}
Expand Down
2 changes: 1 addition & 1 deletion src/RelationshipFiller.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function fillRelationships(array $records, array $relationships): void
$loaded = empty($filled) ? [] : array_merge(... array_values($filled));
$options = array_keys(array_diff_key($options, $filled));

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

Expand Down
18 changes: 18 additions & 0 deletions tests/tests/MySqlIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Simply\Database\Connection\Connection;
use Simply\Database\Connection\MySqlConnection;
use Simply\Database\Connection\Provider\ConnectionProvider;
use Simply\Database\Connection\Provider\GenericConnectionProvider;
use Simply\Database\Connection\Provider\MySqlConnectionProvider;
use Simply\Database\Test\TestCase\IntegrationTestCase;
Expand Down Expand Up @@ -115,4 +116,21 @@ public function testDSNSupport(): void
$connection = new MySqlConnectionProvider('/tmp/mysql.sock', 'database', 'username', 'password');
$this->assertContains('unix_socket', $property->getValue($connection));
}

public function testHandlingFalsePrepare()
{
$pdo = $this->getMockBuilder(\PDO::class)
->disableOriginalConstructor()
->getMock();

$pdo->expects($this->once())->method('prepare')->willReturn(false);

$connection = $this->createMock(ConnectionProvider::class);
$connection->method('getConnection')->willReturn($pdo);

$database = new MySqlConnection($connection);

$this->expectException(\UnexpectedValueException::class);
$database->query('SELECT * FROM `' . $this->personSchema->getTable() . '`');
}
}

0 comments on commit 5e7e3f3

Please sign in to comment.