Completed
Push — master ( 096858...7ec34d )
by Gabriel
03:50
created

HasModelNameRecordsTrait::generateModelClass()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
1
<?php
2
3
namespace Nip\Records\Traits\HasModelName;
4
5
/**
6
 * Trait HasModelNameRecordsTrait
7
 * @package Nip\Records\Traits\HasModelName
8
 */
9
trait HasModelNameRecordsTrait
10
{
11
12
    /**
13
     * Model class name
14
     * @var null|string
15
     */
16
    protected $model = null;
17
18
    /**
19
     * @var null|string
20
     */
21
    protected $modelNamespacePath = null;
22
23
    /**
24
     * @return string
25
     */
26 10
    public function getModelNamespacePath()
27
    {
28 10
        if ($this->modelNamespacePath == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->modelNamespacePath of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
29 10
            $this->initModelNamespacePath();
30
        }
31
32 10
        return $this->modelNamespacePath;
33
    }
34
35 10
    public function initModelNamespacePath()
36
    {
37 10
        if ($this->isNamespaced()) {
0 ignored issues
show
Bug introduced by
It seems like isNamespaced() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        if ($this->/** @scrutinizer ignore-call */ isNamespaced()) {
Loading history...
38 10
            $path = $this->generateModelNamespacePathFromClassName() . '\\';
39
        } else {
40
            $controller = $this->generateControllerGeneric();
0 ignored issues
show
Bug introduced by
It seems like generateControllerGeneric() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            /** @scrutinizer ignore-call */ 
41
            $controller = $this->generateControllerGeneric();
Loading history...
41
            $path = inflector()->classify($controller) . '\\';
42
        }
43 10
        $this->modelNamespacePath = $path;
44 10
    }
45
46
    /**
47
     * @return string
48
     */
49 10
    protected function generateModelNamespacePathFromClassName()
50
    {
51 10
        $className = $this->getClassName();
0 ignored issues
show
Bug introduced by
It seems like getClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        /** @scrutinizer ignore-call */ 
52
        $className = $this->getClassName();
Loading history...
52 10
        $rootNamespace = $this->getRootNamespace();
0 ignored issues
show
Bug introduced by
It seems like getRootNamespace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        /** @scrutinizer ignore-call */ 
53
        $rootNamespace = $this->getRootNamespace();
Loading history...
53 10
        $path = str_replace($rootNamespace, '', $className);
54
55 10
        $nsParts = explode('\\', $path);
56 10
        array_pop($nsParts);
57
58 10
        return implode($nsParts, '\\');
0 ignored issues
show
Bug introduced by
'\' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        return implode($nsParts, /** @scrutinizer ignore-type */ '\\');
Loading history...
59
    }
60
61
    /**
62
     * @return string
63
     */
64 6
    public function getModel()
65
    {
66 6
        if ($this->model == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->model of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
67 2
            $this->inflectModel();
68
        }
69
70 6
        return $this->model;
71
    }
72
73
    /**
74
     * @param null $model
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $model is correct as it would always require null to be passed?
Loading history...
75
     */
76 2
    public function setModel($model)
77
    {
78 2
        $this->model = $model;
79 2
    }
80
81 2
    protected function inflectModel()
82
    {
83 2
        $class = $this->getClassName();
84 2
        $this->model = $this->generateModelClass($class);
85 2
    }
86
87
    /**
88
     * @param string $class
89
     * @return string
90
     */
91 3
    public function generateModelClass($class = null)
92
    {
93 3
        $class = $class ? $class : get_class($this);
94
95 3
        if (strpos($class, '\\')) {
96 3
            $nsParts = explode('\\', $class);
97 3
            $class = array_pop($nsParts);
98
99 3
            if ($class == 'Table') {
100 1
                $class = 'Row';
101
            } else {
102 2
                $class = ucfirst(inflector()->singularize($class));
103
            }
104
105 3
            return implode($nsParts, '\\') . '\\' . $class;
0 ignored issues
show
Bug introduced by
'\' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

105
            return implode($nsParts, /** @scrutinizer ignore-type */ '\\') . '\\' . $class;
Loading history...
106
        }
107
108 1
        return ucfirst(inflector()->singularize($class));
109
    }
110
}
111