Mercurial > packages > magicforger
diff src/Replacer/TableReplacer.php @ 21:f0b0d014e448 main-dev
Cleaning up code based on AI overlord review
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Wed, 26 Feb 2025 19:45:08 -0500 |
| parents | 19b7a8de0019 |
| children | 827efbf4d73c |
line wrap: on
line diff
--- a/src/Replacer/TableReplacer.php Wed Feb 26 19:29:55 2025 -0500 +++ b/src/Replacer/TableReplacer.php Wed Feb 26 19:45:08 2025 -0500 @@ -4,9 +4,14 @@ trait TableReplacer { - protected $columns; + protected ?array $columns = null; - protected function get_columns() + /** + * Retrieve columns for the current table. + * + * @return array + */ + protected function get_columns(): array { if (is_null($this->columns)) { $this->columns = $this->getCurrentTable()->getColumns(); @@ -15,67 +20,80 @@ return $this->columns; } - protected function get_attributes() - { - } - + /** + * Get a string representation of values for creation. + * + * @return string + */ protected function getValuesForCreation(): string { $insert = ''; foreach ($this->get_columns() as $column) { - $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; + $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column->getName(), $column->getName()) . "\n"; } return $insert; } + /** + * Get a string representation of table attributes. + * + * @return string + */ protected function getAttributes(): string { $insert = ''; foreach ($this->get_columns() as $column) { - $insert .= "'".$column->getName()."' => '',\n"; + $insert .= sprintf("'%s' => '',", $column->getName()) . "\n"; } return $insert; } + /** + * Get formatted validation rules for table columns. + * + * @return string + */ protected function getValuesForValidation(): string { $insert = ''; foreach ($this->get_columns() as $column) { - $insert .= "'".$column->getName()."' => 'nullable',\n"; + $insert .= sprintf("'%s' => 'nullable',", $column->getName()) . "\n"; } return $insert; } + /** + * Apply insertions in the target template. + * + * @param string $target + * @return string + */ public function apply_inserts(string $target): string { $inserts = $this->get_all_keywords($target); - $available_replacements = $this->get_available_inserts(); + $available_insertions = $this->get_available_inserts(); - $target = str_replace( - array_keys($available_replacements), - $available_replacements, + return str_replace( + array_keys($available_insertions), + $available_insertions, $target ); - - return $target; } - public function get_available_inserts() + /** + * Get available insertion points for the template. + * + * @return array + */ + public function get_available_inserts(): array { - $table_name = $this->getTableInput(); - $replacements = [ - '// {{ valuesForCreation }}' => self::getValuesForCreation(), - '# {{ atributeInsertPoint }}' => self::getAttributes(), - '// {{ valuesForValidation }}' => self::getValuesForValidation(), - ]; - - foreach ($replacements as $key => &$replacement) { - $replacement = $replacement."\n".$key; - } - - return $replacements; + return [ + '// {{ valuesForCreation }}' => $this->getValuesForCreation(), + '# {{ attributeInsertPoint }}' => $this->getAttributes(), + '// {{ valuesForValidation }}' => $this->getValuesForValidation(), + ]; } }
