GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DbSeeding   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 194
Duplicated Lines 3.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 4
dl 6
loc 194
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 17 3
C convert() 6 77 17
A compile() 0 11 1
A insertPropertyAndValue() 0 16 3
A hasTableData() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Elimuswift\DbExporter;
4
5
use Config;
6
use DB;
7
use Illuminate\Support\Str;
8
use File;
9
10
class DbSeeding extends DbExporter
11
{
12
    /**
13
     * @var string
14
     */
15
    public $database;
16
17
    /**
18
     * @var string
19
     */
20
    public $filename;
21
22
    /**
23
     * @var string
24
     */
25
    protected $seedingStub;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $customDb = false;
31
32
    /**
33
     * Set the database name.
34
     *
35
     * @param string $database
36
     */
37
    public function __construct($database)
38
    {
39
        $this->database = $database;
40
    }
41
42
//end __construct()
43
44
    /**
45
     * Write the seed file.
46
     */
47
    public function write()
48
    {
49
        // Check if convert method was called before
50
        // If not, call it on default DB
51
        if (!$this->customDb) {
52
            $this->convert();
53
        }
54
55
        foreach ($this->seedingStub as $table => $value) 
0 ignored issues
show
Bug introduced by
The expression $this->seedingStub of type string is not traversable.
Loading history...
56
        {
57
            $seed = $this->compile($table);
58
            $absolutePath = Config::get('db-exporter.export_path.seeds');
59
            $this->filename = ucfirst(Str::camel($table)) . 'DatabaseSeeder';
60
            $this->makePath($absolutePath);
61
            file_put_contents($absolutePath . "/{$this->filename}.php", $seed);
62
        }
63
    }
64
65
//end write()
66
67
    /**
68
     * Convert the database tables to something usefull.
69
     *
70
     * @param null $database
71
     *
72
     * @return $this
73
     */
74
    public function convert($database = null)
75
    {
76
        if (!is_null($database)) {
77
            $this->database = $database;
78
            $this->customDb = true;
79
        }
80
81
        // Get the tables for the database
82
        $tables = $this->getTables();
83
        $result = [];
84
85
        // Get tables to ignore
86
        $config = config('db-exporter.seeds');
87
        $ignore_tables = collect([]);
88 View Code Duplication
        if(!is_null($config) && isset($config['ignore_tables']) && !is_null($config['ignore_tables'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            $ignore_tables = collect($config['ignore_tables']);
90
        }
91
92
        $show_tables = collect([]);
93 View Code Duplication
        if(!is_null($config) && isset($config['use_tables']) && !is_null($config['use_tables'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
            $show_tables = collect($config['use_tables']);
95
        }
96
97
        // Loop over the tables
98
        foreach ($tables as $key => $value) 
99
        {
100
            if($show_tables->count() > 0 && !$show_tables->contains($value['table_name'])) {
101
                continue;
102
            }
103
104
            if($ignore_tables->contains($value['table_name'])) {
105
                continue;
106
            }
107
108
            // Do not export the ignored tables
109
            if (in_array($value['table_name'], self::$ignore)) {
110
                continue;
111
            }
112
113
            $tableName = $value['table_name'];
114
            $tableData = $this->getTableData($value['table_name']);
115
            $insertStub = '';
116
117
            foreach ($tableData as $obj) {
118
                $insertStub .= "
119
            [\n";
120
                foreach ($obj as $prop => $value) {
121
                    $insertStub .= $this->insertPropertyAndValue($prop, $value);
122
                }
123
124
                if (count($tableData) > 1) {
125
                    $insertStub .= "            ],\n";
126
                } else {
127
                    $insertStub .= "            ]\n";
128
                }
129
            }
130
131
            $insertStub = "
132
        \$data = [
133
            {$insertStub}
134
        ];";
135
136
            if ($this->hasTableData($tableData)) {
137
                $stub = $insertStub.'
138
139
        foreach($data as $item) 
140
        {
141
            $this->saveData("'.$tableName.'", $item);
142
        }'; 
143
                $result[$tableName] = $stub; 
144
            }
145
        }//end foreach
146
147
        $this->seedingStub = $result;
0 ignored issues
show
Documentation Bug introduced by
It seems like $result of type array is incompatible with the declared type string of property $seedingStub.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
148
149
        return $this;
150
    }
151
152
//end convert()
153
154
    /**
155
     * Compile the current seedingStub with the seed template.
156
     *
157
     * @return mixed
158
     */
159
    protected function compile($table)
160
    {
161
        // Grab the template
162
        $template = File::get(__DIR__ . '/stubs/seed.stub');
163
164
        // Replace the classname
165
        $template = str_replace('{{className}}', ucfirst(Str::camel($table)) . 'DatabaseSeeder', $template);
166
        $template = str_replace('{{run}}', $this->seedingStub[$table], $template);
167
168
        return $template;
169
    }
170
171
//end compile()
172
173
    private function insertPropertyAndValue($prop, $value)
174
    {
175
        if(strlen($prop) > 0) {
176
            $prop = "'{$prop}'";
177
        } else {
178
            $prop = 'null';
179
        }
180
181
        if(strlen($value) > 0) {
182
            $value = str_replace("'", "\'", $value);
183
            $value = "'{$value}'";
184
        } else {
185
            $value = 'null';
186
        }
187
        return "                {$prop} => {$value},\n";
188
    }
189
190
//end insertPropertyAndValue()
191
192
    /**
193
     * @param $tableData
194
     *
195
     * @return bool
196
     */
197
    public function hasTableData($tableData)
198
    {
199
        return count($tableData) >= 1;
200
    }
201
202
//end hasTableData()
203
}//end class
204