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 |
||
18 | final class CreateDatabaseDoctrineCommand extends Command |
||
19 | { |
||
20 | /** |
||
21 | * @var ConnectionRegistry |
||
22 | */ |
||
23 | private $connectionRegistry; |
||
24 | |||
25 | /** |
||
26 | * @param ConnectionRegistry $connectionRegistry |
||
27 | */ |
||
28 | 5 | public function __construct(ConnectionRegistry $connectionRegistry) |
|
34 | |||
35 | 5 | protected function configure() |
|
58 | |||
59 | /** |
||
60 | * @param InputInterface $input |
||
61 | * @param OutputInterface $output |
||
62 | * |
||
63 | * @return int |
||
64 | */ |
||
65 | 5 | protected function execute(InputInterface $input, OutputInterface $output): int |
|
66 | { |
||
67 | 5 | $connectionName = $this->getConnectionName($input); |
|
68 | |||
69 | 5 | $connection = $this->connectionRegistry->getConnection($connectionName); |
|
70 | |||
71 | 5 | $params = $this->getParams($connection); |
|
72 | |||
73 | 5 | $dbName = $this->getDbName($params, $connectionName); |
|
74 | |||
75 | 4 | $isPath = isset($params['path']); |
|
|
|||
76 | |||
77 | 4 | $ifNotExists = $input->getOption('if-not-exists'); |
|
78 | |||
79 | // Need to get rid of _every_ occurrence of dbname from connection configuration |
||
80 | 4 | unset($params['dbname'], $params['path'], $params['url']); |
|
81 | |||
82 | 4 | $tmpConnection = DriverManager::getConnection($params); |
|
83 | 4 | $shouldNotCreateDatabase = $ifNotExists && in_array($dbName, $tmpConnection->getSchemaManager()->listDatabases()); |
|
84 | |||
85 | // Only quote if we don't have a path |
||
86 | 4 | if (!$path) { |
|
87 | $dbName = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($dbName); |
||
88 | } |
||
89 | |||
90 | return $this->createDatabase($output, $connectionName, $tmpConnection, $dbName, $shouldNotCreateDatabase); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param InputInterface $input |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 5 | View Code Duplication | private function getConnectionName(InputInterface $input): string |
108 | |||
109 | /** |
||
110 | * @param Connection $connection |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | 5 | View Code Duplication | private function getParams(Connection $connection): array |
123 | |||
124 | /** |
||
125 | * @param array $params |
||
126 | * @param string $connectionName |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | 5 | View Code Duplication | private function getDbName(array $params, string $connectionName): string |
142 | |||
143 | /** |
||
144 | * @param OutputInterface $output |
||
145 | * @param string $connectionName |
||
146 | * @param Connection $tmpConnection |
||
147 | * @param string $dbName |
||
148 | * @param bool $shouldNotCreateDatabase |
||
149 | * |
||
150 | * @return int |
||
151 | */ |
||
152 | View Code Duplication | private function createDatabase( |
|
189 | } |
||
190 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.