Passed
Push — master ( 23ad8b...41e1cb )
by Bruno
05:36
created

GeneratorNameTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 24
dl 0
loc 81
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStudlyName() 0 3 1
A splitClassName() 0 7 1
A getInflector() 0 7 2
A setName() 0 6 1
A getName() 0 3 1
A getBasePath() 0 7 2
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use Doctrine\Inflector\InflectorFactory;
6
use GraphQL\Type\Definition\Type;
7
use Illuminate\Support\Str;
8
use Modelarium\Exception\Exception;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\Parser;
11
12
trait GeneratorNameTrait
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $name = '';
18
19
    /**
20
     * @var string
21
     */
22
    protected $studlyName = '';
23
24
    /**
25
     * @var string
26
     */
27
    protected $lowerName = '';
28
29
    /**
30
     * @var string
31
     */
32
    protected $lowerNamePlural = '';
33
34
    public function getInflector(): \Doctrine\Inflector\Inflector
35
    {
36
        static $inflector = null;
37
        if (!$inflector) {
38
            $inflector = InflectorFactory::create()->build();
39
        }
40
        return $inflector;
41
    }
42
43
    protected function setName(string $name): void
44
    {
45
        $this->name = $name;
46
        $this->studlyName = Str::studly($this->name);
47
        $this->lowerName = mb_strtolower($this->name);
48
        $this->lowerNamePlural = $this->getInflector()->pluralize($this->lowerName);
49
    }
50
51
    protected function splitClassName(string $fullclass): array
52
    {
53
        $classTokens = explode('\\', $fullclass);
54
        $className = array_pop($classTokens);
55
        $classNamespace = implode('\\', $classTokens);
56
        $relativePath = implode('/', $classTokens);
57
        return [$classNamespace, $className, $relativePath];
58
    }
59
60
    /**
61
     * Returns the base path (where composer.json is located)
62
     *
63
     * @param string $file The filename
64
     * @return string
65
     */
66
    public static function getBasePath(string $file = null): string
67
    {
68
        $basepath = dirname(\Composer\Factory::getComposerFile());
69
        if ($file) {
70
            $basepath .= '/' . $file;
71
        }
72
        return $basepath;
73
    }
74
75
    /**
76
     * Get the value of name
77
     *
78
     * @return  string
79
     */
80
    public function getName()
81
    {
82
        return $this->name;
83
    }
84
85
    /**
86
     * Get the value of studlyName
87
     *
88
     * @return  string
89
     */
90
    public function getStudlyName()
91
    {
92
        return $this->studlyName;
93
    }
94
}
95