Completed
Push — master ( 3e5311...09edda )
by Beniamin
05:08
created

TableRecognizer::recognizeType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
rs 8.8571
cc 5
eloc 11
nc 5
nop 1
crap 5.0144
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_ROUTE = 2;
11
    const TYPE_CLASS_NAME = 3;
12
    const TYPE_TABLE_NAME = 4;
13
    const TYPE_SUB_QUERY = 5;
14
15
    /**
16
     * @param mixed $stuff
17
     *
18
     * @return int
19
     */
20 24
    public function recognizeType($stuff)
21
    {
22 24
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($stuff, '\\') of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
23 24
            case $stuff instanceof \Closure:
24 1
                return static::TYPE_CLOSURE;
25 24
            case $stuff instanceof QueryBuilder:
26 2
                return static::TYPE_SUB_QUERY;
27 24
            case false !== strpos($stuff, '.'):
28 1
                return static::TYPE_ROUTE;
29 24
            case strpos($stuff, '\\'):
30 1
                return static::TYPE_CLASS_NAME;
31
        }
32
33 24
        return static::TYPE_TABLE_NAME;
34
    }
35
36
    /**
37
     * @param \Closure $closure
38
     *
39
     * @return string
40
     */
41
    public function extractClassName(\Closure $closure)
42
    {
43
        $ref = new \ReflectionFunction($closure);
44
        $firstParameter = $ref->getParameters()[0];
45
46
        return $firstParameter->getClass()->getName();
47
    }
48
}