Conditions | 9 |
Paths | 56 |
Total Lines | 62 |
Code Lines | 30 |
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 |
||
141 | protected function doFileSnapshotAssertion(string $filePath) |
||
142 | { |
||
143 | if (! file_exists($filePath)) { |
||
144 | $this->fail('File does not exist'); |
||
145 | } |
||
146 | |||
147 | $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
||
148 | |||
149 | if (empty($fileExtension)) { |
||
150 | $this->fail("Unable to make a file snapshot, file does not have a file extension ({$filePath})"); |
||
151 | } |
||
152 | |||
153 | $fileSystem = Filesystem::inDirectory($this->getFileSnapshotDirectory()); |
||
154 | |||
155 | $this->snapshotIncrementor++; |
||
156 | |||
157 | $snapshotId = $this->getSnapshotId().'.'.$fileExtension; |
||
158 | |||
159 | // If $filePath has a different file extension than the snapshot, the test should fail |
||
160 | if ($namesWithDifferentExtension = $fileSystem->getNamesWithDifferentExtension($snapshotId)) { |
||
161 | // There is always only one existing snapshot with a different extension |
||
162 | $existingSnapshotId = $namesWithDifferentExtension[0]; |
||
163 | |||
164 | if ($this->shouldUpdateSnapshots()) { |
||
165 | $fileSystem->delete($existingSnapshotId); |
||
166 | |||
167 | $fileSystem->copy($filePath, $snapshotId); |
||
168 | |||
169 | return $this->markTestIncomplete("File snapshot updated for {$snapshotId}"); |
||
170 | } |
||
171 | |||
172 | $expectedExtension = pathinfo($existingSnapshotId, PATHINFO_EXTENSION); |
||
173 | |||
174 | return $this->fail("File did not match the snapshot file extension (expected: {$expectedExtension}, was: {$fileExtension})"); |
||
175 | } |
||
176 | |||
177 | $failedSnapshotId = $snapshotId.'_failed.'.$fileExtension; |
||
178 | |||
179 | if ($fileSystem->has($failedSnapshotId)) { |
||
180 | $fileSystem->delete($failedSnapshotId); |
||
181 | } |
||
182 | |||
183 | if (! $fileSystem->has($snapshotId)) { |
||
184 | $fileSystem->copy($filePath, $snapshotId); |
||
185 | |||
186 | $this->markTestIncomplete("File snapshot created for {$snapshotId}"); |
||
187 | } |
||
188 | |||
189 | if (! $fileSystem->fileEquals($filePath, $snapshotId)) { |
||
190 | if ($this->shouldUpdateSnapshots()) { |
||
191 | $fileSystem->copy($filePath, $snapshotId); |
||
192 | |||
193 | $this->markTestIncomplete("File snapshot updated for {$snapshotId}"); |
||
194 | } |
||
195 | |||
196 | $fileSystem->copy($filePath, $failedSnapshotId); |
||
197 | |||
198 | $this->fail("File did not match snapshot ({$snapshotId})"); |
||
199 | } |
||
200 | |||
201 | $this->assertTrue(true); |
||
202 | } |
||
203 | |||
233 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.