Conditions | 17 |
Paths | 513 |
Total Lines | 74 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 declare(strict_types = 1); |
||
38 | public function getIndexes(): array |
||
39 | { |
||
40 | $indexesConfig = Config::inst()->get('Suilven\FreeTextSearch\Indexes', 'indexes') ; |
||
41 | |||
42 | // reset |
||
43 | $this->indexesByName = []; |
||
44 | |||
45 | foreach ($indexesConfig as $indexConfig) { |
||
46 | $name = $indexConfig['index']['name']; |
||
47 | $indexAlreadyExists = isset($this->indexesByName[$name]); |
||
48 | |||
49 | // get the existing index, to tweak, or create a new one |
||
50 | $index = $indexAlreadyExists |
||
51 | ? $this->indexesByName[$name] |
||
52 | : new Index(); |
||
53 | $index->setName($name); |
||
54 | |||
55 | if (isset($indexConfig['index']['class'])) { |
||
56 | $index->setClass($indexConfig['index']['class']); |
||
57 | } |
||
58 | |||
59 | if (isset($indexConfig['index']['fields'])) { |
||
60 | foreach ($indexConfig['index']['fields'] as $fieldname) { |
||
61 | $index->addField($fieldname); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | if (isset($indexConfig['index']['tokens'])) { |
||
66 | foreach ($indexConfig['index']['tokens'] as $token) { |
||
67 | $index->addToken($token); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | // has one fields |
||
72 | if (isset($indexConfig['index']['has_one'])) { |
||
73 | foreach ($indexConfig['index']['has_one'] as $hasOneField) { |
||
74 | $index->addHasOneField($hasOneField); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | // has many fields |
||
79 | // NB many many may need to be treated as bipartisan has many |
||
80 | if (isset($indexConfig['index']['has_many'])) { |
||
81 | foreach ($indexConfig['index']['has_many'] as $hasManyField) { |
||
82 | $index->addHasManyField($hasManyField['name'], [ |
||
83 | 'relationship' => $hasManyField['relationship'], |
||
84 | 'field' => $hasManyField['field'], |
||
85 | ]); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | // fields that will be used for highlighting |
||
90 | if (isset($indexConfig['index']['highlighted_fields'])) { |
||
91 | foreach ($indexConfig['index']['highlighted_fields'] as $highlightedField) { |
||
92 | $index->addHighlightedField($highlightedField); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | // fields that will be used for storage, but not indexed |
||
97 | if (isset($indexConfig['index']['stored_fields'])) { |
||
98 | foreach ($indexConfig['index']['stored_fields'] as $storedField) { |
||
99 | $index->addStoredField($storedField); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // tokenizer |
||
104 | if (isset($indexConfig['index']['tokenizer'])) { |
||
105 | $index->setTokenizer($indexConfig['index']['tokenizer']); |
||
106 | } |
||
107 | |||
108 | $this->indexesByName[$index->getName()] = $index; |
||
109 | } |
||
110 | |||
111 | return $this->indexesByName; |
||
112 | } |
||
185 |