Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- codestyle fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Dec 26, 2019
1 parent d937a94 commit cdfbc09
Show file tree
Hide file tree
Showing 21 changed files with 250 additions and 182 deletions.
14 changes: 0 additions & 14 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/Atomizer/Atomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function sortedTables($reverse = false): array
*/
private function declareBlock(Source $source): void
{
if (!empty($source->getLines())) {
if ($source->getLines() !== []) {
$source->addLine('');
}
}
Expand Down
177 changes: 128 additions & 49 deletions src/Atomizer/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ final class Renderer implements RendererInterface
*/
public function createTable(Source $source, AbstractTable $table): void
{
$this->render($source, '$this->table(%s)', $table);
$this->render(
$source,
'$this->table(%s)',
$table
);
$comparator = $table->getComparator();

$this->declareColumns($source, $comparator);
$this->declareIndexes($source, $comparator);
$this->declareForeignKeys($source, $comparator, $table->getPrefix());

if (count($table->getPrimaryKeys())) {
$this->render($source, ' ->setPrimaryKeys(%s)', $table->getPrimaryKeys());
$this->render(
$source,
' ->setPrimaryKeys(%s)',
$table->getPrimaryKeys()
);
}

//Finalization
Expand All @@ -55,11 +63,19 @@ public function createTable(Source $source, AbstractTable $table): void
*/
public function updateTable(Source $source, AbstractTable $table): void
{
$this->render($source, '$this->table(%s)', $table);
$this->render(
$source,
'$this->table(%s)',
$table
);
$comparator = $table->getComparator();

if ($comparator->isPrimaryChanged()) {
$this->render($source, ' ->setPrimaryKeys(%s)', $table->getPrimaryKeys());
$this->render(
$source,
' ->setPrimaryKeys(%s)',
$table->getPrimaryKeys()
);
}

$this->declareColumns($source, $comparator);
Expand All @@ -76,10 +92,14 @@ public function updateTable(Source $source, AbstractTable $table): void
public function revertTable(Source $source, AbstractTable $table): void
{
//Get table blueprint
$this->render($source, '$this->table(%s)', $table);
$this->render(
$source,
'$this->table(%s)',
$table
);
$comparator = $table->getComparator();

$this->revertForeigns($source, $comparator, $table->getPrefix());
$this->revertForeignKeys($source, $comparator, $table->getPrefix());
$this->revertIndexes($source, $comparator);
$this->revertColumns($source, $comparator);

Expand All @@ -92,14 +112,18 @@ public function revertTable(Source $source, AbstractTable $table): void
*/
public function dropTable(Source $source, AbstractTable $table): void
{
$this->render($source, '$this->table(%s)->drop();', $table);
$this->render(
$source,
'$this->table(%s)->drop();',
$table
);
}

/**
* @param Source $source
* @param Comparator $comparator
*/
protected function declareColumns(Source $source, Comparator $comparator): void
private function declareColumns(Source $source, Comparator $comparator): void
{
foreach ($comparator->addedColumns() as $column) {
$this->render(
Expand All @@ -112,32 +136,54 @@ protected function declareColumns(Source $source, Comparator $comparator): void
}

foreach ($comparator->alteredColumns() as $pair) {
$this->alterColumn($source, $pair[self::NEW_STATE], $pair[self::ORIGINAL_STATE]);
$this->alterColumn(
$source,
$pair[self::NEW_STATE],
$pair[self::ORIGINAL_STATE]
);
}

foreach ($comparator->droppedColumns() as $column) {
$this->render($source, ' ->dropColumn(%s)', $column->getName());
$this->render(
$source,
' ->dropColumn(%s)',
$column->getName()
);
}
}

/**
* @param Source $source
* @param Comparator $comparator
*/
protected function declareIndexes(Source $source, Comparator $comparator): void
private function declareIndexes(Source $source, Comparator $comparator): void
{
foreach ($comparator->addedIndexes() as $index) {
$this->render($source, ' ->addIndex(%s, %s)', $index->getColumns(), $index);
$this->render(
$source,
' ->addIndex(%s, %s)',
$index->getColumns(),
$index
);
}

foreach ($comparator->alteredIndexes() as $pair) {
/** @var AbstractIndex $index */
$index = $pair[self::NEW_STATE];
$this->render($source, ' ->alterIndex(%s, %s)', $index->getColumns(), $index);
$this->render(
$source,
' ->alterIndex(%s, %s)',
$index->getColumns(),
$index
);
}

foreach ($comparator->droppedIndexes() as $index) {
$this->render($source, ' ->dropIndex(%s)', $index->getColumns());
$this->render(
$source,
' ->dropIndex(%s)',
$index->getColumns()
);
}
}

Expand All @@ -146,11 +192,8 @@ protected function declareIndexes(Source $source, Comparator $comparator): void
* @param Comparator $comparator
* @param string $prefix Database isolation prefix
*/
protected function declareForeignKeys(
Source $source,
Comparator $comparator,
string $prefix = ''
): void {
private function declareForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void
{
foreach ($comparator->addedForeignKeys() as $key) {
$this->render(
$source,
Expand All @@ -176,15 +219,19 @@ protected function declareForeignKeys(
}

foreach ($comparator->droppedForeignKeys() as $key) {
$this->render($source, ' ->dropForeignKey(%s)', $key->getColumns());
$this->render(
$source,
' ->dropForeignKey(%s)',
$key->getColumns()
);
}
}

/**
* @param Source $source
* @param Comparator $comparator
*/
protected function revertColumns(Source $source, Comparator $comparator): void
private function revertColumns(Source $source, Comparator $comparator): void
{
foreach ($comparator->droppedColumns() as $column) {
$this->render(
Expand All @@ -197,32 +244,54 @@ protected function revertColumns(Source $source, Comparator $comparator): void
}

foreach ($comparator->alteredColumns() as $pair) {
$this->alterColumn($source, $pair[self::ORIGINAL_STATE], $pair[self::NEW_STATE]);
$this->alterColumn(
$source,
$pair[self::ORIGINAL_STATE],
$pair[self::NEW_STATE]
);
}

foreach ($comparator->addedColumns() as $column) {
$this->render($source, ' ->dropColumn(%s)', $column->getName());
$this->render(
$source,
' ->dropColumn(%s)',
$column->getName()
);
}
}

/**
* @param Source $source
* @param Comparator $comparator
*/
protected function revertIndexes(Source $source, Comparator $comparator): void
private function revertIndexes(Source $source, Comparator $comparator): void
{
foreach ($comparator->droppedIndexes() as $index) {
$this->render($source, ' ->addIndex(%s, %s)', $index->getColumns(), $index);
$this->render(
$source,
' ->addIndex(%s, %s)',
$index->getColumns(),
$index
);
}

foreach ($comparator->alteredIndexes() as $pair) {
/** @var AbstractIndex $index */
$index = $pair[self::ORIGINAL_STATE];
$this->render($source, ' ->alterIndex(%s, %s)', $index->getColumns(), $index);
$this->render(
$source,
' ->alterIndex(%s, %s)',
$index->getColumns(),
$index
);
}

foreach ($comparator->addedIndexes() as $index) {
$this->render($source, ' ->dropIndex(%s)', $index->getColumns());
$this->render(
$source,
' ->dropIndex(%s)',
$index->getColumns()
);
}
}

Expand All @@ -231,11 +300,8 @@ protected function revertIndexes(Source $source, Comparator $comparator): void
* @param Comparator $comparator
* @param string $prefix Database isolation prefix.
*/
protected function revertForeigns(
Source $source,
Comparator $comparator,
string $prefix = ''
): void {
private function revertForeignKeys(Source $source, Comparator $comparator, string $prefix = ''): void
{
foreach ($comparator->droppedForeignKeys() as $key) {
$this->render(
$source,
Expand Down Expand Up @@ -275,7 +341,7 @@ protected function alterColumn(
AbstractColumn $column,
AbstractColumn $original
): void {
if ($column->getName() != $original->getName()) {
if ($column->getName() !== $original->getName()) {
$name = $original->getName();
} else {
$name = $column->getName();
Expand All @@ -289,8 +355,13 @@ protected function alterColumn(
$column
);

if ($column->getName() != $original->getName()) {
$this->render($source, ' ->renameColumn(%s, %s)', $name, $column->getName());
if ($column->getName() !== $original->getName()) {
$this->render(
$source,
' ->renameColumn(%s, %s)',
$name,
$column->getName()
);
}
}

Expand Down Expand Up @@ -331,7 +402,7 @@ protected function render(Source $source, string $format, ...$values): void

// numeric array
if (is_array($value) && count($value) > 0 && is_numeric(array_keys($value)[0])) {
$rendered[] = '["' . join('", "', $value) . '"]';
$rendered[] = '["' . implode('", "', $value) . '"]';
continue;
}

Expand All @@ -356,15 +427,15 @@ private function columnOptions(Serializer $serializer, AbstractColumn $column):
'default' => $column->getDefaultValue()
];

if ($column->getAbstractType() == 'enum') {
if ($column->getAbstractType() === 'enum') {
$options['values'] = $column->getEnumValues();
}

if ($column->getAbstractType() == 'string') {
if ($column->getAbstractType() === 'string') {
$options['size'] = $column->getSize();
}

if ($column->getAbstractType() == 'decimal') {
if ($column->getAbstractType() === 'decimal') {
$options['scale'] = $column->getScale();
$options['precision'] = $column->getPrecision();
}
Expand All @@ -379,10 +450,14 @@ private function columnOptions(Serializer $serializer, AbstractColumn $column):
*/
private function indexOptions(Serializer $serializer, AbstractIndex $index): string
{
return $this->mountIndents($serializer->serialize([
'name' => $index->getName(),
'unique' => $index->isUnique()
]));
return $this->mountIndents(
$serializer->serialize(
[
'name' => $index->getName(),
'unique' => $index->isUnique()
]
)
);
}

/**
Expand All @@ -394,11 +469,15 @@ private function foreignKeyOptions(
Serializer $serializer,
AbstractForeignKey $reference
): string {
return $this->mountIndents($serializer->serialize([
'name' => $reference->getName(),
'delete' => $reference->getDeleteRule(),
'update' => $reference->getUpdateRule()
]));
return $this->mountIndents(
$serializer->serialize(
[
'name' => $reference->getName(),
'delete' => $reference->getDeleteRule(),
'update' => $reference->getUpdateRule()
]
)
);
}

/**
Expand All @@ -415,6 +494,6 @@ private function mountIndents(string $serialized): string
unset($line);
}

return ltrim(join("\n", $lines));
return ltrim(implode("\n", $lines));
}
}
10 changes: 6 additions & 4 deletions src/Capsule.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public function execute(array $operations): void
{
foreach ($operations as $operation) {
if (!$operation instanceof OperationInterface) {
throw new CapsuleException(sprintf(
'Migration operation expected to be an instance of `OperationInterface`, `%s` given',
get_class($operation)
));
throw new CapsuleException(
sprintf(
'Migration operation expected to be an instance of `OperationInterface`, `%s` given',
get_class($operation)
)
);
}

$operation->execute($this);
Expand Down
Loading

0 comments on commit cdfbc09

Please sign in to comment.