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 TraitProxy extends ClassProxy |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * List of advices for traits |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected static $traitAdvices = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Overridden static property for TraitProxy |
||
| 34 | * |
||
| 35 | * {@inheritDoc} |
||
| 36 | */ |
||
| 37 | protected static $invocationClassMap = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Inject advices for given trait |
||
| 41 | * |
||
| 42 | * NB This method will be used as a callback during source code evaluation to inject joinpoints |
||
| 43 | * |
||
| 44 | * @param string $className Aop child proxy class |
||
| 45 | * @param array|\Go\Aop\Advice[] $traitAdvices List of advices to inject into class |
||
| 46 | * |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public static function injectJoinPoints($className, array $traitAdvices = []) |
||
| 53 | |||
| 54 | public static function getJoinPoint($traitName, $className, $joinPointType, $pointName) |
||
| 55 | { |
||
| 56 | /** @var LazyAdvisorAccessor $accessor */ |
||
| 57 | static $accessor = null; |
||
| 58 | |||
| 59 | View Code Duplication | if (empty(self::$invocationClassMap)) { |
|
|
|
|||
| 60 | $aspectKernel = AspectKernel::getInstance(); |
||
| 61 | $accessor = $aspectKernel->getContainer()->get('aspect.advisor.accessor'); |
||
| 62 | self::setMappings( |
||
| 63 | $aspectKernel->hasFeature(Features::USE_SPLAT_OPERATOR) |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | $advices = self::$traitAdvices[$traitName][$joinPointType][$pointName]; |
||
| 68 | |||
| 69 | $filledAdvices = []; |
||
| 70 | foreach ($advices as $advisorName) { |
||
| 71 | $filledAdvices[] = $accessor->$advisorName; |
||
| 72 | } |
||
| 73 | |||
| 74 | $joinpoint = new self::$invocationClassMap[$joinPointType]($className, $pointName . '➩', $filledAdvices); |
||
| 75 | |||
| 76 | return $joinpoint; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Creates definition for trait method body |
||
| 81 | * |
||
| 82 | * @param ParsedMethod $method Method reflection |
||
| 83 | * |
||
| 84 | * @return string new method body |
||
| 85 | */ |
||
| 86 | protected function getJoinpointInvocationBody(ParsedMethod $method) |
||
| 87 | { |
||
| 88 | $isStatic = $method->isStatic(); |
||
| 89 | $class = '\\' . __CLASS__; |
||
| 90 | $scope = $isStatic ? self::$staticLsbExpression : '$this'; |
||
| 91 | $prefix = $isStatic ? AspectContainer::STATIC_METHOD_PREFIX : AspectContainer::METHOD_PREFIX; |
||
| 92 | |||
| 93 | $args = $this->prepareArgsLine($method); |
||
| 94 | $args = $scope . ($args ? ", [$args]" : ''); |
||
| 95 | |||
| 96 | return <<<BODY |
||
| 97 | static \$__joinPoint = null; |
||
| 98 | if (!\$__joinPoint) { |
||
| 99 | \$__joinPoint = {$class}::getJoinPoint(__TRAIT__, __CLASS__, '{$prefix}', '{$method->name}'); |
||
| 100 | } |
||
| 101 | return \$__joinPoint->__invoke($args); |
||
| 102 | BODY; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | public function __toString() |
||
| 130 | |||
| 131 | private function getMethodAliasesCode() |
||
| 140 | } |
||
| 141 |
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.