Completed
Push — master ( b54f87...bde51f )
by Beniamin
04:50
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 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 9.2
cc 4
eloc 9
nc 4
nop 1
crap 4.016
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 29
    public function recognizeType($stuff)
32
    {
33 29
        switch (true) {
34 29
            case $stuff instanceof \Closure:
35 1
                return static::TYPE_CLOSURE;
36 29
            case $stuff instanceof AbstractBuilder:
37 2
                return static::TYPE_SUB_QUERY;
38 29
            case false !== strpos($stuff, '\\'):
39 1
                return static::TYPE_CLASS_NAME;
40
        }
41
42 29
        return static::TYPE_TABLE_NAME;
43
    }
44
45
    /**
46
     * @param \Closure $closure
47
     *
48
     * @return string
49
     */
50
    public function extractClassName(\Closure $closure)
51
    {
52
        $ref = new \ReflectionFunction($closure);
53
        $firstParameter = $ref->getParameters()[0];
54
55
        return $firstParameter->getClass()->getName();
56
    }
57
}