Complex classes like Serializer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Serializer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | final class Serializer implements SerializerInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Denotes the current object depth of the serializer. |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $depth = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritDoc} |
||
| 34 | */ |
||
| 35 | public function serialize(Model $model = null, AdapterInterface $adapter) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritDoc} |
||
| 46 | */ |
||
| 47 | public function serializeCollection(Collection $collection, AdapterInterface $adapter) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * {@inheritDoc} |
||
| 54 | */ |
||
| 55 | public function serializeArray(array $models, AdapterInterface $adapter) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritDoc} |
||
| 66 | */ |
||
| 67 | public function serializeError($title, $message, $httpCode) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Serializes the "interior" of a model. |
||
| 78 | * This is the serialization that takes place outside of a "data" container. |
||
| 79 | * Can be used for root model and relationship model serialization. |
||
| 80 | * |
||
| 81 | * @param Model $model |
||
| 82 | * @param AdapterInterface $adapter |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | protected function serializeModel(Model $model, AdapterInterface $adapter) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Serializes an attribute value. |
||
| 122 | * |
||
| 123 | * @param mixed $value |
||
| 124 | * @param AttributeMetadata $attrMeta |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | protected function serializeAttribute($value, AttributeMetadata $attrMeta) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Serializes an embed value. |
||
| 144 | * |
||
| 145 | * @param Embed|EmbedCollection|null $value |
||
| 146 | * @param EmbeddedPropMetadata $embeddedPropMeta |
||
| 147 | * @return array|null |
||
| 148 | */ |
||
| 149 | protected function serializeEmbed($value, EmbeddedPropMetadata $embeddedPropMeta) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Serializes an embed one value. |
||
| 160 | * |
||
| 161 | * @param EmbedMetadata $embedMeta |
||
| 162 | * @param Embed|null $embed |
||
| 163 | * @return array|null |
||
| 164 | */ |
||
| 165 | protected function serializeEmbedOne(EmbedMetadata $embedMeta, Embed $embed = null) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Serializes an embed many value. |
||
| 183 | * |
||
| 184 | * @param EmbedMetadata $embedMeta |
||
| 185 | * @param Embed|null $embed |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function serializeEmbedMany(EmbedMetadata $embedMeta, EmbedCollection $collection) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Serializes a relationship value |
||
| 199 | * |
||
| 200 | * @param Model $owner |
||
| 201 | * @param Model|Model[]|null $relationship |
||
| 202 | * @param RelationshipMetadata $relMeta |
||
| 203 | * @param AdapterInterface $adapter |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function serializeRelationship(Model $owner, $relationship = null, RelationshipMetadata $relMeta, AdapterInterface $adapter) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Serializes a has-many relationship value |
||
| 229 | * |
||
| 230 | * @param Model $owner |
||
| 231 | * @param Model[]|null $models |
||
| 232 | * @param AdapterInterface $adapter |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | protected function serializeHasMany(Model $owner, array $models = null, AdapterInterface $adapter) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Serializes a has-one relationship value |
||
| 245 | * |
||
| 246 | * @param Model $owner |
||
| 247 | * @param Model|null $model |
||
| 248 | * @param AdapterInterface $adapter |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | protected function serializeHasOne(Model $owner, Model $model = null, AdapterInterface $adapter) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Encodes the formatted payload array. |
||
| 258 | * |
||
| 259 | * @param array $payload |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | private function encode(array $payload) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Increases the serializer depth. |
||
| 269 | * |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | protected function increaseDepth() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Decreases the serializer depth. |
||
| 280 | * |
||
| 281 | * @return self |
||
| 282 | */ |
||
| 283 | protected function decreaseDepth() |
||
| 290 | } |
||
| 291 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.