Conditions | 10 |
Paths | 25 |
Total Lines | 50 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
98 | protected function save(): void |
||
99 | { |
||
100 | $params = $this->getParamDefinitions(); |
||
101 | $moduleName = $this->getModuleName(); |
||
102 | //TODO get all configuration in order to know if need insert/update |
||
103 | $this->dbConfig->get(sprintf('%s.', $moduleName)); |
||
104 | |||
105 | foreach ($params as $name => $arr) { |
||
106 | $postKey = $arr['key'] ?? $name; |
||
107 | $value = $this->param->post($postKey); |
||
108 | // if is array and empty |
||
109 | if ($arr['type'] === 'array' && empty($value)) { |
||
110 | $value = []; |
||
111 | } elseif ($arr['type'] === 'callable' && is_callable($arr['callable'])) { |
||
112 | $value = call_user_func_array($arr['callable'], [$this->dbConfig]); |
||
113 | if ($value !== null) { |
||
114 | $arr['type'] = gettype($value); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | if (is_array($value)) { |
||
119 | $value = serialize($value); |
||
120 | } |
||
121 | $key = sprintf('%s.%s', $moduleName, $name); |
||
122 | if ($this->dbConfig->has($key)) { |
||
123 | $entity = $this->dbConfig->getLoader()->loadConfig([ |
||
124 | 'module' => $moduleName, |
||
125 | 'name' => $name, |
||
126 | ]); |
||
127 | |||
128 | if ($entity !== null) { |
||
129 | $entity->name = $name; |
||
130 | $entity->env = null; |
||
131 | $entity->module = $moduleName; |
||
132 | $entity->value = $value; |
||
133 | $entity->status = YesNoStatus::YES; |
||
134 | $entity->type = $arr['type']; |
||
135 | $entity->comment = $arr['comment']; |
||
136 | |||
137 | $this->dbConfig->getLoader()->updateConfig($entity); |
||
138 | } |
||
139 | } else { |
||
140 | $this->dbConfig->getLoader()->insertConfig([ |
||
141 | 'name' => $name, |
||
142 | 'env' => null, |
||
143 | 'module' => $moduleName, |
||
144 | 'value' => $value, |
||
145 | 'status' => YesNoStatus::YES, |
||
146 | 'type' => $arr['type'], |
||
147 | 'comment' => $arr['comment'], |
||
148 | ]); |
||
195 |