Completed
Push — master ( 7fd5f6...af8ac7 )
by James Ekow Abaka
01:40
created

Resolver::getJunctionClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 9
cp 0
cc 2
eloc 11
nc 2
nop 2
crap 6
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\nibii\interfaces\ModelClassResolverInterface;
6
use ntentan\nibii\interfaces\ModelJoinerInterface;
7
use ntentan\nibii\interfaces\TableNameResolverInterface;
8
use ntentan\config\Config;
9
use ntentan\utils\Text;
10
11
/**
12
 * Description of DefaultClassResolver
13
 *
14
 * @author ekow
15
 */
16
class Resolver implements ModelClassResolverInterface, ModelJoinerInterface, TableNameResolverInterface 
17
{
18
    public function getModelClassName($className, $context) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
19
        return $className;
20
    }
21
22
    private function getClassFileDetails($className) {
23
        $arrayed = explode('\\', $className);
24
        $class = array_pop($arrayed);
25
        if ($arrayed[0] == '') {
26
            array_shift($arrayed);
27
        }
28
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
29
    }
30
31
    public function getJunctionClassName($classA, $classB) {
32
        $classA = $this->getClassFileDetails($classA);
33
        $classB = $this->getClassFileDetails($classB);
34
        if ($classA['namespace'] != $classB['namespace']) {
35
            throw new NibiiException(
36
            "Cannot automatically join two classes of different "
37
            . "namespaces. Please provide a model joiner or "
38
            . "explicitly specify your joint model."
39
            );
40
        }
41
        $classes = [$classA['class'], $classB['class']];
42
        sort($classes);
43
        return "{$classA['namespace']}\\" . implode('', $classes);
44
    }
45
46
    public function getTableName($instance) {
47
        $class = new \ReflectionClass($instance);
48
        $nameParts = explode("\\", $class->getName());
49
        return \ntentan\utils\Text::deCamelize(end($nameParts));
50
    }
51
52 37
    public static function getDriverAdapterClassName($driver = false) {
53 37
        if ($driver) {
54 37
            return __NAMESPACE__ . '\adapters\\' . Text::ucamelize($driver) . 'Adapter';
0 ignored issues
show
Documentation introduced by
$driver is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        }
56
        throw new NibiiException("Please specify a driver");
57
    }
58
59
}
60