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 |
||
14 | class EntityContainerBuilder extends ContainerBuilder |
||
15 | { |
||
16 | static $DEFAULT_ANNOTATIONS=[ |
||
17 | [ClassAnnotationHandler::class, 'class'], |
||
18 | [PropertyAnnotationHandler::class, 'properties'], |
||
19 | [VarAnnotationHandler::class, "properties.*.children[?name=='var'][]"], |
||
20 | [ValidateAnnotationHandler::class, "properties.*.children[?name=='v'][]"], |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * ControllerContainerBuilder constructor. |
||
25 | * @param FactoryInterface $factory |
||
26 | * @param DIInvokerInterface $diInvoker |
||
27 | * @param Cache $cache |
||
28 | * |
||
29 | * @param array $annotations |
||
30 | */ |
||
31 | 21 | View Code Duplication | public function __construct(FactoryInterface $factory, |
45 | /** |
||
46 | * load from class with local cache |
||
47 | * @param string $className |
||
48 | * @return EntityContainer |
||
49 | */ |
||
50 | 18 | public function build($className) |
|
54 | |||
55 | /** |
||
56 | * @param $className |
||
57 | * @return EntityContainer |
||
58 | */ |
||
59 | 9 | public function buildWithoutCache($className) |
|
63 | |||
64 | /** |
||
65 | * @param string $className |
||
66 | * @return EntityContainer |
||
67 | */ |
||
68 | 7 | protected function createContainer($className) |
|
72 | |||
73 | 9 | protected function handleAnnotation($handlerName, $container, $ann) |
|
78 | |||
79 | |||
80 | /** |
||
81 | * @var FactoryInterface |
||
82 | */ |
||
83 | protected $factory; |
||
84 | /** |
||
85 | * @var DIInvokerInterface |
||
86 | */ |
||
87 | protected $diInvoker; |
||
88 | } |
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.