Passed
Push — master ( ad038c...02f8e8 )
by Anton
01:54
created

RenderTable::renderColumn()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 19
nc 12
nop 4
dl 0
loc 41
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\Schema\Processor;
11
12
use Cycle\Schema\Definition\Entity;
13
use Cycle\Schema\Processor\Table\ColumnSchema;
14
use Cycle\Schema\ProcessorInterface;
15
use Cycle\Schema\Registry;
16
17
/**
18
 * Generate table columns based on entity definition.
19
 */
20
class RenderTable implements ProcessorInterface
21
{
22
    /**
23
     * Generate table schema based on given entity definition.
24
     *
25
     * @param Registry $registry
26
     * @param Entity   $entity
27
     */
28
    public function compute(Registry $registry, Entity $entity)
29
    {
30
        if (!$registry->hasTable($entity)) {
31
            // do not render entities without associated table
32
            return;
33
        }
34
35
        $table = $registry->getTableSchema($entity);
36
37
        $primaryKeys = [];
38
        foreach ($entity->getFields() as $field) {
39
            $column = ColumnSchema::parse($field);
40
41
            if ($column->isPrimary()) {
42
                $primaryKeys[] = $field->getColumn();
43
            }
44
45
            $column->render($table->column($field->getColumn()));
46
        }
47
48
        if (count($primaryKeys)) {
49
            $table->setPrimaryKeys($primaryKeys);
50
        }
51
    }
52
}