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 | ||
| 13 | class ClassEnsurance | ||
| 14 | { | ||
| 15 | /** | ||
| 16 | * @var string | ||
| 17 | */ | ||
| 18 | private $class; | ||
| 19 | /** | ||
| 20 | * @var ReflectionClass | ||
| 21 | */ | ||
| 22 | private $reflection; | ||
| 23 | |||
| 24 | use EnforcementTrait; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * ClassEnsurance constructor. | ||
| 28 | * | ||
| 29 | * @param string $class | ||
| 30 | * | ||
| 31 | * @throws EnsuranceException | ||
| 32 | */ | ||
| 33 | public function __construct(string $class) | ||
| 42 | |||
| 43 | /** | ||
| 44 | * @return string | ||
| 45 | */ | ||
| 46 | final protected function getClass(): string | ||
| 50 | |||
| 51 | /** | ||
| 52 | * @return ReflectionClass | ||
| 53 | */ | ||
| 54 | final protected function getReflection(): ReflectionClass | ||
| 58 | |||
| 59 | /** | ||
| 60 | * @param string $class | ||
| 61 | * | ||
| 62 | * @return ClassEnsurance | ||
| 63 | */ | ||
| 64 | public function is(string $class): ClassEnsurance | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @param string $class | ||
| 73 | * | ||
| 74 | * @return ClassEnsurance | ||
| 75 | */ | ||
| 76 | public function isNot(string $class): ClassEnsurance | ||
| 82 | |||
| 83 | /** | ||
| 84 | * @param string $class | ||
| 85 | * | ||
| 86 | * @return ClassEnsurance | ||
| 87 | */ | ||
| 88 | public function extends (string $class): ClassEnsurance | ||
| 94 | |||
| 95 | /** | ||
| 96 | * @param string $class | ||
| 97 | * | ||
| 98 | * @return ClassEnsurance | ||
| 99 | */ | ||
| 100 | public function extendsNot(string $class): ClassEnsurance | ||
| 106 | |||
| 107 | /** | ||
| 108 | * @param string $interface | ||
| 109 | * | ||
| 110 | * @return ClassEnsurance | ||
| 111 | */ | ||
| 112 | public function implements (string $interface): ClassEnsurance | ||
| 120 | |||
| 121 | /** | ||
| 122 | * @param string $interface | ||
| 123 | * | ||
| 124 | * @return ClassEnsurance | ||
| 125 | */ | ||
| 126 | public function implementsNot(string $interface): ClassEnsurance | ||
| 133 | |||
| 134 | /** | ||
| 135 | * @param string $class | ||
| 136 | * | ||
| 137 | * @return ClassEnsurance | ||
| 138 | */ | ||
| 139 | View Code Duplication | public function isParentOf(string $class): ClassEnsurance | |
| 146 | |||
| 147 | /** | ||
| 148 | * @param string $class | ||
| 149 | * | ||
| 150 | * @return ClassEnsurance | ||
| 151 | */ | ||
| 152 | View Code Duplication | public function isNotParentOf(string $class): ClassEnsurance | |
| 159 | |||
| 160 | /** | ||
| 161 | * @param string $trait | ||
| 162 | * | ||
| 163 | * @return ClassEnsurance | ||
| 164 | */ | ||
| 165 | public function uses(string $trait): ClassEnsurance | ||
| 172 | |||
| 173 | /** | ||
| 174 | * @param string $property | ||
| 175 | * | ||
| 176 | * @return ClassEnsurance | ||
| 177 | */ | ||
| 178 | public function hasProperty(string $property): ClassEnsurance | ||
| 185 | |||
| 186 | /** | ||
| 187 | * @param string $method | ||
| 188 | * | ||
| 189 | * @return ClassEnsurance | ||
| 190 | */ | ||
| 191 | public function hasMethod(string $method): ClassEnsurance | ||
| 198 | } |