consigliere /
components
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 Consigliere\Components\Support\Migrations; |
||
| 4 | |||
| 5 | use Illuminate\Contracts\Support\Arrayable; |
||
| 6 | |||
| 7 | class SchemaParser implements Arrayable |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * The array of custom attributes. |
||
| 11 | * |
||
| 12 | * @var array |
||
| 13 | */ |
||
| 14 | protected $customAttributes |
||
| 15 | = [ |
||
| 16 | 'remember_token' => 'rememberToken()', |
||
| 17 | 'soft_delete' => 'softDeletes()', |
||
| 18 | ]; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The migration schema. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $schema; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The relationship keys. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $relationshipKeys = ['belongsTo']; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create new instance. |
||
| 36 | * |
||
| 37 | * @param string|null $schema |
||
| 38 | */ |
||
| 39 | public function __construct($schema = null) |
||
| 40 | { |
||
| 41 | $this->schema = $schema; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Parse a string to array of formatted schema. |
||
| 46 | * |
||
| 47 | * @param string $schema |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | public function parse($schema) |
||
| 52 | { |
||
| 53 | $this->schema = $schema; |
||
| 54 | |||
| 55 | $parsed = []; |
||
| 56 | |||
| 57 | foreach ($this->getSchemas() as $schemaArray) { |
||
| 58 | $column = $this->getColumn($schemaArray); |
||
| 59 | |||
| 60 | $attributes = $this->getAttributes($column, $schemaArray); |
||
| 61 | |||
| 62 | $parsed[$column] = $attributes; |
||
| 63 | } |
||
| 64 | |||
| 65 | return $parsed; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Get array of schema. |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public function getSchemas() |
||
| 74 | { |
||
| 75 | if (is_null($this->schema)) { |
||
| 76 | return []; |
||
| 77 | } |
||
| 78 | |||
| 79 | return explode(',', str_replace(' ', '', $this->schema)); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Convert string migration to array. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | public function toArray() |
||
| 88 | { |
||
| 89 | return $this->parse($this->schema); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Render the migration to formatted script. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function render() |
||
| 98 | { |
||
| 99 | $results = ''; |
||
| 100 | |||
| 101 | foreach ($this->toArray() as $column => $attributes) { |
||
| 102 | $results .= $this->createField($column, $attributes); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $results; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Render up migration fields. |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function up() |
||
| 114 | { |
||
| 115 | return $this->render(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Render down migration fields. |
||
| 120 | * |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function down() |
||
| 124 | { |
||
| 125 | $results = ''; |
||
| 126 | |||
| 127 | foreach ($this->toArray() as $column => $attributes) { |
||
| 128 | $attributes = [head($attributes)]; |
||
| 129 | $results .= $this->createField($column, $attributes, 'remove'); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $results; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Create field. |
||
| 137 | * |
||
| 138 | * @param string $column |
||
| 139 | * @param array $attributes |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function createField($column, $attributes, $type = 'add') |
||
| 144 | { |
||
| 145 | $results = "\t\t\t" . '$table'; |
||
| 146 | |||
| 147 | foreach ($attributes as $key => $field) { |
||
| 148 | if (in_array($column, $this->relationshipKeys)) { |
||
| 149 | $results .= $this->addRelationColumn($key, $field, $column); |
||
| 150 | } else { |
||
| 151 | $results .= $this->{"{$type}Column"}($key, $field, $column); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | return $results .= ';' . PHP_EOL; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Add relation column. |
||
| 160 | * |
||
| 161 | * @param int $key |
||
| 162 | * @param string $field |
||
| 163 | * @param string $column |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | protected function addRelationColumn($key, $field, $column) |
||
|
0 ignored issues
–
show
|
|||
| 168 | { |
||
| 169 | $relatedColumn = snake_case(class_basename($field)) . '_id'; |
||
| 170 | |||
| 171 | $method = 'integer'; |
||
| 172 | |||
| 173 | return "->{$method}('{$relatedColumn}')"; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Format field to script. |
||
| 178 | * |
||
| 179 | * @param int $key |
||
| 180 | * @param string $field |
||
| 181 | * @param string $column |
||
| 182 | * |
||
| 183 | * @return string |
||
| 184 | */ |
||
| 185 | protected function addColumn($key, $field, $column) |
||
| 186 | { |
||
| 187 | if ($this->hasCustomAttribute($column)) { |
||
| 188 | return '->' . $field; |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($key == 0) { |
||
| 192 | return '->' . $field . "('" . $column . "')"; |
||
| 193 | } |
||
| 194 | |||
| 195 | if (str_contains($field, '(')) { |
||
| 196 | return '->' . $field; |
||
| 197 | } |
||
| 198 | |||
| 199 | return '->' . $field . '()'; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Format field to script. |
||
| 204 | * |
||
| 205 | * @param int $key |
||
| 206 | * @param string $field |
||
| 207 | * @param string $column |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function removeColumn($key, $field, $column) |
||
|
0 ignored issues
–
show
|
|||
| 212 | { |
||
| 213 | if ($this->hasCustomAttribute($column)) { |
||
| 214 | return '->' . $field; |
||
| 215 | } |
||
| 216 | |||
| 217 | return '->dropColumn(' . "'" . $column . "')"; |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get column name from schema. |
||
| 222 | * |
||
| 223 | * @param string $schema |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getColumn($schema) |
||
| 228 | { |
||
| 229 | return array_get(explode(':', $schema), 0); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get column attributes. |
||
| 234 | * |
||
| 235 | * @param string $column |
||
| 236 | * @param string $schema |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function getAttributes($column, $schema) |
||
| 241 | { |
||
| 242 | $fields = str_replace($column . ':', '', $schema); |
||
| 243 | |||
| 244 | return $this->hasCustomAttribute($column) ? $this->getCustomAttribute($column) : explode(':', $fields); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Determine whether the given column is exist in customAttributes array. |
||
| 249 | * |
||
| 250 | * @param string $column |
||
| 251 | * |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | public function hasCustomAttribute($column) |
||
| 255 | { |
||
| 256 | return array_key_exists($column, $this->customAttributes); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Get custom attributes value. |
||
| 261 | * |
||
| 262 | * @param string $column |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getCustomAttribute($column) |
||
| 267 | { |
||
| 268 | return (array)$this->customAttributes[$column]; |
||
| 269 | } |
||
| 270 | } |
||
| 271 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.