浏览代码

Raise an exception on undefined clauses.

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
Mark Story 8 年之前
父节点
当前提交
33e71e07b7
共有 2 个文件被更改,包括 9 次插入2 次删除
  1. 6 1
      src/Database/Query.php
  2. 3 1
      tests/TestCase/Database/QueryTest.php

+ 6 - 1
src/Database/Query.php

@@ -1695,10 +1695,15 @@ class Query implements ExpressionInterface, IteratorAggregate
      *
      * @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];
     }
 
     /**

+ 3 - 1
tests/TestCase/Database/QueryTest.php

@@ -4332,13 +4332,15 @@ class QueryTest extends TestCase
     /**
      * 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');
     }
 
     /**