| Conditions | 1 |
| Paths | 1 |
| Total Lines | 89 |
| Lines | 89 |
| Ratio | 100 % |
| 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 |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | protected function getOtherKey() |
||
| 26 | { |
||
| 27 | if (isset(static::$otherKey[$this->getName()])) { |
||
| 28 | return static::$otherKey[$this->getName()]; |
||
| 29 | } |
||
| 30 | |||
| 31 | $model = $this->getGrid()->model()->getOriginalModel(); |
||
|
|
|||
| 32 | |||
| 33 | if (is_callable([$model, $this->getName()]) && |
||
| 34 | ($relation = $model->{$this->getName()}()) instanceof Relation |
||
| 35 | ) { |
||
| 36 | /* @var Relation $relation */ |
||
| 37 | $fullKey = $relation->getQualifiedRelatedPivotKeyName(); |
||
| 38 | $fullKeyArray = explode('.', $fullKey); |
||
| 39 | |||
| 40 | return static::$otherKey[$this->getName()] = end($fullKeyArray); |
||
| 41 | } |
||
| 42 | |||
| 43 | throw new \Exception('Column of this field must be a `BelongsToMany` relation.'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @throws \Exception |
||
| 48 | * |
||
| 49 | * @return false|string|void |
||
| 50 | */ |
||
| 51 | protected function getOriginalData() |
||
| 52 | { |
||
| 53 | $relations = $this->getColumn()->getOriginal(); |
||
| 54 | |||
| 55 | if (is_string($relations)) { |
||
| 56 | $data = explode(',', $relations); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (!is_array($relations)) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | $first = current($relations); |
||
| 64 | |||
| 65 | if (is_null($first)) { |
||
| 66 | $data = null; |
||
| 67 | |||
| 68 | // MultipleSelect value store as an ont-to-many relationship. |
||
| 69 | } elseif (is_array($first)) { |
||
| 70 | foreach ($relations as $relation) { |
||
| 71 | $data[] = Arr::get($relation, "pivot.{$this->getOtherKey()}"); |
||
| 72 | } |
||
| 73 | |||
| 74 | // MultipleSelect value store as a column. |
||
| 75 | } else { |
||
| 76 | $data = $relations; |
||
| 77 | } |
||
| 78 | |||
| 79 | return json_encode($data); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: