Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class FakeManagerTestSessionExtension extends Extension { |
||
8 | |||
9 | public function updateBaseFields($fields) { |
||
13 | |||
14 | public function fakeDatabasePath() { |
||
17 | |||
18 | public function templatePathRelative() { |
||
21 | |||
22 | public function updateStartForm($form) { |
||
48 | |||
49 | /** |
||
50 | * This needs to handle two distinct cases: |
||
51 | * - Test Session being created by behat (calling TestSessionEnvironment directly), and |
||
52 | * - Test Session being created by browsing to dev/testsession and submitting the form. |
||
53 | * |
||
54 | * The form is modified above (@see self::updateStartForm()) and we need to ensure we respect those selections, if |
||
55 | * necessary. If it wasn't submitted via a form, then we can set the fakes up as required for behat. |
||
56 | * |
||
57 | * @param $state Array of state passed from TestSessionEnvironment |
||
58 | */ |
||
59 | public function onBeforeStartTestSession(&$state) { |
||
90 | |||
91 | /** |
||
92 | * Only used for manual testing, not on Behat runs. |
||
93 | */ |
||
94 | public function onBeforeClear() { |
||
102 | |||
103 | /** |
||
104 | * Only used for manual testing, not on Behat runs. |
||
105 | */ |
||
106 | public function onBeforeEndTestSession() { |
||
113 | |||
114 | /** |
||
115 | * A similar reset is also performed in App\Tests\Behaviour\FeatureContext->resetFakeDatabase(). |
||
116 | * We can't reset Behat CLI runs through this measure because the CLI has a persistent connection |
||
117 | * to the underlying SQLite database file, so the browser can't remove it. |
||
118 | */ |
||
119 | View Code Duplication | protected function resetFakeManager() { |
|
131 | |||
132 | } |
||
133 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.