Passed
Push — master ( a70199...a4ba76 )
by Beniamin
03:44
created

TableRecognizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 39
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A recognizeType() 0 13 4
A extractClassName() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\SQLBuilder;
13
14
use Phuria\SQLBuilder\QueryBuilder\AbstractBuilder;
15
16
/**
17
 * @author Beniamin Jonatan Šimko <[email protected]>
18
 */
19
class TableRecognizer
20
{
21
    const TYPE_CLOSURE = 1;
22
    const TYPE_CLASS_NAME = 2;
23
    const TYPE_TABLE_NAME = 3;
24
    const TYPE_SUB_QUERY = 4;
25
26
    /**
27
     * @param mixed $stuff
28
     *
29
     * @return int
30
     */
31 1
    public function recognizeType($stuff)
32
    {
33 1
        switch (true) {
34 1
            case $stuff instanceof \Closure:
35 1
                return static::TYPE_CLOSURE;
36 1
            case $stuff instanceof AbstractBuilder:
37 1
                return static::TYPE_SUB_QUERY;
38 1
            case false !== strpos($stuff, '\\'):
39 1
                return static::TYPE_CLASS_NAME;
40
        }
41
42 1
        return static::TYPE_TABLE_NAME;
43
    }
44
45
    /**
46
     * @param \Closure $closure
47
     *
48
     * @return string
49
     */
50 1
    public function extractClassName(\Closure $closure)
51
    {
52 1
        $ref = new \ReflectionFunction($closure);
53 1
        $firstParameter = $ref->getParameters()[0];
54
55 1
        return $firstParameter->getClass()->getName();
56
    }
57
}