Conditions | 9 |
Paths | 10 |
Total Lines | 68 |
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 |
||
92 | private function loadDatabase($databaseFile) |
||
93 | { |
||
94 | // add gettext domain |
||
95 | bindtextdomain( |
||
96 | $this->getISONumber(), |
||
97 | $this->getLocalMessagesPath() |
||
98 | ); |
||
99 | |||
100 | bind_textdomain_codeset( |
||
101 | $this->getISONumber(), |
||
102 | 'UTF-8' |
||
103 | ); |
||
104 | |||
105 | // load database from json file |
||
106 | $json = json_decode( |
||
107 | file_get_contents($databaseFile), |
||
108 | true |
||
109 | ); |
||
110 | |||
111 | // build index from database |
||
112 | $entryList = $json[$this->getISONumber()]; |
||
113 | |||
114 | // index database |
||
115 | $indexedFields = $this->getIndexDefinition(); |
||
116 | |||
117 | if (empty($indexedFields)) { |
||
118 | $this->clusterIndex = $entryList; |
||
119 | } else { |
||
120 | // init all defined indexes |
||
121 | foreach ($entryList as &$entry) { |
||
122 | foreach ($indexedFields as $indexName => $indexDefinition) { |
||
123 | if (is_array($indexDefinition)) { |
||
124 | $reference = &$this->index[$indexName]; |
||
125 | // compound index |
||
126 | foreach ($indexDefinition as $indexDefinitionPart) { |
||
127 | // limited length of field |
||
128 | if (is_array($indexDefinitionPart)) { |
||
129 | $indexDefinitionPartValue = substr( |
||
130 | $entry[$indexDefinitionPart[0]], |
||
131 | 0, |
||
132 | $indexDefinitionPart[1] |
||
133 | ); |
||
134 | } else { |
||
135 | $indexDefinitionPartValue = $entry[$indexDefinitionPart]; |
||
136 | } |
||
137 | if (!isset($reference[$indexDefinitionPartValue])) { |
||
138 | $reference[$indexDefinitionPartValue] = []; |
||
139 | } |
||
140 | $reference = &$reference[$indexDefinitionPartValue]; |
||
141 | } |
||
142 | |||
143 | $reference = $this->arrayToEntry($entry); |
||
144 | } else { |
||
145 | // single index |
||
146 | $indexName = $indexDefinition; |
||
147 | // skip empty field |
||
148 | if (empty($entry[$indexDefinition])) { |
||
149 | continue; |
||
150 | } |
||
151 | // add to index |
||
152 | $this->index[$indexName][$entry[$indexDefinition]] = $this->arrayToEntry($entry); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | // set cluster index as first index |
||
158 | $clusterIndexName = key($this->index); |
||
159 | $this->clusterIndex = &$this->index[$clusterIndexName]; |
||
160 | } |
||
230 |