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 namespace Limoncello\Application\Data; |
||
32 | abstract class BaseSeedRunner |
||
33 | { |
||
34 | /** Seed column name */ |
||
35 | const SEEDS_COLUMN_ID = 'id'; |
||
36 | |||
37 | /** Seed column name */ |
||
38 | const SEEDS_COLUMN_CLASS = 'class'; |
||
39 | |||
40 | /** Seed column name */ |
||
41 | const SEEDS_COLUMN_SEEDED_AT = 'seeded_at'; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $seedsTable; |
||
47 | |||
48 | /** |
||
49 | * @var null|callable |
||
50 | */ |
||
51 | private $seedInit = null; |
||
52 | |||
53 | /** |
||
54 | * @var IoInterface |
||
55 | */ |
||
56 | private $inOut; |
||
57 | |||
58 | /** |
||
59 | * @return string[] |
||
60 | */ |
||
61 | abstract protected function getSeedClasses(): array; |
||
62 | |||
63 | /** |
||
64 | * @param IoInterface $inOut |
||
65 | * @param callable $seedInit |
||
|
|||
66 | * @param string $seedsTable |
||
67 | */ |
||
68 | 2 | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * @param ContainerInterface $container |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | 1 | public function run(ContainerInterface $container): void |
|
87 | { |
||
88 | 1 | foreach ($this->getSeeds($container) as $seederClass) { |
|
89 | 1 | $this->getIO()->writeInfo("Starting seed for `$seederClass`..." . PHP_EOL, IoInterface::VERBOSITY_VERBOSE); |
|
90 | 1 | $this->executeSeedInit($container, $seederClass); |
|
91 | /** @var SeedInterface $seeder */ |
||
92 | 1 | $seeder = new $seederClass(); |
|
93 | 1 | $seeder->init($container)->run(); |
|
94 | 1 | $this->getIO()->writeInfo("Seed finished for `$seederClass`." . PHP_EOL, IoInterface::VERBOSITY_NORMAL); |
|
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param ContainerInterface $container |
||
100 | * |
||
101 | * @return Generator |
||
102 | * |
||
103 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
104 | */ |
||
105 | 1 | View Code Duplication | protected function getSeeds(ContainerInterface $container): Generator |
106 | { |
||
107 | 1 | $connection = $this->getConnection($container); |
|
108 | 1 | $manager = $connection->getSchemaManager(); |
|
109 | |||
110 | 1 | if ($manager->tablesExist([$this->getSeedsTable()]) === true) { |
|
111 | 1 | $seeded = $this->readSeeded($connection); |
|
112 | } else { |
||
113 | 1 | $this->createSeedsTable($manager); |
|
114 | 1 | $seeded = []; |
|
115 | } |
||
116 | |||
117 | 1 | $notYetSeeded = array_diff($this->getSeedClasses(), $seeded); |
|
118 | |||
119 | 1 | foreach ($notYetSeeded as $class) { |
|
120 | 1 | yield $class; |
|
121 | 1 | $this->saveSeed($connection, $class); |
|
122 | } |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param ContainerInterface $container |
||
127 | * |
||
128 | * @return Connection |
||
129 | */ |
||
130 | 1 | protected function getConnection(ContainerInterface $container): Connection |
|
131 | { |
||
132 | 1 | return $container->get(Connection::class); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param ContainerInterface $container |
||
137 | * @param string $seedClass |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 1 | protected function executeSeedInit(ContainerInterface $container, string $seedClass): void |
|
142 | { |
||
143 | 1 | if ($this->seedInit !== null) { |
|
144 | 1 | call_user_func($this->seedInit, $container, $seedClass); |
|
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return IoInterface |
||
150 | */ |
||
151 | 1 | protected function getIO(): IoInterface |
|
152 | { |
||
153 | 1 | return $this->inOut; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @param IoInterface $inOut |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | 2 | private function setIO(IoInterface $inOut): self |
|
167 | |||
168 | /** |
||
169 | * @param AbstractSchemaManager $manager |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | 1 | View Code Duplication | private function createSeedsTable(AbstractSchemaManager $manager): void |
192 | |||
193 | /** |
||
194 | * @param Connection $connection |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | 1 | private function readSeeded(Connection $connection): array |
|
219 | |||
220 | /** |
||
221 | * @param Connection $connection |
||
222 | * @param string $class |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | 1 | View Code Duplication | private function saveSeed(Connection $connection, string $class): void |
235 | |||
236 | /** |
||
237 | * @return string |
||
238 | */ |
||
239 | 1 | private function getSeedsTable(): string |
|
243 | } |
||
244 |
This check looks for
@param
annotations 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. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.