| Conditions | 4 |
| Paths | 3 |
| Total Lines | 53 |
| Code Lines | 36 |
| 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 get() |
||
| 22 | { |
||
| 23 | $blocksPath = base_path('blocks/'); |
||
| 24 | $directories = (new Filesystem)->directories($blocksPath); |
||
| 25 | $linkTypes = collect(); |
||
| 26 | |||
| 27 | // Prepend "predefined" entry to the $linkTypes list |
||
| 28 | $predefinedLinkType = new self([ |
||
| 29 | 'id' => 1, |
||
| 30 | 'typename' => 'predefined', |
||
| 31 | 'title' => null, |
||
| 32 | 'description' => null, |
||
| 33 | 'icon' => 'bi bi-boxes', |
||
| 34 | 'custom_html' => false, |
||
| 35 | ]); |
||
| 36 | |||
| 37 | $linkTypes->prepend($predefinedLinkType); |
||
| 38 | |||
| 39 | foreach ($directories as $dir) { |
||
| 40 | $configPath = $dir . '/config.yml'; |
||
| 41 | if (file_exists($configPath)) { |
||
| 42 | $configData = Yaml::parse(file_get_contents($configPath)); |
||
| 43 | |||
| 44 | // Create a new instance of LinkType for each config file |
||
| 45 | $linkType = new self([ |
||
| 46 | 'id' => $configData['id'] ?? 0, |
||
| 47 | 'typename' => $configData['typename'] ?? null, |
||
| 48 | 'title' => $configData['title'] ?? null, |
||
| 49 | 'description' => $configData['description'] ?? null, |
||
| 50 | 'icon' => $configData['icon'] ?? null, |
||
| 51 | 'custom_html' => $configData['custom_html'] ?? [], |
||
| 52 | ]); |
||
| 53 | $linkTypes->push($linkType); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | $custom_order = [ |
||
| 58 | 'predefined', |
||
| 59 | 'link', |
||
| 60 | 'vcard', |
||
| 61 | 'email', |
||
| 62 | 'telephone', |
||
| 63 | 'heading', |
||
| 64 | 'spacer', |
||
| 65 | 'text', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | $sorted = $linkTypes->sortBy(function ($item) use ($custom_order) { |
||
| 69 | $index = array_search($item->typename, $custom_order); |
||
| 70 | return $index !== false ? $index : count($custom_order); |
||
| 71 | }); |
||
| 72 | |||
| 73 | return $sorted->values(); |
||
| 74 | } |
||
| 98 |