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 |
||
| 22 | class DependencyInjector |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var DependencyInjector |
||
| 27 | */ |
||
| 28 | private static $singleton; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Container |
||
| 33 | */ |
||
| 34 | private $container; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Container $container |
||
| 38 | */ |
||
| 39 | private function __construct( Container $container ){ |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return DependencyInjector |
||
| 45 | * @throws \Exception |
||
| 46 | */ |
||
| 47 | public static function instance(){ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param Container $container |
||
| 56 | */ |
||
| 57 | public static function init( Container $container ){ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Inject dependencies from, or including, the Pimple container, into a given method on a given object |
||
| 64 | * @param $object |
||
| 65 | * @param $methodName |
||
| 66 | * @return mixed The method's return value |
||
| 67 | */ |
||
| 68 | View Code Duplication | public function injectIntoMethod( $object, $methodName = "injectDependencies" ){ |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Inject dependencies from, or including, the Pimple container, into a a given class's constructor |
||
| 90 | * Optionally providing a number of arguments to the constructor to prepend. |
||
| 91 | * |
||
| 92 | * @param string $classToBuildName |
||
| 93 | * @param array $toInject |
||
| 94 | * @return bool Whether the method was found |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function injectIntoConstructor( $classToBuildName, array $toInject=[] ){ |
|
| 119 | |||
| 120 | } |
||
| 121 |