Passed
Push — master ( 30151a...dbd1b9 )
by Chris
14:08
created

HopliteCommand::resolveAbstractDir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Leonidas\Console\Command;
4
5
use Jawira\CaseConverter\CaseConverter;
6
use Leonidas\Library\Core\Abstracts\ConvertsCaseTrait;
7
use Noodlehaus\Config;
8
use PHP_Parallel_Lint\PhpConsoleColor\ConsoleColor;
9
use PHP_Parallel_Lint\PhpConsoleHighlighter\Highlighter;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
abstract class HopliteCommand extends Command
17
{
18
    use ConvertsCaseTrait;
19
20
    protected Config $config;
21
22
    protected InputInterface $input;
23
24
    protected SymfonyStyle $output;
25
26
    protected Config $composerConfig;
27
28
    protected Filesystem $filesystem;
29
30
    protected Highlighter $highlighter;
31
32
    public function __construct(string $name = null)
33
    {
34
        $this->config = new Config($this->external('/hoplite.yml'));
35
        $this->composerConfig = new Config($this->external('/composer.json'));
36
        $this->filesystem = new Filesystem();
37
        $this->highlighter = new Highlighter(new ConsoleColor());
38
        $this->caseConverter = new CaseConverter();
39
40
        parent::__construct($name);
41
    }
42
43
    public function run(InputInterface $input, OutputInterface $output)
44
    {
45
        $this->input = $input;
46
        $this->output = new SymfonyStyle($input, $output);
47
48
        return parent::run($this->input, $this->output);
49
    }
50
51
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53
        return $this->handle();
54
    }
55
56
    protected function config(string $key, $default = null)
57
    {
58
        return $this->config->get($key, $default);
59
    }
60
61
    protected function composer(string $key, $default = null)
62
    {
63
        return $this->composerConfig->get($key, $default);
64
    }
65
66
    protected function getNamespaceFromPath(string $path): string
67
    {
68
        // todo: match completely
69
        $namespaces = array_flip($this->composer('autoload.psr-4'));
0 ignored issues
show
Bug introduced by
It seems like $this->composer('autoload.psr-4') can also be of type null; however, parameter $array of array_flip() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

69
        $namespaces = array_flip(/** @scrutinizer ignore-type */ $this->composer('autoload.psr-4'));
Loading history...
70
        $parts = explode('/', $path);
71
        $entry = array_shift($parts);
72
        $base = $namespaces[$entry . '/'];
73
74
        return $base . implode('\\', $parts);
75
    }
76
77
    protected function getPathFromNamespace(string $namespace): string
78
    {
79
        // todo: match completely
80
        $namespaces = $this->composer('autoload.psr-4');
81
        $parts = explode('\\', $namespace);
82
        $entry = array_shift($parts);
83
        $base = $namespaces[$entry . '\\'];
84
85
        return $base . implode('/', $parts);
86
    }
87
88
    protected function resolveAbstractNamespace(string $namespace): string
89
    {
90
        return $this->config('abstract_dir', true)
91
            ? $namespace . '\\Abstracts'
92
            : $namespace;
93
    }
94
95
    protected function resolveAbstractDir(string $dir): string
96
    {
97
        return $this->config('abstract_dir', true)
98
            ? $dir . DIRECTORY_SEPARATOR . 'Abstracts'
99
            : $dir;
100
    }
101
102
    protected function configurableArgument(string $option, string $configKey, $default = null)
103
    {
104
        return $this->input->getArgument($option)
105
            ?? $this->config($configKey, $default);
106
    }
107
108
    protected function configurableOption(string $option, string $configKey, $default = null)
109
    {
110
        return $this->input->getOption($option)
111
            ?? $this->config($configKey, $default);
112
    }
113
114
    protected function internal(string $path = ''): string
115
    {
116
        return dirname(__DIR__, 1) . $path;
117
    }
118
119
    protected function external(string $path = ''): string
120
    {
121
        return getcwd() . $path;
122
    }
123
124
    protected function printPhp(string $code): void
125
    {
126
        echo $this->highlighter->getWholeFile($code);
127
    }
128
129
    protected function writeFile(string $path, string $content): void
130
    {
131
        $this->filesystem->dumpFile($path, $content);
132
    }
133
134
    protected function writePhpFile(string $path, string $name, string $content): void
135
    {
136
        $this->writeFile($this->phpFile($path, $name), $content);
137
    }
138
139
    protected function writePhpFileRel(string $path, string $name, string $content): void
140
    {
141
        $this->writeFile($this->phpFileRel($path, $name), $content);
142
    }
143
144
    protected function phpFile(string $path, string $name): string
145
    {
146
        return $path . DIRECTORY_SEPARATOR . $name . '.php';
147
    }
148
149
    protected function phpFileRel(string $path, string $name): string
150
    {
151
        return $this->phpFile($this->external(DIRECTORY_SEPARATOR . $path), $name);
152
    }
153
154
    abstract protected function handle(): int;
155
}
156