Skip to content

Commit

Permalink
Raise an exception on undefined clauses.
Browse files Browse the repository at this point in the history
When the named clause doesn't exist throw an exception. This helps
prevent null checks in userland code and more clearly signals an error.

Refs #10734
  • Loading branch information
markstory committed Jun 8, 2017
1 parent 8782ba1 commit 33e71e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Database/Query.php
Expand Up @@ -1695,10 +1695,15 @@ public function getIterator()
*
* @param string $name name of the clause to be returned
* @return mixed
* @throws InvalidArgumentException When the named clause does not exist.
*/
public function clause($name)
{
return isset($this->_parts[$name]) ? $this->_parts[$name] : null;
if (!array_key_exists($name, $this->_parts)) {
$clauses = implode(', ', array_keys($this->_parts));
throw new InvalidArgumentException("The '$name' clause is not defined. Valid clauses are: $clauses");
}
return $this->_parts[$name];
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase/Database/QueryTest.php
Expand Up @@ -4332,13 +4332,15 @@ public function testSelectWithObjFetchType()
/**
* Test that reading an undefined clause does not emit an error.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The 'nope' clause is not defined. Valid clauses are: delete, update
* @return void
*/
public function testClauseUndefined()
{
$query = new Query($this->connection);
$this->assertEmpty($query->clause('where'));
$this->assertNull($query->clause('nope'));
$query->clause('nope');
}

/**
Expand Down

0 comments on commit 33e71e0

Please sign in to comment.