Browse Source

Add and/or methods on QueryExpression directly

Edgaras Janušauskas 6 years ago
parent
commit
806941202b

+ 33 - 23
src/Database/Expression/QueryExpression.php

@@ -16,7 +16,6 @@ declare(strict_types=1);
  */
 namespace Cake\Database\Expression;
 
-use BadMethodCallException;
 use Cake\Database\ExpressionInterface;
 use Cake\Database\Query;
 use Cake\Database\TypeMapTrait;
@@ -29,9 +28,6 @@ use InvalidArgumentException;
  * Represents a SQL Query expression. Internally it stores a tree of
  * expressions that can be compiled by converting this object to string
  * and will contain a correctly parenthesized and nested expression.
- *
- * @method $this and(callable|string|array|\Cake\Database\ExpressionInterface $conditions)
- * @method $this or(callable|string|array|\Cake\Database\ExpressionInterface $conditions)
  */
 class QueryExpression implements ExpressionInterface, Countable
 {
@@ -411,7 +407,6 @@ class QueryExpression implements ExpressionInterface, Countable
         return $this->add(new BetweenExpression($field, $from, $to, $type));
     }
 
-// phpcs:disable
     /**
      * Returns a new QueryExpression object containing all the conditions passed
      * and set up the conjunction to be "AND"
@@ -421,7 +416,7 @@ class QueryExpression implements ExpressionInterface, Countable
      * values that are being passed. Used for correctly binding values to statements.
      * @return \Cake\Database\Expression\QueryExpression
      */
-    public function and_($conditions, $types = [])
+    public function and($conditions, $types = [])
     {
         if ($conditions instanceof Closure) {
             return $conditions(new static([], $this->getTypeMap()->setTypes($types)));
@@ -439,7 +434,7 @@ class QueryExpression implements ExpressionInterface, Countable
      * values that are being passed. Used for correctly binding values to statements.
      * @return \Cake\Database\Expression\QueryExpression
      */
-    public function or_($conditions, $types = [])
+    public function or($conditions, $types = [])
     {
         if ($conditions instanceof Closure) {
             return $conditions(new static([], $this->getTypeMap()->setTypes($types), 'OR'));
@@ -447,6 +442,37 @@ class QueryExpression implements ExpressionInterface, Countable
 
         return new static($conditions, $this->getTypeMap()->setTypes($types), 'OR');
     }
+
+// phpcs:disable
+    /**
+     * Returns a new QueryExpression object containing all the conditions passed
+     * and set up the conjunction to be "AND"
+     *
+     * @param \Closure|string|array|\Cake\Database\ExpressionInterface $conditions to be joined with AND
+     * @param array $types associative array of fields pointing to the type of the
+     * values that are being passed. Used for correctly binding values to statements.
+     * @return \Cake\Database\Expression\QueryExpression
+     * @deprecated 4.0.0 Use and() instead.
+     */
+    public function and_($conditions, $types = [])
+    {
+        return $this->and($conditions, $types);
+    }
+
+    /**
+     * Returns a new QueryExpression object containing all the conditions passed
+     * and set up the conjunction to be "OR"
+     *
+     * @param \Closure|string|array|\Cake\Database\ExpressionInterface $conditions to be joined with OR
+     * @param array $types associative array of fields pointing to the type of the
+     * values that are being passed. Used for correctly binding values to statements.
+     * @return \Cake\Database\Expression\QueryExpression
+     * @deprecated 4.0.0 Use or() instead.
+     */
+    public function or_($conditions, $types = [])
+    {
+        return $this->or($conditions, $types);
+    }
 // phpcs:enable
 
     /**
@@ -583,22 +609,6 @@ class QueryExpression implements ExpressionInterface, Countable
     }
 
     /**
-     * Helps calling the `and()` and `or()` methods transparently.
-     *
-     * @param string $method The method name.
-     * @param array $args The arguments to pass to the method.
-     * @return $this
-     * @throws \BadMethodCallException
-     */
-    public function __call(string $method, array $args)
-    {
-        if (in_array($method, ['and', 'or'])) {
-            return call_user_func_array([$this, $method . '_'], $args);
-        }
-        throw new BadMethodCallException(sprintf('Method %s does not exist', $method));
-    }
-
-    /**
      * Check whether or not a callable is acceptable.
      *
      * We don't accept ['class', 'method'] style callbacks,

+ 1 - 1
src/ORM/Marshaller.php

@@ -411,7 +411,7 @@ class Marshaller
             $query = $target->find();
             $query->andWhere(function ($exp) use ($conditions) {
                 /** @var \Cake\Database\Expression\QueryExpression $exp */
-                return $exp->or_($conditions);
+                return $exp->or($conditions);
             });
 
             $keyFields = array_keys($primaryKey);

+ 2 - 2
tests/TestCase/Database/Expression/QueryExpressionTest.php

@@ -196,8 +196,8 @@ class QueryExpressionTest extends TestCase
     public function testEmptyOr()
     {
         $expr = new QueryExpression();
-        $expr = $expr->or_([]);
-        $expr = $expr->or_([]);
+        $expr = $expr->or([]);
+        $expr = $expr->or([]);
         $this->assertCount(0, $expr);
 
         $expr = new QueryExpression(['OR' => []]);