Conditions | 21 |
Paths | 1540 |
Total Lines | 127 |
Code Lines | 40 |
Lines | 6 |
Ratio | 4.72 % |
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 namespace Anomaly\Streams\Platform\Support; |
||
69 | public function make($parameters, $entry, $term = 'entry', $payload = []) |
||
70 | { |
||
71 | $payload[$term] = $entry; |
||
72 | |||
73 | /* |
||
74 | * If a flat value was sent in |
||
75 | * then convert it to an array. |
||
76 | */ |
||
77 | if (!is_array($parameters)) { |
||
78 | $parameters = [ |
||
79 | 'value' => $parameters, |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | $value = array_get($parameters, 'value'); |
||
84 | |||
85 | /* |
||
86 | * If the value is a view path then return a view. |
||
87 | */ |
||
88 | View Code Duplication | if ($view = array_get($parameters, 'view')) { |
|
|
|||
89 | return view($view, ['value' => $value, $term => $entry]); |
||
90 | } |
||
91 | |||
92 | /* |
||
93 | * If the value uses a template then parse it. |
||
94 | */ |
||
95 | View Code Duplication | if ($template = array_get($parameters, 'template')) { |
|
96 | return $this->template->render($template, ['value' => $value, $term => $entry]); |
||
97 | } |
||
98 | |||
99 | /* |
||
100 | * If the entry is an instance of EntryInterface |
||
101 | * then try getting the field value from the entry. |
||
102 | */ |
||
103 | if ($entry instanceof EntryInterface && $entry->getField($value)) { |
||
104 | |||
105 | /* @var EntryInterface $relation */ |
||
106 | if ($entry->assignmentIsRelationship($value) && $relation = $entry->{camel_case($value)}) { |
||
107 | if ($relation instanceof EloquentModel) { |
||
108 | $value = $relation->getTitle(); |
||
109 | } |
||
110 | } else { |
||
111 | $value = $entry->getFieldValue($value); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /* |
||
116 | * Decorate the entry object before |
||
117 | * sending to decorate so that data_get() |
||
118 | * can get into the presenter methods. |
||
119 | */ |
||
120 | $payload[$term] = $entry = $this->decorator->decorate($entry); |
||
121 | |||
122 | /* |
||
123 | * If the value matches a dot notation |
||
124 | * then parse it as a template. |
||
125 | */ |
||
126 | if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { |
||
127 | $value = $this->template->render("{{ {$value}|raw }}", $payload); |
||
128 | } |
||
129 | |||
130 | /* |
||
131 | * If the value matches a method in the presenter. |
||
132 | */ |
||
133 | if (is_string($value) && preg_match("/^{$term}.([a-zA-Z\\_]+)/", $value, $match)) { |
||
134 | if (method_exists($entry, camel_case($match[1]))) { |
||
135 | $value = $entry->{camel_case($match[1])}(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $payload[$term] = $entry; |
||
140 | |||
141 | /* |
||
142 | * By default we can just pass the value through |
||
143 | * the evaluator utility and be done with it. |
||
144 | */ |
||
145 | $value = $this->evaluator->evaluate($value, $payload); |
||
146 | |||
147 | /* |
||
148 | * Lastly, prepare the entry to be |
||
149 | * parsed into the string. |
||
150 | */ |
||
151 | if ($entry instanceof Arrayable) { |
||
152 | $entry = $entry->toArray(); |
||
153 | } |
||
154 | |||
155 | /* |
||
156 | * Parse the value with the entry. |
||
157 | */ |
||
158 | if ($wrapper = array_get($parameters, 'wrapper')) { |
||
159 | $value = $this->parser->parse( |
||
160 | $wrapper, |
||
161 | ['value' => $value, $term => $entry] |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | /* |
||
166 | * Parse the value with the value too. |
||
167 | */ |
||
168 | if (is_string($value)) { |
||
169 | $value = $this->parser->parse( |
||
170 | $value, |
||
171 | [ |
||
172 | 'value' => $value, |
||
173 | $term => $entry, |
||
174 | ] |
||
175 | ); |
||
176 | } |
||
177 | |||
178 | /* |
||
179 | * If the value looks like a language |
||
180 | * key then try translating it. |
||
181 | */ |
||
182 | if (is_string($value) && str_is('*.*.*::*', $value)) { |
||
183 | $value = trans($value); |
||
184 | } |
||
185 | |||
186 | /* |
||
187 | * If the value looks like a render-able |
||
188 | * string then render it. |
||
189 | */ |
||
190 | if (is_string($value) && str_contains($value, '{{')) { |
||
191 | $value = $this->template->render($value, [$term => $entry]); |
||
192 | } |
||
193 | |||
194 | return $value; |
||
195 | } |
||
196 | } |
||
197 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.