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 |
||
| 17 | class DoctrineDatabase extends Gateway |
||
| 18 | { |
||
| 19 | const TABLE_USER_PREFERENCES = 'ezpreferences'; |
||
| 20 | |||
| 21 | const COLUMN_ID = 'id'; |
||
| 22 | const COLUMN_NAME = 'name'; |
||
| 23 | const COLUMN_USER_ID = 'user_id'; |
||
| 24 | const COLUMN_VALUE = 'value'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var \Doctrine\DBAL\Connection |
||
| 28 | */ |
||
| 29 | protected $connection; |
||
| 30 | |||
| 31 | public function __construct(Connection $connection) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function setUserPreference(UserPreferenceSetStruct $userPreference): int |
||
| 76 | |||
| 77 | View Code Duplication | public function getUserPreferenceByUserIdAndName(int $userId, string $name): array |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | View Code Duplication | public function loadUserPreferences(int $userId, int $offset = 0, int $limit = -1): array |
|
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function countUserPreferences(int $userId): int |
|
| 118 | { |
||
| 119 | $query = $this->connection->createQueryBuilder(); |
||
| 120 | $query |
||
| 121 | ->select( |
||
| 122 | $this->connection->getDatabasePlatform()->getCountExpression(self::COLUMN_ID) |
||
| 123 | ) |
||
| 124 | ->from(self::TABLE_USER_PREFERENCES) |
||
| 125 | ->where($query->expr()->eq(self::COLUMN_USER_ID, ':user_id')) |
||
| 126 | ->setParameter(':user_id', $userId, ParameterType::INTEGER); |
||
| 127 | |||
| 128 | return (int) $query->execute()->fetchColumn(); |
||
| 129 | } |
||
| 130 | |||
| 131 | private function getColumns(): array |
||
| 140 | } |
||
| 141 |