Completed
Push — master ( 8bc203...e5859f )
by recca
09:43
created

src/Code.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Recca0120\Generator;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Filesystem\Filesystem;
8
use Recca0120\Generator\Fixers\UseSortFixer;
9
10
class Code
11
{
12
    private $name;
13
14
    private $className;
15
16
    private $config;
17
18
    private $attributes = [];
19
20
    private $dependencies = [];
21
22
    private $files;
23
24 3
    public function __construct($name, $config, $dependencies = [], Filesystem $files = null, UseSortFixer $useSortFixer = null)
25
    {
26 3
        $this->files = $files ?: new Filesystem;
27 3
        $this->useSortFixer = $useSortFixer ?: new UseSortFixer();
0 ignored issues
show
The property useSortFixer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28 3
        $this->useSortFixer->setSortType(UseSortFixer::SORT_TYPE_LENGTH);
29 3
        $this->name = $name;
30 3
        $this->config = $config;
31 3
        $this->dependencies = $dependencies;
32
33 3
        $this->className = $this->name.Arr::get($this->config, 'suffix', '');
34 3
        $this->attributes = $this->mergeAttributes($dependencies);
35 3
    }
36
37
    public function __toString()
38
    {
39
        return $this->render();
40
    }
41
42 1
    public function getAttributes($prefix = null)
43
    {
44 1
        if (empty($prefix) === true) {
45
            return $this->attributes;
46
        }
47
48 1
        $attributes = [];
49 1
        foreach ($this->attributes as $key => $value) {
50 1
            $attributes[$prefix.'_'.$key] = $value;
51 1
        }
52
53 1
        return $attributes;
54
    }
55
56 3
    public function render()
57
    {
58 3
        return $this->format($this->renderStub(), Arr::get($this->config, 'sort', true));
59
    }
60
61
    public function store()
62
    {
63
        foreach ($this->dependencies as $dependency) {
64
            $dependency->store();
65
        }
66
67
        $file = Arr::get($this->config, 'path', '').'/'.$this->className.'.'.Arr::get($this->config, 'extension', 'php');
68
        $directory = dirname($file);
69
70
        if ($this->files->isDirectory($directory) === false) {
71
            $this->files->makeDirectory($directory, 0755, true);
72
        }
73
74
        return $this->files->exists($file) === false
75
            ? $this->files->put($file, $this->render())
76
            : false;
77
    }
78
79 3
    private function getDummyAttributes()
80
    {
81 3
        $dummy = [];
82 3
        foreach ($this->attributes as $key => $value) {
83 3
            $dummy['Dummy'.Str::studly($key)] = $value;
84 3
            $dummy['dummy'.Str::studly($key)] = Str::camel($value);
85 3
        }
86
87 3
        return $dummy;
88
    }
89
90 3
    private function renderStub()
91
    {
92 3
        return strtr($this->files->get($this->config['stub']), $this->getDummyAttributes());
93
    }
94
95 3
    private function format($content, $useSort = false)
96
    {
97 3
        return $useSort === true ? $this->useSortFixer->fix($content) : $content;
98
    }
99
100 3
    private function mergeAttributes($dependencies)
101
    {
102 3
        $attributes = array_merge(Arr::get($this->config, 'attributes', []), [
103 3
            'name' => $this->name,
104 3
            'class' => $this->className,
105 3
        ]);
106
107 3
        if (empty($attributes['extends']) === false) {
108 2
            $attributes['base_extends'] = basename($attributes['extends']);
109 2
        }
110
111 3
        if (empty($attributes['namespace']) === false) {
112 3
            $attributes['fully_qualified_name'] = '\\'.$attributes['namespace'].'\\'.$attributes['class'];
113 3
            $attributes['qualified_name'] = $attributes['namespace'].'\\'.$attributes['class'];
114 3
        }
115
116 3
        foreach ($dependencies as $name => $dependency) {
117 1
            $attributes = array_merge($attributes, $dependency->getAttributes($name));
118 3
        }
119
120 3
        return $attributes;
121
    }
122
}
123