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 Comodojo\Extender\Schedule; |
||
| 32 | class Manager { |
||
| 33 | |||
| 34 | use ConfigurationTrait; |
||
| 35 | use LoggerTrait; |
||
| 36 | use EventsTrait; |
||
| 37 | use EntityManagerTrait; |
||
| 38 | |||
| 39 | // protected $locker; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Class constructor |
||
| 43 | * |
||
| 44 | * @param string $manager_name |
||
| 45 | * @param Configuration $configuration |
||
| 46 | * @param LoggerInterface $logger |
||
| 47 | * @param TasksTable $tasks |
||
| 48 | * @param EventsManager $events |
||
| 49 | * @param EntityManager $em |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function __construct( |
|
| 69 | |||
| 70 | public function getJobById($id) { |
||
| 77 | |||
| 78 | public function getJobs($ready = false) { |
||
| 93 | |||
| 94 | public function getNextCycleTimestamp() { |
||
| 110 | |||
| 111 | public function updateSchedules(array $results) { |
||
| 134 | |||
| 135 | // public function remove(array $queue) { |
||
| 136 | // |
||
| 137 | // $em = $this->getEntityManager(); |
||
| 138 | // |
||
| 139 | // foreach ($queue as $record) { |
||
| 140 | // $em->remove($record); |
||
| 141 | // } |
||
| 142 | // |
||
| 143 | // $em->flush(); |
||
| 144 | // |
||
| 145 | // } |
||
| 146 | // |
||
| 147 | // public function add($name, Request $request) { |
||
| 148 | // |
||
| 149 | // $em = $this->getEntityManager(); |
||
| 150 | // |
||
| 151 | // $uid = $this->doAddRequest($name, $request, $em); |
||
| 152 | // |
||
| 153 | // $em->flush(); |
||
| 154 | // |
||
| 155 | // return $uid; |
||
| 156 | // |
||
| 157 | // } |
||
| 158 | // |
||
| 159 | // public function addBulk(array $queue) { |
||
| 160 | // |
||
| 161 | // $em = $this->getEntityManager(); |
||
| 162 | // |
||
| 163 | // $records = []; |
||
| 164 | // |
||
| 165 | // foreach ($queue as $name => $request) { |
||
| 166 | // $records[] = $this->doAddRequest($name, $request, $em); |
||
| 167 | // } |
||
| 168 | // |
||
| 169 | // $em->flush(); |
||
| 170 | // |
||
| 171 | // return $records; |
||
| 172 | // |
||
| 173 | // } |
||
| 174 | // |
||
| 175 | // protected function doAddRequest($name, Request $request, EntityManager $em) { |
||
| 176 | // |
||
| 177 | // $record = new Queue(); |
||
| 178 | // $record->setName($name)->setRequest($request); |
||
| 179 | // |
||
| 180 | // $em->persist($record); |
||
| 181 | // |
||
| 182 | // return $request->getUid(); |
||
| 183 | // |
||
| 184 | // } |
||
| 185 | |||
| 186 | } |
||
| 187 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: