Conditions | 12 |
Paths | 149 |
Total Lines | 76 |
Code Lines | 37 |
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 |
||
130 | public function next() |
||
131 | { |
||
132 | $row = $this->statement->fetch(\PDO::FETCH_LAZY); |
||
133 | if ($row) { |
||
134 | |||
135 | // array<tablegroup, array<table, array<column, value>>> |
||
|
|||
136 | $beansData = []; |
||
137 | foreach ($row as $key => $value) { |
||
138 | if (!isset($this->columnDescriptors[$key])) { |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | $columnDescriptor = $this->columnDescriptors[$key]; |
||
143 | |||
144 | if ($columnDescriptor['tableGroup'] === null) { |
||
145 | // A column can have no tableGroup (if it comes from an ORDER BY expression) |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | // Let's cast the value according to its type |
||
150 | $value = $columnDescriptor['type']->convertToPHPValue($value, $this->databasePlatform); |
||
151 | |||
152 | $beansData[$columnDescriptor['tableGroup']][$columnDescriptor['table']][$columnDescriptor['column']] = $value; |
||
153 | } |
||
154 | |||
155 | $reflectionClassCache = []; |
||
156 | $firstBean = true; |
||
157 | foreach ($beansData as $beanData) { |
||
158 | |||
159 | // Let's find the bean class name associated to the bean. |
||
160 | |||
161 | list($actualClassName, $mainBeanTableName, $tablesUsed) = $this->tdbmService->_getClassNameFromBeanData($beanData); |
||
162 | |||
163 | if ($this->className !== null) { |
||
164 | $actualClassName = $this->className; |
||
165 | } |
||
166 | |||
167 | // Let's filter out the beanData that is not used (because it belongs to a part of the hierarchy that is not fetched: |
||
168 | foreach ($beanData as $tableName => $descriptors) { |
||
169 | if (!in_array($tableName, $tablesUsed)) { |
||
170 | unset($beanData[$tableName]); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | // Must we create the bean? Let's see in the cache if we have a mapping DbRow? |
||
175 | // Let's get the first object mapping a row: |
||
176 | // We do this loop only for the first table |
||
177 | |||
178 | $primaryKeys = $this->tdbmService->_getPrimaryKeysFromObjectData($mainBeanTableName, $beanData[$mainBeanTableName]); |
||
179 | $hash = $this->tdbmService->getObjectHash($primaryKeys); |
||
180 | |||
181 | if ($this->objectStorage->has($mainBeanTableName, $hash)) { |
||
182 | $dbRow = $this->objectStorage->get($mainBeanTableName, $hash); |
||
183 | $bean = $dbRow->getTDBMObject(); |
||
184 | } else { |
||
185 | // Let's construct the bean |
||
186 | if (!isset($reflectionClassCache[$actualClassName])) { |
||
187 | $reflectionClassCache[$actualClassName] = new \ReflectionClass($actualClassName); |
||
188 | } |
||
189 | // Let's bypass the constructor when creating the bean! |
||
190 | $bean = $reflectionClassCache[$actualClassName]->newInstanceWithoutConstructor(); |
||
191 | $bean->_constructFromData($beanData, $this->tdbmService); |
||
192 | } |
||
193 | |||
194 | // The first bean is the one containing the main table. |
||
195 | if ($firstBean) { |
||
196 | $firstBean = false; |
||
197 | $this->current = $bean; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | ++$this->key; |
||
202 | } else { |
||
203 | $this->current = null; |
||
204 | } |
||
205 | } |
||
206 | |||
300 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.