| Conditions | 15 |
| Paths | 1 |
| Total Lines | 96 |
| Code Lines | 59 |
| 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 boot(): void |
||
| 22 | { |
||
| 23 | App::$Event->on(Main::SEARCH_EVENT_NAME, function ($model) { |
||
| 24 | /** @var CollectionSearchResults $model */ |
||
| 25 | $records = \Apps\ActiveRecord\App::search($model->getQuery()) |
||
| 26 | ->take($model->getLimit()) |
||
| 27 | ->get(); |
||
| 28 | |||
| 29 | /** @var \Apps\ActiveRecord\App[]|Collection $records */ |
||
| 30 | $records->each(function($item) use ($model) { |
||
| 31 | /** @var \Apps\ActiveRecord\App $item */ |
||
| 32 | $text = App::$Translate->get('Main', 'Type: %type%, system name: %sys%, version: %version%', [ |
||
| 33 | 'type' => $item->type, |
||
| 34 | 'sys' => $item->sys_name, |
||
| 35 | 'version' => $item->version |
||
| 36 | ]); |
||
| 37 | |||
| 38 | // initialize abstract response pattern |
||
| 39 | $res = new AbstractSearchItem(); |
||
| 40 | $res->setTitle($item->getLocaled('name')); |
||
|
|
|||
| 41 | $res->setSnippet($text); |
||
| 42 | $res->setDate($item->created_at); |
||
| 43 | $res->setRelevance((int)$item->relevance); |
||
| 44 | $res->setUrl(Str::lowerCase($item->sys_name) . '/index'); |
||
| 45 | $res->setMarker('App'); |
||
| 46 | |||
| 47 | $model->add($res); |
||
| 48 | }); |
||
| 49 | |||
| 50 | // search in translation files |
||
| 51 | $usedLanguage = App::$Request->getLanguage(); |
||
| 52 | $onlyKeys = false; |
||
| 53 | if ($usedLanguage === 'en') { |
||
| 54 | $usedLanguage = 'ru'; // use "ru" locale with keys as default |
||
| 55 | $onlyKeys = true; |
||
| 56 | } |
||
| 57 | |||
| 58 | // prepare search words for input query |
||
| 59 | $searchWords = explode(' ', $model->getQuery()); |
||
| 60 | $usedWords = array_filter($searchWords, function($value) { |
||
| 61 | return Str::length($value) > 2; |
||
| 62 | }); |
||
| 63 | |||
| 64 | if (!$usedWords || count($usedWords) < 1) { |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | $files = File::listFiles('/I18n/Admin/' . $usedLanguage, ['.php']); |
||
| 69 | // each translation files |
||
| 70 | foreach ($files as $file) { |
||
| 71 | // prepare file name and route target |
||
| 72 | $name = Str::lastIn($file, DIRECTORY_SEPARATOR, true); |
||
| 73 | $route = Str::lowerCase(Str::firstIn($name, '.')); |
||
| 74 | if (Arr::in($name, ['Main.php', 'Default.php'])) { // do not process main & defaults |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | $lines = File::inc($file, true, false); |
||
| 78 | if (!Any::isArray($lines) && count($lines) < 1) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | // list i18n file translation lines and search for entries |
||
| 82 | $filter = array_filter($lines, function($target, $en) use ($usedWords, $onlyKeys) { |
||
| 83 | $enWords = explode(' ', $en); |
||
| 84 | foreach ($enWords as $enWord) { |
||
| 85 | if(Arr::in($enWord, $usedWords)) { |
||
| 86 | return true; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | if (!$onlyKeys) { |
||
| 91 | $targetWords = explode(' ', $target); |
||
| 92 | foreach ($targetWords as $targetWord) { |
||
| 93 | if (Arr::in($targetWord, $usedWords)) { |
||
| 94 | return true; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | return false; |
||
| 100 | }, ARRAY_FILTER_USE_BOTH); |
||
| 101 | |||
| 102 | if (!$filter || count($filter) < 1) { |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | $relevance = 10 * (count($filter) / count($files)); |
||
| 107 | // initialize abstract response pattern |
||
| 108 | $res = new AbstractSearchItem(); |
||
| 109 | $res->setTitle(ucfirst($route)); |
||
| 110 | $res->setSnippet(implode('; ', $filter)); |
||
| 111 | $res->setDate(time()); |
||
| 112 | $res->setRelevance($relevance); |
||
| 113 | $res->setUrl($route . '/index'); |
||
| 114 | $res->setMarker('i18n'); |
||
| 115 | |||
| 116 | $model->add($res); |
||
| 117 | } |
||
| 120 | } |