Conditions | 17 |
Paths | 513 |
Total Lines | 88 |
Code Lines | 44 |
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 | $singleton = \singleton($index->getClass()); |
||
73 | |||
74 | if (isset($indexConfig['index']['has_one'])) { |
||
75 | foreach ($indexConfig['index']['has_one'] as $hasOneField) { |
||
76 | $relationship = $hasOneField['relationship']; |
||
77 | // @phpstan-ignore-next-line |
||
78 | $singletonOfRel = \call_user_func(array($singleton, $relationship)); |
||
79 | |||
80 | $index->addHasOneField($hasOneField['name'], [ |
||
81 | 'relationship' => $relationship, |
||
82 | 'field' => $hasOneField['field'], |
||
83 | 'class' => $singletonOfRel->ClassName, |
||
84 | ]); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | // has many fields |
||
89 | // NB many many may need to be treated as bipartisan has many |
||
90 | if (isset($indexConfig['index']['has_many'])) { |
||
91 | foreach ($indexConfig['index']['has_many'] as $hasOneField) { |
||
92 | $relationship = $hasOneField['relationship']; |
||
93 | // @phpstan-ignore-next-line |
||
94 | $singletonOfRel = \call_user_func(array($singleton, $relationship)); |
||
95 | $index->addHasManyField($hasOneField['name'], [ |
||
96 | 'relationship' => $relationship, |
||
97 | 'field' => $hasOneField['field'], |
||
98 | 'class' => $singletonOfRel->dataClass(), |
||
99 | ]); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // fields that will be used for highlighting |
||
104 | if (isset($indexConfig['index']['highlighted_fields'])) { |
||
105 | foreach ($indexConfig['index']['highlighted_fields'] as $highlightedField) { |
||
106 | $index->addHighlightedField($highlightedField); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // fields that will be used for storage, but not indexed |
||
111 | if (isset($indexConfig['index']['stored_fields'])) { |
||
112 | foreach ($indexConfig['index']['stored_fields'] as $storedField) { |
||
113 | $index->addStoredField($storedField); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // tokenizer |
||
118 | if (isset($indexConfig['index']['tokenizer'])) { |
||
119 | $index->setTokenizer($indexConfig['index']['tokenizer']); |
||
120 | } |
||
121 | |||
122 | $this->indexesByName[$index->getName()] = $index; |
||
123 | } |
||
124 | |||
125 | return $this->indexesByName; |
||
126 | } |
||
206 |