Factory::make()   C
last analyzed

Complexity

Conditions 13
Paths 13

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.1234
c 0
b 0
f 0
ccs 22
cts 22
cp 1
cc 13
eloc 22
nc 13
nop 3
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mattbit\Flat\Query\Expression;
4
5
use Mattbit\Flat\Query\Expression\Leaf\EqExpression;
6
use Mattbit\Flat\Query\Expression\Leaf\Expression;
7
use Mattbit\Flat\Query\Expression\Leaf\InExpression;
8
use Mattbit\Flat\Query\Expression\Tree\NotExpression;
9
10
class Factory
11
{
12
    private $namespace = 'Mattbit\Flat\Query\Expression';
13
14
    /**
15
     * Return an Expression based on the operator.
16
     *
17
     * @param $operator
18
     * @param $key
19
     * @param $reference
20
     * @return ExpressionInterface
21
     * @throws \Exception
22
     */
23 5
    public function make($operator, $key = null, $reference = null)
24
    {
25
        switch ($operator) {
26
            // Leaf expressions
27 5
            case Type::EQ:
28 5
            case Type::GT:
29 5
            case Type::GTE:
30 5
            case Type::LT:
31 5
            case Type::LTE:
32 5
            case Type::IN:
33 5
            case Type::REGEX:
34 1
                $class = $this->classFromOperator('Leaf', $operator);
35
36 1
                return new $class($key, $reference);
37
38
            // Tree expressions
39 4
            case Type::AND_MATCH:
40 4
            case Type::OR_MATCH:
41 4
            case Type::NOT:
42 1
                $class = $this->classFromOperator('Tree', $operator);
43
44 1
                return new $class();
45
46
            // Negations
47 3
            case Type::NE:
48 1
                return new NotExpression([new EqExpression($key, $reference)]);
49 2
            case Type::NIN:
50 1
                return new NotExpression([new InExpression($key, $reference)]);
51
52 1
            default:
53 1
                throw new \Exception("Unknown operator `$operator`.");
54 1
        }
55
    }
56
57 2
    protected function classFromOperator($prefix, $operator)
58
    {
59 2
        return sprintf(
60 2
            '%s\%s\%sExpression',
61 2
            $this->namespace,
62 2
            $prefix,
63 2
            ucfirst(ltrim($operator, '$'))
64 2
        );
65
    }
66
}
67