Completed
Push — master ( 3e5311...09edda )
by Beniamin
05:08
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_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
}