Passed
Push — master ( f71b04...7dcb0c )
by P.R.
12:30
created

InsertRoutine::generateBody()   B

Complexity

Conditions 8
Paths 32

Size

Total Lines 58
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 32
nop 0
dl 0
loc 58
rs 8.1954
c 0
b 0
f 0

How to fix   Long Method   

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
namespace SetBased\Stratum\MySql\Crud\Helper;
5
6
/**
7
 * Generates the code for a stored routine that inserts a row.
8
 */
9
class InsertRoutine extends BaseRoutine
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  /**
13
   * @inheritdoc
14
   */
15
  protected function generateBody(): void
16
  {
17
    $this->codeStore->append(sprintf('insert into %s(', $this->tableName));
18
19
    $offset  = mb_strlen($this->codeStore->getLastLine());
20
    $columns = $this->tableColumnsWithoutAutoIncrement();
21
    $padding = $this->maxColumnNameLength($columns);
22
23
    $first = true;
24
    foreach ($columns as $column)
25
    {
26
      if ($first)
27
      {
28
        $this->codeStore->appendToLastLine(sprintf(' %s', $column['column_name']));
29
30
        $first = false;
31
      }
32
      else
33
      {
34
        $format = sprintf('%%-%ds %%s', $offset);
35
        $this->codeStore->append(sprintf($format, ',', $column['column_name']));
36
37
        if ($column===end($this->tableColumns))
38
        {
39
          $this->codeStore->appendToLastLine(' )');
40
        }
41
      }
42
    }
43
44
    $this->codeStore->append('values(');
45
    $offset = mb_strlen($this->codeStore->getLastLine());
46
47
    $first = true;
48
    foreach ($columns as $column)
49
    {
50
      if ($first)
51
      {
52
        $this->codeStore->appendToLastLine(sprintf(' p_%s', $column['column_name']));
53
54
        $first = false;
55
      }
56
      else
57
      {
58
        $format = sprintf('%%-%ds p_%%-%ds', $offset, $padding);
59
        $this->codeStore->append(sprintf($format, ',', $column['column_name']));
60
61
        if ($column===end($this->tableColumns))
62
        {
63
          $this->codeStore->appendToLastLine(' )');
64
        }
65
      }
66
    }
67
    $this->codeStore->append(';');
68
69
    if ($this->checkAutoIncrement($this->tableColumns))
70
    {
71
      $this->codeStore->append('');
72
      $this->codeStore->append('select last_insert_id();');
73
    }
74
  }
75
76
  //--------------------------------------------------------------------------------------------------------------------
77
  /**
78
   * @inheritDoc
79
   */
80
  protected function generateDocBlock(): void
81
  {
82
    $this->generateDocBlockAllColumnsWithoutAutoIncrement();
83
  }
84
85
  //--------------------------------------------------------------------------------------------------------------------
86
  /**
87
   * Generates the function name and parameters of the stored routine.
88
   */
89
  protected function generateRoutineDeclaration(): void
90
  {
91
    $this->generateRoutineDeclarationAllColumnsWithoutAutoIncrement();
92
  }
93
94
  //--------------------------------------------------------------------------------------------------------------------
95
  /**
96
   * @inheritDoc
97
   */
98
  protected function generateSqlDataAndDesignationType(): void
99
  {
100
    $this->codeStore->append('modifies sql data');
101
102
    if ($this->checkAutoIncrement($this->tableColumns))
103
    {
104
      $this->codeStore->append('-- type:   singleton1');
105
      $this->codeStore->append('-- return: int');
106
    }
107
    else
108
    {
109
      $this->codeStore->append('-- type: none');
110
    }
111
  }
112
113
  //--------------------------------------------------------------------------------------------------------------------
114
}
115
116
//--------------------------------------------------------------------------------------------------------------------
117