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

TableRecognizer::extractClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
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
}