Completed
Push — master ( 1cbd4f...6f70d1 )
by Beniamin
07:58
created

TableRecognizer::recognizeType()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 5
Bugs 0 Features 3
Metric Value
c 5
b 0
f 3
dl 0
loc 13
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4.016
1
<?php
2
namespace Phuria\QueryBuilder;
3
4
/**
5
 * @author Beniamin Jonatan Šimko <[email protected]>
6
 */
7
class TableRecognizer
8
{
9
    const TYPE_CLOSURE = 1;
10
    const TYPE_CLASS_NAME = 2;
11
    const TYPE_TABLE_NAME = 3;
12
    const TYPE_SUB_QUERY = 4;
13
14
    /**
15
     * @param mixed $stuff
16
     *
17
     * @return int
18
     */
19 25
    public function recognizeType($stuff)
20
    {
21 25
        switch (true) {
22 25
            case $stuff instanceof \Closure:
23 1
                return static::TYPE_CLOSURE;
24 25
            case $stuff instanceof QueryBuilder:
25 2
                return static::TYPE_SUB_QUERY;
26 25
            case false !== strpos($stuff, '\\'):
27 1
                return static::TYPE_CLASS_NAME;
28
        }
29
30 25
        return static::TYPE_TABLE_NAME;
31
    }
32
33
    /**
34
     * @param \Closure $closure
35
     *
36
     * @return string
37
     */
38
    public function extractClassName(\Closure $closure)
39
    {
40
        $ref = new \ReflectionFunction($closure);
41
        $firstParameter = $ref->getParameters()[0];
42
43
        return $firstParameter->getClass()->getName();
44
    }
45
}