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 |
||
39 | abstract class AbstractPostgreSQLDriver implements Driver, ExceptionConverterDriver, VersionAwarePlatformDriver |
||
40 | { |
||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * |
||
44 | * @link http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html |
||
45 | */ |
||
46 | 2 | public function convertException($message, DriverException $exception) |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 4 | public function createDatabasePlatformForVersion($version) |
|
102 | { |
||
103 | 4 | if ( ! preg_match('/^(?P<major>\d+)(?:\.(?P<minor>\d+)(?:\.(?P<patch>\d+))?)?/', $version, $versionParts)) { |
|
104 | 2 | throw DBALException::invalidPlatformVersionSpecified( |
|
105 | 2 | $version, |
|
106 | 2 | '<major_version>.<minor_version>.<patch_version>' |
|
107 | ); |
||
108 | } |
||
109 | |||
110 | 2 | $majorVersion = $versionParts['major']; |
|
111 | 2 | $minorVersion = $versionParts['minor'] ?? 0; |
|
112 | 2 | $patchVersion = $versionParts['patch'] ?? 0; |
|
113 | 2 | $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; |
|
114 | |||
115 | switch(true) { |
||
116 | 2 | case version_compare($version, '9.4', '>='): |
|
|
|||
117 | 2 | return new PostgreSQL94Platform(); |
|
118 | 2 | case version_compare($version, '9.2', '>='): |
|
119 | 2 | return new PostgreSQL92Platform(); |
|
120 | 2 | case version_compare($version, '9.1', '>='): |
|
121 | 2 | return new PostgreSQL91Platform(); |
|
122 | default: |
||
123 | 2 | return new PostgreSqlPlatform(); |
|
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 2 | View Code Duplication | public function getDatabase(\Doctrine\DBAL\Connection $conn) |
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 2 | public function getDatabasePlatform() |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 2 | public function getSchemaManager(\Doctrine\DBAL\Connection $conn) |
|
154 | } |
||
155 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.