Conditions | 11 |
Paths | 1 |
Total Lines | 127 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
59 | protected function generateFileContents(DmsDatabaseInterface $dmsDatabase, DmsTableInterface $dmsTable): string |
||
60 | { |
||
61 | $gettersAndSetters = $dmsTable->getDmsColumns(); |
||
62 | $gettersAndSetters = \implode("\n\n", \array_map( |
||
63 | function (DmsColumn $column) use ($dmsTable) { |
||
64 | /** phpcs:disable */ |
||
65 | return /** @lang PHP */ |
||
66 | <<<PHP |
||
67 | |||
68 | /** |
||
69 | * @return {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? 'null|' : '')}{$column->getPhpType()} |
||
70 | */ |
||
71 | public function get{$column->getPhpName()}(): {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? '?' : '')}{$column->getPhpType()} |
||
72 | { |
||
73 | return \$this->data['{$column->getName()}']; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? 'null|' : '')}{$column->getPhpType()} \${$this->heredoc(\lcfirst($column->getPhpName()))} |
||
78 | * |
||
79 | * @return \$this |
||
80 | */ |
||
81 | public function set{$column->getPhpName()}({$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? '?' : '')}{$column->getPhpType()} \${$this->heredoc(\lcfirst($column->getPhpName()))}): {$this->generateClassName($dmsTable)} |
||
82 | { |
||
83 | \$this->data['{$column->getName()}'] = \${$this->heredoc(\lcfirst($column->getPhpName()))}; |
||
84 | |||
85 | return \$this; |
||
86 | } |
||
87 | PHP; |
||
88 | /** phpcs:enable */ |
||
89 | }, |
||
90 | $gettersAndSetters |
||
91 | )); |
||
92 | |||
93 | $columnsConstants = $dmsTable->getDmsColumns(); |
||
94 | $columnsConstants = \implode( |
||
95 | "\n ", |
||
96 | \array_map( |
||
97 | function (DmsColumn $column) { |
||
98 | return \sprintf( |
||
99 | 'const COLUMN_%s = \'%s\';', |
||
100 | \mb_strtoupper($column->getName()), |
||
101 | $column->getName() |
||
102 | ); |
||
103 | }, |
||
104 | $columnsConstants |
||
105 | ) |
||
106 | ); |
||
107 | |||
108 | $primaryKeys = $dmsTable->getDmsColumns(); |
||
109 | $primaryKeys = \implode( |
||
110 | "\n ", |
||
111 | \array_filter(\array_map( |
||
112 | function (DmsColumn $column) { |
||
113 | if ($column->getKey() !== 'PRI') { |
||
114 | return null; |
||
115 | } |
||
116 | |||
117 | return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
||
118 | }, |
||
119 | $primaryKeys |
||
120 | )) |
||
121 | ); |
||
122 | |||
123 | $primaryKeysAutoIncrement = $dmsTable->getDmsColumns(); |
||
124 | $primaryKeysAutoIncrement = \implode( |
||
125 | "\n ", |
||
126 | \array_filter(\array_map( |
||
127 | function (DmsColumn $column) { |
||
128 | if ($column->getExtra() !== 'auto_increment') { |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
||
133 | }, |
||
134 | $primaryKeysAutoIncrement |
||
135 | )) |
||
136 | ); |
||
137 | |||
138 | $columns = $dmsTable->getDmsColumns(); |
||
139 | $columns = \implode( |
||
140 | "\n ", |
||
141 | \array_map( |
||
142 | function (DmsColumn $column) { |
||
143 | return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
||
144 | }, |
||
145 | $columns |
||
146 | ) |
||
147 | ); |
||
148 | |||
149 | /** phpcs:disable */ |
||
150 | return /** @lang PHP */ |
||
151 | <<<PHP |
||
152 | <?php declare(strict_types=1); |
||
153 | |||
154 | namespace {$this->generateNamespace($dmsDatabase)}; |
||
155 | |||
156 | use Janisbiz\LightOrm\Entity\BaseEntity; |
||
157 | |||
158 | class {$this->generateClassName($dmsTable)} extends BaseEntity |
||
159 | { |
||
160 | const {$this->heredoc(WriterInterface::CLASS_CONSTANT_DATABASE_NAME)} = '{$dmsDatabase->getName()}'; |
||
161 | const {$this->heredoc(WriterInterface::CLASS_CONSTANT_TABLE_NAME)} = '{$dmsDatabase->getName()}.{$dmsTable->getName()}'; |
||
162 | |||
163 | {$columnsConstants} |
||
164 | |||
165 | /** |
||
166 | * @param bool \$isNew |
||
167 | */ |
||
168 | public function __construct(\$isNew = true) |
||
169 | { |
||
170 | \$this->primaryKeys = [ |
||
171 | {$primaryKeys} |
||
172 | ]; |
||
173 | \$this->primaryKeysAutoIncrement = [ |
||
174 | {$primaryKeysAutoIncrement} |
||
175 | ]; |
||
176 | \$this->columns = [ |
||
177 | {$columns} |
||
178 | ]; |
||
179 | |||
180 | \$this->isNew = \$isNew; |
||
181 | if (empty(\$this->data)) { |
||
182 | \$this->isNew = true; |
||
183 | } |
||
184 | } |
||
185 | {$gettersAndSetters} |
||
186 | } |
||
192 |