This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace App\Console\Commands; |
||
4 | |||
5 | use Illuminate\Database\Capsule\Manager as Capsule; |
||
6 | use Symfony\Component\Console\Command\Command; |
||
7 | use Symfony\Component\Console\Input\InputInterface; |
||
8 | use Symfony\Component\Console\Output\OutputInterface; |
||
9 | use Symfony\Component\Console\Question\Question; |
||
10 | use App\Common\Helper; |
||
11 | use App\Console\Traits\CodeGenerate; |
||
12 | |||
13 | /** |
||
14 | * GenerateSchemaCommand |
||
15 | */ |
||
16 | class GenerateSchemaCommand extends Command |
||
17 | { |
||
18 | use CodeGenerate; |
||
19 | |||
20 | /** |
||
21 | * Configuration of command |
||
22 | */ |
||
23 | protected function configure() |
||
24 | { |
||
25 | $this |
||
26 | ->setName('generate:schema') |
||
27 | ->setDescription('Command for generate schema') |
||
28 | ; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Execute method of command |
||
33 | * |
||
34 | * @param InputInterface $input |
||
35 | * @param OutputInterface $output |
||
36 | * |
||
37 | * @return void |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | protected function execute(InputInterface $input, OutputInterface $output) |
||
41 | { |
||
42 | $output->writeln(['<comment>Welcome to the schema generator</comment>']); |
||
43 | |||
44 | $helper = $this->getHelper('question'); |
||
45 | $question = new Question('<info>Please enter table name: </info>'); |
||
46 | $tableName = $helper->ask($input, $output, $question); |
||
47 | $tableInfo = Capsule::schema()->getColumnListing($tableName); |
||
48 | if (count($tableInfo) === 0) { |
||
49 | $output->writeln([sprintf('<comment>Not found table `%s`</comment>', $tableName)]); |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $columns = $this->getColumnsInfo($tableInfo, $tableName); |
||
54 | |||
55 | $modelName = substr($tableName, 0, -1); |
||
56 | $className = Helper::underscoreToCamelCase($modelName, true).'Schema'; |
||
57 | $baseName = $className.'.php'; |
||
58 | $path = $this->getPath($baseName, SCHEMAS_PATH); |
||
59 | $resourceType = str_replace('_', '-', strtolower($modelName)); |
||
60 | |||
61 | $placeHolders = [ |
||
62 | '<class>', |
||
63 | '<resourceType>', |
||
64 | '<resourceTypeInCamelCase>', |
||
65 | '<attributes>', |
||
66 | '<params>', |
||
67 | '<attributesToClass>', |
||
68 | ]; |
||
69 | $replacements = [ |
||
70 | $className, |
||
71 | str_replace('_', '-', strtolower($modelName)), |
||
72 | Helper::dashesToCamelCase($resourceType, true), |
||
73 | $this->generateAttributes($columns), |
||
74 | $this->generateParams($columns), |
||
75 | $this->generateAttributesToClass($columns), |
||
76 | ]; |
||
77 | |||
78 | $this->generateCode($placeHolders, $replacements, 'SchemaTemplate.tpl', $path); |
||
79 | |||
80 | $output->writeln(sprintf('Generated new schema class to "<info>%s</info>"', realpath($path))); |
||
81 | |||
82 | return; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param array $tableInfo |
||
87 | * @param string $tableName |
||
88 | * @return array |
||
89 | */ |
||
90 | private function getColumnsInfo($tableInfo, $tableName) |
||
91 | { |
||
92 | $columns = []; |
||
93 | View Code Duplication | foreach ($tableInfo as $columnName) { |
|
0 ignored issues
–
show
|
|||
94 | $columnType = Capsule::schema()->getColumnType($tableName, $columnName); |
||
95 | |||
96 | $columns[] = [ |
||
97 | 'name' => $columnName, |
||
98 | 'type' => $columnType, |
||
99 | 'fake' => $this->getFakeData($columnType), |
||
100 | ]; |
||
101 | } |
||
102 | |||
103 | return $columns; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return fake data for examples |
||
108 | * @param $columnType |
||
109 | * @return string |
||
110 | */ |
||
111 | private function getFakeData($columnType) |
||
112 | { |
||
113 | switch ($columnType) { |
||
114 | case 'string': |
||
115 | case 'text': |
||
116 | $fake = '"String"'; |
||
117 | break; |
||
118 | case 'integer': |
||
119 | $fake = '1'; |
||
120 | break; |
||
121 | case 'decimal': |
||
122 | $fake = '1.0'; |
||
123 | break; |
||
124 | case 'datetime': |
||
125 | $fake = '"2016-10-17T07:38:21+0000"'; |
||
126 | break; |
||
127 | default: |
||
128 | $fake = ''; |
||
129 | } |
||
130 | |||
131 | return $fake; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param array $columns |
||
136 | * @return string |
||
137 | */ |
||
138 | private function generateAttributes($columns) |
||
139 | { |
||
140 | $attributes = []; |
||
141 | $counter = 1; |
||
142 | foreach ($columns as $column) { |
||
143 | if ($column['name'] === 'id') { |
||
144 | continue; |
||
145 | } |
||
146 | $counter++; |
||
147 | if (count($columns) !== $counter) { |
||
148 | $attributes[] = sprintf(' * "%s": %s,', $column['name'], $column['fake']); |
||
149 | } else { |
||
150 | $attributes[] = sprintf(' * "%s": %s', $column['name'], $column['fake']); |
||
151 | } |
||
152 | }; |
||
153 | |||
154 | return implode("\n", $attributes); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param array $columns |
||
159 | * @return string |
||
160 | */ |
||
161 | private function generateParams($columns) |
||
162 | { |
||
163 | $params = []; |
||
164 | foreach ($columns as $column) { |
||
165 | if ($column['name'] === 'id') { |
||
166 | continue; |
||
167 | } |
||
168 | |||
169 | $params[] = sprintf(' * @apiParam {%s} %s', ucfirst($column['type']), $column['name']); |
||
170 | }; |
||
171 | |||
172 | return implode("\n", $params); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param array $columns |
||
177 | * @return string |
||
178 | */ |
||
179 | private function generateAttributesToClass($columns) |
||
180 | { |
||
181 | $attributes = []; |
||
182 | foreach ($columns as $column) { |
||
183 | if ($column['name'] === 'id') { |
||
184 | continue; |
||
185 | } |
||
186 | if ($column['type'] === 'datetime') { |
||
187 | $attributes[] = sprintf(" '%s' => Carbon::parse(\$entity->%s)->setTimezone('UTC')->format(Carbon::ISO8601),", $column['name'], $column['name']); |
||
188 | } else { |
||
189 | $attributes[] = sprintf(" '%s' => (%s)\$entity->%s,", $column['name'], $column['type'], $column['name']); |
||
190 | } |
||
191 | }; |
||
192 | |||
193 | return implode("\n", $attributes); |
||
194 | } |
||
195 | } |
||
196 |
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.