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 |
||
| 14 | class EntityContext extends Context |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @Given /^the following ([\w ]+):?$/ |
||
| 18 | */ |
||
| 19 | public function theFollowing($name, TableNode $table) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @Given /^there (?:is|are) (\d+) ((?!\w* like)\w*)$/ |
||
| 54 | */ |
||
| 55 | public function thereIs($nbr, $name) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @Given /^there (?:is|are) (\d+) (.*) like:?$/ |
||
| 79 | */ |
||
| 80 | public function thereIsLikeFollowing($nbr, $name, TableNode $table) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @Given /^(\w+) (.+) should have been (created|deleted)$/ |
||
| 110 | */ |
||
| 111 | public function entitiesShouldHaveBeen($expected, $entity, $state) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @Then /^should be (\d+) (.*) like:?$/ |
||
| 155 | */ |
||
| 156 | public function existLikeFollowing($nbr, $name, TableNode $table) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @BeforeScenario |
||
| 190 | */ |
||
| 191 | public function beforeScenario($event) |
||
| 192 | { |
||
| 193 | $this->storeTags($event); |
||
| 194 | |||
| 195 | if ($this->hasTags([ 'reset-schema', '~not-reset-schema' ])) { |
||
| 196 | foreach ($this->getEntityManagers() as $name => $entityManager) { |
||
| 197 | $connection = $entityManager->getConnection(); |
||
| 198 | if ($connection instanceof PoolingShardConnection) { |
||
| 199 | $poolingShardManager = $this->getPoolingShardManager($connection); |
||
| 200 | foreach ($poolingShardManager->getShards() as $shardId) { |
||
| 201 | // Switch to shard database |
||
| 202 | $connection->connect($shardId['id']); |
||
| 203 | $this->resetSchema($entityManager); |
||
| 204 | } |
||
| 205 | // Back to global |
||
| 206 | $connection->connect(0); |
||
| 207 | } else { |
||
| 208 | $this->resetSchema($entityManager); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @AfterScenario |
||
| 216 | */ |
||
| 217 | public function afterScenario($event) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param EntityManager $entityManager |
||
| 226 | */ |
||
| 227 | protected function resetSchema(EntityManager $entityManager) |
||
| 236 | |||
| 237 | protected function compareArray(array $a1, array $a2) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Find PoolingShardManager related to connection. |
||
| 251 | * |
||
| 252 | * Cannot directly find PoolingShardManager by EntityManager's name cause |
||
| 253 | * PoolingShardManager is created from Connection's name. |
||
| 254 | * |
||
| 255 | * @param PoolingShardConnection $poolingShardConnection |
||
| 256 | * |
||
| 257 | * @return PoolingShardManager |
||
| 258 | * |
||
| 259 | * @throws \LogicException Unable to find PoolingShardManager related to this PoolingShardConnection. |
||
| 260 | */ |
||
| 261 | protected function getPoolingShardManager(PoolingShardConnection $poolingShardConnection) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param EntityManager $entityManager |
||
| 275 | * |
||
| 276 | * @return ClassMetadata[] |
||
| 277 | */ |
||
| 278 | protected function getMetadata(EntityManager $entityManager) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @return EntityManager[] |
||
| 285 | */ |
||
| 286 | protected function getEntityManagers() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return Connection[] |
||
| 293 | */ |
||
| 294 | protected function getConnections() |
||
| 298 | |||
| 299 | protected function getDefaultOptions() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $entityName |
||
| 308 | * @param array $headers Headers of the Behat TableNode |
||
| 309 | * @param array $row current row if tge Behat TableNode |
||
| 310 | * @return array ['id_column_A' => 'value', 'id_column_B' => 'value'] |
||
| 311 | * @throws \Exception |
||
| 312 | */ |
||
| 313 | protected function getEntityIdentifiers($entityName, $headers, $row) |
||
| 327 | } |
||
| 328 | |||
| 329 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.