Passed
Push — master ( f5c22c...929873 )
by Chris
16:12
created

HookCommand::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Leonidas\Console\Command\Make;
4
5
use Leonidas\Console\Command\HopliteCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class HookCommand extends HopliteCommand
12
{
13
    protected const STUB_NAMESPACE = 'Leonidas\\Console\\Stubs\\Hook';
14
15
    protected static $defaultName = 'make:hook';
16
17
    protected static $defaultDescription = 'Creates a hook helper class';
18
19
    protected function configure(): void
20
    {
21
        $this
22
            ->addArgument('tag', InputArgument::REQUIRED)
23
            ->addArgument('type', InputArgument::OPTIONAL, 'The type of hook to create', 'action')
24
            ->addOption('path', 'p', InputOption::VALUE_OPTIONAL);
25
    }
26
27
    protected function execute(InputInterface $input, OutputInterface $output): int
28
    {
29
        $tag = $input->getArgument('tag');
30
        $type = $input->getArgument('type');
31
        $converted = $this->convert($tag)->toPascal();
32
33
        $parts = explode('/', $this->config('make.hook.path'));
0 ignored issues
show
Bug introduced by
It seems like $this->config('make.hook.path') can also be of type null; however, parameter $string of explode() does only seem to accept string, 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

33
        $parts = explode('/', /** @scrutinizer ignore-type */ $this->config('make.hook.path'));
Loading history...
34
        $root = array_shift($parts);
35
        $namespace = implode('\\', $parts);
36
        $dir = $root . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array_slice($parts, 1));
37
        $filename = 'Targets' . $converted . 'Hook.php';
38
        $file = getcwd() . '/' . $dir . '/' . $filename;
39
40
        $template = $this->getTemplate($type);
41
42
        $replacements = [
43
            static::STUB_NAMESPACE => $namespace,
44
            'Dummy' . ucfirst($type) => $converted,
45
            'dummy_' . strtolower($type) => $tag,
46
        ];
47
48
        foreach ($replacements as $key => $value) {
49
            $template = str_replace($key, $value, $template);
50
        }
51
52
        if (!file_exists($file)) {
53
            touch($file);
54
        }
55
56
        file_put_contents($file, $template);
57
58
        return self::SUCCESS;
59
    }
60
61
    protected function getTemplate(string $type): string
62
    {
63
        return file_get_contents($this->getTemplateFile($type));
64
    }
65
66
    protected function getTemplateFile(string $type): string
67
    {
68
        return $this->internal('/Stubs/Hook/TargetsDummy' . ucfirst($type) . 'Hook.php');
69
    }
70
}
71