Completed
Push — master ( b9b4e1...acec2e )
by James Ekow Abaka
02:32
created

Resolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 88%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 44
ccs 22
cts 25
cp 0.88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelClassName() 0 3 1
A getClassFileDetails() 0 8 2
A getJunctionClassName() 0 14 2
A getTableName() 0 5 1
A getDriverAdapterClassName() 0 6 2
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 14
    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 14
        return $className;
20
    }
21
22 4
    private function getClassFileDetails($className) {
23 4
        $arrayed = explode('\\', $className);
24 4
        $class = array_pop($arrayed);
25 4
        if ($arrayed[0] == '') {
26 4
            array_shift($arrayed);
27
        }
28 4
        return ['class' => $class, 'namespace' => implode('\\', $arrayed)];
29
    }
30
31 4
    public function getJunctionClassName($classA, $classB) {
32 4
        $classA = $this->getClassFileDetails($classA);
33 4
        $classB = $this->getClassFileDetails($classB);
34 4
        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 4
        $classes = [$classA['class'], $classB['class']];
42 4
        sort($classes);
43 4
        return "{$classA['namespace']}\\" . implode('', $classes);
44
    }
45
46 36
    public function getTableName($instance) {
47 36
        $class = new \ReflectionClass($instance);
48 36
        $nameParts = explode("\\", $class->getName());
49 36
        return \ntentan\utils\Text::deCamelize(end($nameParts));
50
    }
51
52 36
    public static function getDriverAdapterClassName($driver = false) {
53 36
        if ($driver) {
54 36
            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