Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

MigrationCreateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Apps\Console;
4
5
6
use Ffcms\Console\Command;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Str;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * Class MigrationCreateCommand. Create new migrations files
16
 * @package Apps\Console
17
 */
18
class MigrationCreateCommand extends Command
19
{
20
    /**
21
     * Configure command
22
     */
23
    public function configure()
24
    {
25
        $this->setName('migration:create')
26
            ->setDescription('Create new migration for cms database')
27
            ->addArgument('name', InputArgument::REQUIRED, 'Set name of new migration. Example: create_demo_table')
28
            ->addOption('dir', 'dir', InputOption::VALUE_OPTIONAL, 'Set output directory for new migration file. Example: /vendor/myname/package/migrations');
29
    }
30
31
    /**
32
     * Create new migration php file based on carcase template
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return void
36
     */
37
    public function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        // get migration name from params
40
        $name = $input->getArgument('name');
41
        $name = Str::lowerCase($name);
0 ignored issues
show
Bug introduced by zenn
It seems like $name can also be of type string[]; however, parameter $string of Ffcms\Core\Helper\Type\Str::lowerCase() does only seem to accept null|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

41
        $name = Str::lowerCase(/** @scrutinizer ignore-type */ $name);
Loading history...
42
        // get output directory
43
        $dir = $this->option('dir');
44
        if ($dir === null || Str::likeEmpty($dir)) {
45
            $dir = '/Private/Migrations/';
46
        } else {
47
            $dir = rtrim($dir, '\\/');
48
            $dir .= '/';
49
        }
50
        // parse table name
51
        list ($action, $table, $etc) = explode('_', $name);
52
        if ($table === null || Str::likeEmpty($table)) {
53
            $table = 'table';
54
        }
55
        // create suffix for filename
56
        $suffix = date('Y-m-d-H-i-s');
57
        // work with migration template: read & parse & save
58
        $tpl = File::read('/Private/Carcase/Migration.tphp');
59
        $classContent = Str::replace(['%class%', '%table%'], [$name, $table], $tpl);
0 ignored issues
show
Bug introduced by zenn
It seems like $tpl can also be of type false; however, parameter $haystack of Ffcms\Core\Helper\Type\Str::replace() does only seem to accept null|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

59
        $classContent = Str::replace(['%class%', '%table%'], [$name, $table], /** @scrutinizer ignore-type */ $tpl);
Loading history...
60
        $fullPath = $dir . $name . '-' . $suffix . '.php';
61
        File::write($fullPath, $classContent);
62
        // show success msg
63
        $output->write('New migration is created: ' . $fullPath);
64
    }
65
}