| Conditions | 2 |
| Total Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public static function castUsing(array $arguments) |
||
| 22 | { |
||
| 23 | $encoder = $arguments[0] ?? null; |
||
| 24 | $metadataClass = $arguments[1] ?? Metadata::class; |
||
| 25 | |||
| 26 | return new class($encoder, $metadataClass) implements CastsAttributes { |
||
|
|
|||
| 27 | |||
| 28 | protected $encoder = 'json'; |
||
| 29 | protected $metadataClass = Metadata::class; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * constructor. |
||
| 33 | * @param $encoder |
||
| 34 | */ |
||
| 35 | public function __construct($encoder, $metadataClass) |
||
| 36 | { |
||
| 37 | $this->encoder = $encoder; |
||
| 38 | $this->metadataClass = $metadataClass; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritDoc |
||
| 43 | */ |
||
| 44 | public function get($model, $key, $value, $attributes): Metadata |
||
| 45 | { |
||
| 46 | if (empty($value)) { |
||
| 47 | $value = []; |
||
| 48 | } else { |
||
| 49 | $value = $this->decode($value); |
||
| 50 | } |
||
| 51 | if (!is_array($value)) { |
||
| 52 | $value = []; |
||
| 53 | } |
||
| 54 | return (new $this->metadataClass($value)) |
||
| 55 | ->setObserver( |
||
| 56 | function (Metadata $metadata) use ($model, $key) { |
||
| 57 | $model->set($key, $metadata); |
||
| 58 | } |
||
| 59 | ); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @inheritDoc |
||
| 64 | */ |
||
| 65 | public function set($model, $key, $value, $attributes) |
||
| 66 | { |
||
| 67 | if (is_string($value)) { |
||
| 68 | return [$key => $value]; |
||
| 69 | } |
||
| 70 | if ($value instanceof Metadata) { |
||
| 71 | $value = $this->encode($value); |
||
| 72 | } |
||
| 73 | return [$key => $value]; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @inheritDoc |
||
| 78 | */ |
||
| 79 | public function serialize($model, string $key, $value, array $attributes) |
||
| 80 | { |
||
| 81 | return $value->getArrayCopy(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param $value |
||
| 86 | * @return false|string |
||
| 87 | */ |
||
| 88 | protected function encode($value) |
||
| 89 | { |
||
| 90 | if ($this->encoder == 'serialize') { |
||
| 91 | return $value instanceof Metadata ? $value->serialize() : serialize($value); |
||
| 92 | } |
||
| 93 | return json_encode($value); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param $value |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | protected function decode($value) |
||
| 101 | { |
||
| 102 | if ($this->encoder == 'serialize') { |
||
| 103 | return unserialize($value); |
||
| 104 | } |
||
| 105 | return Json::decode($value, true); |
||
| 106 | } |
||
| 110 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: