Passed
Push — master ( 0a3787...bc905b )
by Mihail
06:05
created

Apps/Console/MigrationCreateCommand.php (1 issue)

Labels
Severity
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\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Class MigrationCreateCommand. Create new migrations files
17
 * @package Apps\Console
18
 */
19
class MigrationCreateCommand extends Command
20
{
21
    /**
22
     * Configure command
23
     */
24
    public function configure()
25
    {
26
        $this->setName('migration:create')
27
            ->setDescription('Create new migration for cms database')
28
            ->addArgument('name', InputArgument::REQUIRED, 'Set name of new migration. Example: create_demo_table')
29
            ->addOption('dir', 'dir', InputOption::VALUE_OPTIONAL, 'Set output directory for new migration file. Example: /vendor/myname/package/migrations');
30
    }
31
32
    /**
33
     * Create new migration php file based on carcase template
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @return void
37
     */
38
    public function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        // get migration name from params
41
        $name = $input->getArgument('name');
42
        $name = Str::lowerCase($name);
0 ignored issues
show
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

42
        $name = Str::lowerCase(/** @scrutinizer ignore-type */ $name);
Loading history...
43
        // get output directory
44
        $dir = $this->option('dir');
45
        if ($dir === null || Str::likeEmpty($dir)) {
46
            $dir = '/Private/Migrations/';
47
        } else {
48
            $dir = rtrim($dir, '\\/');
49
            $dir .= '/';
50
        }
51
        // parse table name
52
        list ($action, $table, $etc) = explode('_', $name);
53
        if ($table === null || Str::likeEmpty($table)) {
54
            $table = 'table';
55
        }
56
        // create suffix for filename
57
        $suffix = date('Y-m-d-H-i-s');
58
        // work with migration template: read & parse & save
59
        $tpl = File::read('/Private/Carcase/Migration.tphp');
60
        $classContent = Str::replace(['%class%', '%table%'], [$name, $table], $tpl);
61
        $fullPath = $dir . $name . '-' . $suffix . '.php';
62
        File::write($fullPath, $classContent);
63
        // show success msg
64
        $output->write('New migration is created: ' . $fullPath);
65
    }
66
}