Completed
Push — 6.0 ( 8cfa5f...e2c4d7 )
by liu
05:43
created

Schema   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 58
c 5
b 0
f 0
dl 0
loc 95
ccs 0
cts 61
cp 0
rs 10
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A buildDataBaseSchema() 0 13 3
B execute() 0 48 10
A buildModelSchema() 0 18 4
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: yunwuxin <[email protected]>
10
// +----------------------------------------------------------------------
11
namespace think\console\command\optimize;
12
13
use think\console\Command;
14
use think\console\Input;
15
use think\console\input\Argument;
16
use think\console\input\Option;
17
use think\console\Output;
18
19
class Schema extends Command
20
{
21
    protected function configure()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configure()
Loading history...
22
    {
23
        $this->setName('optimize:schema')
24
            ->addArgument('app', Argument::OPTIONAL, 'app name .')
25
            ->addOption('db', null, Option::VALUE_REQUIRED, 'db name .')
26
            ->addOption('table', null, Option::VALUE_REQUIRED, 'table name .')
27
            ->setDescription('Build database schema cache.');
28
    }
29
30
    protected function execute(Input $input, Output $output)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function execute()
Loading history...
31
    {
32
        $app = $input->getArgument('app');
33
34
        $schemaPath = $this->app->db->getConnection()->getConfig('schema_cache_path');
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on think\Db. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

34
        $schemaPath = $this->app->db->/** @scrutinizer ignore-call */ getConnection()->getConfig('schema_cache_path');
Loading history...
35
36
        if (!is_dir($schemaPath)) {
37
            mkdir($schemaPath, 0755, true);
38
        }
39
40
        if ($input->hasOption('table')) {
41
            $table = $input->getOption('table');
42
            if (false === strpos($table, '.')) {
0 ignored issues
show
Bug introduced by
It seems like $table can also be of type think\console\input\Option; however, parameter $haystack of strpos() 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

42
            if (false === strpos(/** @scrutinizer ignore-type */ $table, '.')) {
Loading history...
43
                $dbName = $this->app->db->getConnection()->getConfig('database');
44
            }
45
46
            $tables[] = $table;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$tables was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tables = array(); before regardless.
Loading history...
47
        } elseif ($input->hasOption('db')) {
48
            $dbName = $input->getOption('db');
49
            $tables = $this->app->db->getConnection()->getTables($dbName);
50
        } else {
51
            if ($app) {
52
                $appPath   = $this->app->getBasePath() . $app . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug introduced by
Are you sure $app of type mixed|think\console\input\Argument can be used in concatenation? ( Ignorable by Annotation )

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

52
                $appPath   = $this->app->getBasePath() . /** @scrutinizer ignore-type */ $app . DIRECTORY_SEPARATOR;
Loading history...
53
                $namespace = 'app\\' . $app;
54
            } else {
55
                $appPath   = $this->app->getBasePath();
56
                $namespace = 'app';
57
            }
58
59
            $path = $appPath . 'model';
60
            $list = is_dir($path) ? scandir($path) : [];
61
62
            foreach ($list as $file) {
63
                if (0 === strpos($file, '.')) {
64
                    continue;
65
                }
66
                $class = '\\' . $namespace . '\\model\\' . pathinfo($file, PATHINFO_FILENAME);
67
                $this->buildModelSchema($class);
68
            }
69
70
            $output->writeln('<info>Succeed!</info>');
71
            return;
72
        }
73
74
        $db = isset($dbName) ? $dbName . '.' : '';
75
        $this->buildDataBaseSchema($schemaPath, $tables, $db);
76
77
        $output->writeln('<info>Succeed!</info>');
78
    }
79
80
    protected function buildModelSchema(string $class): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function buildModelSchema()
Loading history...
81
    {
82
        $reflect = new \ReflectionClass($class);
83
        if (!$reflect->isAbstract() && $reflect->isSubclassOf('\think\Model')) {
84
            /** @var \think\Model $model */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
85
            $model = new $class;
86
87
            $table  = $model->getTable();
88
            $dbName = $model->getConnection()->getConfig('database');
89
            $path   = $model->getConnection()->getConfig('schema_cache_path');
90
            if (!is_dir($path)) {
91
                mkdir($path, 0755, true);
92
            }
93
            $content = '<?php ' . PHP_EOL . 'return ';
94
            $info    = $model->db()->getConnection()->getTableFieldsInfo($table);
0 ignored issues
show
Bug introduced by
The method getTableFieldsInfo() does not exist on think\db\Connection. ( Ignorable by Annotation )

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

94
            $info    = $model->db()->getConnection()->/** @scrutinizer ignore-call */ getTableFieldsInfo($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
            $content .= var_export($info, true) . ';';
96
97
            file_put_contents($path . $dbName . '.' . $table . '.php', $content);
98
        }
99
    }
100
101
    protected function buildDataBaseSchema(string $path, array $tables, string $db): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function buildDataBaseSchema()
Loading history...
102
    {
103
        if ('' == $db) {
104
            $dbName = $this->app->db->getConnection()->getConfig('database') . '.';
105
        } else {
106
            $dbName = $db;
107
        }
108
109
        foreach ($tables as $table) {
110
            $content = '<?php ' . PHP_EOL . 'return ';
111
            $info    = $this->app->db->getConnection()->getTableFieldsInfo($db . $table);
112
            $content .= var_export($info, true) . ';';
113
            file_put_contents($path . $dbName . $table . '.php', $content);
114
        }
115
    }
116
}
117