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 |
||
21 | class DatabaseManager extends Component implements InjectorInterface, DatabasesInterface |
||
22 | { |
||
23 | /** |
||
24 | * Declares to Spiral IoC that component instance should be treated as singleton. |
||
25 | */ |
||
26 | const SINGLETON = self::class; |
||
27 | |||
28 | /** |
||
29 | * By default spiral will force time conversion into single timezone before storing in |
||
30 | * database, it will help us to ensure that we have no problems with switching timezones and |
||
31 | * save a lot of time while development (potentially). |
||
32 | */ |
||
33 | const DEFAULT_TIMEZONE = 'UTC'; |
||
34 | |||
35 | /** |
||
36 | * @var Database[] |
||
37 | */ |
||
38 | private $databases = []; |
||
39 | |||
40 | /** |
||
41 | * @var Driver[] |
||
42 | */ |
||
43 | private $drivers = []; |
||
44 | |||
45 | /** |
||
46 | * @var DatabasesConfig |
||
47 | */ |
||
48 | protected $config = null; |
||
49 | |||
50 | /** |
||
51 | * @invisible |
||
52 | * @var FactoryInterface |
||
53 | */ |
||
54 | protected $factory = null; |
||
55 | |||
56 | /** |
||
57 | * @param DatabasesConfig $config |
||
58 | * @param FactoryInterface $factory |
||
59 | */ |
||
60 | public function __construct(DatabasesConfig $config, FactoryInterface $factory) |
||
61 | { |
||
62 | $this->config = $config; |
||
63 | $this->factory = $factory; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | * |
||
69 | * @return Database |
||
70 | */ |
||
71 | View Code Duplication | public function database($database = null) |
|
99 | |||
100 | /** |
||
101 | * Get driver by it's name. Every driver associated with configured connection, there is minor |
||
102 | * de-sync in naming due legacy code. |
||
103 | * |
||
104 | * @param string $connection |
||
105 | * @return Driver |
||
106 | * @throws DatabaseException |
||
107 | */ |
||
108 | public function driver($connection) |
||
130 | |||
131 | /** |
||
132 | * Get instance of every available database. |
||
133 | * |
||
134 | * @return Database[] |
||
135 | * @throws DatabaseException |
||
136 | */ |
||
137 | public function getDatabases() |
||
146 | |||
147 | /** |
||
148 | * Get instance of every available driver/connection. |
||
149 | * |
||
150 | * @return Driver[] |
||
151 | * @throws DatabaseException |
||
152 | */ |
||
153 | public function getDrivers() |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | public function createInjection(\ReflectionClass $class, $context = null) |
||
171 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.