Completed
Pull Request — master (#66)
by
unknown
01:44
created

BaseCommand::extractClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace Wn\Generators\Commands;
2
3
use Illuminate\Console\Command;
4
use Illuminate\Filesystem\Filesystem;
5
use Wn\Generators\Argument\ArgumentFormatLoader;
6
use Wn\Generators\Argument\ArgumentParser;
7
use Wn\Generators\Template\TemplateLoader;
8
9
10
class BaseCommand extends Command {
11
12
    protected $fs;
13
	protected $templates;
14
15
	public function __construct(Filesystem $fs)
16
	{
17
        parent::__construct();
18
19
        $this->fs = $fs;
20
        $this->templates = new TemplateLoader($fs);
21
        $this->argumentFormatLoader = new ArgumentFormatLoader($fs);
0 ignored issues
show
Bug introduced by
The property argumentFormatLoader 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...
22
    }
23
24
    protected function getTemplate($name)
25
    {
26
        return $this->templates->load($name);
27
    }
28
29
    protected function getArgumentParser($name){
30
        $format = $this->argumentFormatLoader->load($name);
31
        return new ArgumentParser($format);
32
    }
33
34
    protected function save($content, $path, $name, $force = false)
35
    {
36
        if (!$force && $this->fs->exists($path) && $this->input->hasOption('force') && !$this->option('force')) {
37
            $this->info("{$name} already exists; use --force option to override it !");
38
            return;
39
        }
40
        $this->makeDirectory($path);
41
        $this->fs->put($path, $content);
42
        $this->info("{$name} generated in {$path}!");
43
    }
44
45
    protected function makeDirectory($path)
46
    {
47
        if (!$this->fs->isDirectory(dirname($path))) {
48
            $this->fs->makeDirectory(dirname($path), 0777, true, true);
49
        }
50
    }
51
52
    protected function spaces($n): string
53
    {
54
        return str_repeat(' ', $n);
55
    }
56
57
    protected function getNamespace(string $path = null): string
58
    {
59
		if (! $path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
60
			$path = $this->option('path');
61
		}
62
63
    	return implode('\\', array_map('studly_case', array_filter(array_map('trim', explode('/', $path)), function($value) {
64
            return !empty($value);
65
        })));
66
    }
67
68
    protected function parseValue($value, string $parser)
69
    {
70
        if(! $value){
71
            return false;
72
        }
73
74
        if(! $this->option('parsed')){
75
            return $this->getArgumentParser($parser)->parse($value);
76
        }
77
78
        return $value;
79
    }
80
81
    protected function prependNamespace(string $value, string $path = null): string
82
    {
83
        if (strpos($value, '\\') === false) {
84
            return $this->getNamespace($path) . '\\' . studly_case(str_singular($value));
85
        }
86
87
        return $value;
88
    }
89
90
    protected function extractClassName(string $fqClassName): string
91
    {
92
        $names = array_reverse(explode("\\", $fqClassName));
93
        return $names[0];
94
    }
95
96
}
97