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 |
||
| 23 | class Module extends \yii\base\Module implements BootstrapInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Default database component. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $defaultDbComponent = 'db'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default name for dump file. |
||
| 34 | * |
||
| 35 | * Aliases supported. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $defaultDumpFile = '@app/runtime/dump.sql'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Array of dumpers for different databases. |
||
| 43 | * |
||
| 44 | * Keys must be a valid DSN prefix. |
||
| 45 | * |
||
| 46 | * Values must be either a string which represents dumper class name |
||
| 47 | * or an array with 'class' field. |
||
| 48 | * Values are passed to Yii::createObject() method. |
||
| 49 | * |
||
| 50 | * @var mixed |
||
| 51 | */ |
||
| 52 | public $dumpers = [ |
||
| 53 | 'mysql' => [ |
||
| 54 | 'class' => 'herroffizier\yii2dumpling\dumpers\MysqlDumper', |
||
| 55 | ], |
||
| 56 | ]; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param mixed $app |
||
| 60 | * @codeCoverageIgnore |
||
| 61 | */ |
||
| 62 | public function bootstrap($app) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Parse DSN string into array. |
||
| 76 | * |
||
| 77 | * @param string $dsn |
||
| 78 | * |
||
| 79 | * @return string[] |
||
| 80 | */ |
||
| 81 | 15 | protected function parseDsn($dsn) |
|
| 82 | { |
||
| 83 | 15 | list($driverName, $dsn) = explode(':', $dsn, 2); |
|
| 84 | |||
| 85 | 15 | $values = ['driverName' => $driverName]; |
|
| 86 | 15 | $options = explode(';', $dsn); |
|
| 87 | 15 | foreach ($options as $option) { |
|
| 88 | // Some options in DSN may be a single string. |
||
| 89 | 15 | if (mb_strpos($option, '=') !== false) { |
|
| 90 | 12 | list($key, $value) = explode('=', $option, 2); |
|
| 91 | 12 | $values[$key] = $value; |
|
| 92 | 8 | } else { |
|
| 93 | 7 | $values[] = $option; |
|
| 94 | } |
||
| 95 | 10 | } |
|
| 96 | |||
| 97 | 15 | return $values; |
|
| 98 | } |
||
| 99 | |||
| 100 | 15 | protected function createDumper(Connection $dbComponent) |
|
| 119 | |||
| 120 | 15 | protected function getDbComponent($db) |
|
| 134 | |||
| 135 | 15 | protected function getDumpFilePath($file) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Dump database to file. |
||
| 146 | * |
||
| 147 | * If file or db params are not specified, default values will be used. |
||
| 148 | * |
||
| 149 | * @param string $file |
||
|
|
|||
| 150 | * @param string $db |
||
| 151 | */ |
||
| 152 | 9 | View Code Duplication | public function dump($file = null, $db = null) |
| 161 | |||
| 162 | /** |
||
| 163 | * Restore database from file. |
||
| 164 | * |
||
| 165 | * If file or db params are not specified, default values will be used. |
||
| 166 | * |
||
| 167 | * @param string $file |
||
| 168 | * @param string $db |
||
| 169 | */ |
||
| 170 | 6 | View Code Duplication | public function restore($file = null, $db = null) |
| 179 | } |
||
| 180 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.