Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Fix isset on null values #43

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function __set($name, $value)
*/
public function __unset($name)
{
if (!isset($this->columns[$name])) {
if (!$this->__isset($name)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use array_key_exists() here as well; this is a case where DRY is not better, but instead adds performance overhead and indirection.

Make the same change on the __get() method, too.

throw new Exception\InvalidArgumentException('The key ' . $name . ' was not found in this objects column list');
}

Expand All @@ -243,7 +243,7 @@ public function __unset($name)
*/
public function __isset($name)
{
return isset($this->columns[$name]);
return array_key_exists($name, $this->columns);
}

/**
Expand All @@ -257,7 +257,7 @@ public function __isset($name)
*/
public function __get($name)
{
if (!isset($this->columns[$name])) {
if (!$this->__isset($name)) {
throw new Exception\InvalidArgumentException('The key ' . $name . ' was not found in this objects column list');
}
return $this->columns[$name];
Expand Down
14 changes: 14 additions & 0 deletions test/Sql/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,14 @@ public function test__unset()
unset($this->insert->foo);
$this->assertEquals([], $this->insert->getRawState('columns'));
$this->assertEquals([], $this->insert->getRawState('values'));

$this->insert->foo = NULL;
$this->assertEquals(['foo'], $this->insert->getRawState('columns'));
$this->assertEquals([NULL], $this->insert->getRawState('values'));

unset($this->insert->foo);
$this->assertEquals([], $this->insert->getRawState('columns'));
$this->assertEquals([], $this->insert->getRawState('values'));
}

/**
Expand All @@ -252,6 +260,9 @@ public function test__isset()
{
$this->insert->foo = 'bar';
$this->assertTrue(isset($this->insert->foo));

$this->insert->foo = NULL;
$this->assertTrue(isset($this->insert->foo));
}

/**
Expand All @@ -261,6 +272,9 @@ public function test__get()
{
$this->insert->foo = 'bar';
$this->assertEquals('bar', $this->insert->foo);

$this->insert->foo = NULL;
$this->assertNull($this->insert->foo);
}

/**
Expand Down