Passed
Push — master ( b3365b...65b7f9 )
by Chris
16:01
created

HopliteCommand::resolveAbstractNamespace()   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 configuredArgument(string $option, string $configKey, $default = null)
96
    {
97
        return $this->input->getArgument($option)
98
            ?? $this->config($configKey, $default);
99
    }
100
101
    protected function configuredOption(string $option, string $configKey, $default = null)
102
    {
103
        return $this->input->getOption($option)
104
            ?? $this->config($configKey, $default);
105
    }
106
107
    protected function internal(string $path = ''): string
108
    {
109
        return dirname(__DIR__, 1) . $path;
110
    }
111
112
    protected function external(string $path = ''): string
113
    {
114
        return getcwd() . $path;
115
    }
116
117
    protected function printPhp(string $code): void
118
    {
119
        echo $this->highlighter->getWholeFile($code);
120
    }
121
122
    protected function writeFile(string $path, string $content): void
123
    {
124
        $this->filesystem->dumpFile($path, $content);
125
    }
126
127
    abstract protected function handle(): int;
128
}
129