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 | 19 | View Code Duplication | public function __construct(FactoryInterface $factory, |
| 32 | DIInvokerInterface $diInvoker, |
||
| 33 | Cache $cache, |
||
| 34 | array $annotations = null |
||
| 35 | ) |
||
| 36 | { |
||
| 37 | 19 | if($annotations){ |
|
| 38 | 10 | parent::__construct($annotations, $cache); |
|
| 39 | 10 | }else{ |
|
| 40 | 9 | parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
|
| 41 | } |
||
| 42 | 19 | $this->factory = $factory; |
|
| 43 | 19 | $this->diInvoker = $diInvoker; |
|
| 44 | 19 | } |
|
| 45 | /** |
||
| 46 | * load from class with local cache |
||
| 47 | * @param string $className |
||
| 48 | * @return EntityContainer |
||
| 49 | */ |
||
| 50 | 17 | 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 | } |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.