Completed
Push — master ( 642f12...12ee63 )
by Vitaly
02:58
created

Condition::addCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace samsonframework\orm;
3
4
/**
5
 * Query condition arguments group
6
 * @author Vitaly Iegorov <[email protected]>
7
 * @version 2.0
8
 */
9
class Condition
10
{
11
    /** AND(conjunction) - Condition relation type */
12
    const REL_AND = 'AND';
13
14
    /** OR(disjunction) - Condition relation type */
15
    const REL_OR = 'OR';
16
17
    /** @var Argument[] Collection of condition arguments */
18
    public $arguments = array();
19
20
    /** @var string Relation logic between arguments */
21
    public $relation = self::REL_AND;
22
23
    /**
24
     * Add condition argument to this condition group
25
     * @param Argument $argument Condition argument to be added
26
     */
27
    public function addArgument(Argument $argument)
28
    {
29
        // Add condition as current condition argument
30
        $this->arguments[] = $argument;
31
    }
32
33
    /**
34
     * Add condition group to this condition group
35
     * @param Condition $condition Condition group to be added
36
     */
37
    public function addCondition(self $condition)
38
    {
39
        // Add condition as current condition argument
40
        $this->arguments[] = $condition;
41
    }
42
43
    /**
44
     * Generic condition addiction function
45
     * @param self|Argument|string $argument Entity for adding to arguments collection
46
     * @param string $value Argument value
47
     * @param string $relation Relation between argument and value
48
     * @return self Chaining
49
     */
50
    public function add($argument, $value = '', $relation = Relation::EQUAL)
51
    {
52
        if (is_string($argument) || is_scalar($argument)) {
53
            // Add new argument to arguments collection
54
            $this->arguments[] = new Argument($argument, $value, $relation);
55
        } elseif (is_a($argument, get_class($this))) {
56
            $this->addCondition($argument);
0 ignored issues
show
Documentation introduced by
$argument is of type object<samsonframework\o...framework\orm\Argument>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
        } elseif (is_a($argument, __NAMESPACE__.'Argument')) {
58
            $this->addArgument($argument);
0 ignored issues
show
Bug introduced by
It seems like $argument defined by parameter $argument on line 50 can also be of type object<samsonframework\orm\Condition>; however, samsonframework\orm\Condition::addArgument() does only seem to accept object<samsonframework\orm\Argument>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Constructor
66
     * @param string $relation Relation type between arguments
67
     */
68
    public function __construct($relation = null)
69
    {
70
        $this->relation = isset($relation) ? $relation : self::REL_AND;
71
    }
72
}
73