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 declare(strict_types=1); |
||
| 13 | trait UsesPHPMetaDataTrait |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var \ReflectionClass |
||
| 18 | */ |
||
| 19 | private static $reflectionClass; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private static $singular; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $plural; |
||
| 30 | |||
| 31 | |||
| 32 | public function __construct() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Find and run all init methods |
||
| 39 | * - defined in relationship traits and generally to init ArrayCollection properties |
||
| 40 | */ |
||
| 41 | protected function runInitMethods(): void |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Loads the class and property meta data in the class |
||
| 56 | * |
||
| 57 | * This is the method called by Doctrine to load the meta data |
||
| 58 | * |
||
| 59 | * @param DoctrineClassMetaData $metadata |
||
| 60 | * |
||
| 61 | * @throws DoctrineStaticMetaException |
||
| 62 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 63 | */ |
||
| 64 | public static function loadMetadata(DoctrineClassMetaData $metadata): void |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Setthing the change policy to be deferred explicit for the moment. Should consider if this needs to be |
||
| 83 | * configurable in the future |
||
| 84 | * |
||
| 85 | * @see http://doctrine-orm.readthedocs.io/en/latest/reference/change-tracking-policies.html |
||
| 86 | * |
||
| 87 | * @param ClassMetadataBuilder $builder |
||
| 88 | */ |
||
| 89 | public static function setChangeTrackingPolicy(ClassMetadataBuilder $builder) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * This method will reflect on the entity class and pull out all the methods that begin with |
||
| 96 | * UsesPHPMetaDataInterface::METHOD_PREFIX_GET_PROPERTY_DOCTRINE_META |
||
| 97 | * |
||
| 98 | * Once it has an array of methods, it calls them all, passing in the $builder |
||
| 99 | * |
||
| 100 | * @param ClassMetadataBuilder $builder |
||
| 101 | * |
||
| 102 | * @throws DoctrineStaticMetaException |
||
| 103 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 104 | */ |
||
| 105 | View Code Duplication | protected static function loadPropertyDoctrineMetaData(ClassMetadataBuilder $builder): void |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get class level meta data, eg table name |
||
| 128 | * |
||
| 129 | * @param ClassMetadataBuilder $builder |
||
| 130 | * |
||
| 131 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 132 | */ |
||
| 133 | protected static function loadClassDoctrineMetaData(ClassMetadataBuilder $builder): void |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Get an array of all static methods implemented by the current class |
||
| 141 | * |
||
| 142 | * Merges trait methods |
||
| 143 | * Filters out this trait |
||
| 144 | * |
||
| 145 | * @return array|\ReflectionMethod[] |
||
| 146 | * @throws \ReflectionException |
||
| 147 | */ |
||
| 148 | protected static function getStaticMethods(): array |
||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the property name the Entity is mapped by when plural |
||
| 182 | * |
||
| 183 | * Override it in your entity class if you are using an Entity class name that doesn't pluralize nicely |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | * @throws DoctrineStaticMetaException |
||
| 187 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 188 | */ |
||
| 189 | public static function getPlural(): string |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get the property the name the Entity is mapped by when singular |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | * @throws DoctrineStaticMetaException |
||
| 208 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 209 | */ |
||
| 210 | public static function getSingular(): string |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Which field is being used for ID - will normally be `id` as implemented by |
||
| 242 | * \EdmondsCommerce\DoctrineStaticMeta\Fields\Traits\IdField |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 246 | */ |
||
| 247 | public static function getIdField(): string |
||
| 251 | } |
||
| 252 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: