Conditions | 7 |
Paths | 28 |
Total Lines | 75 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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); |
||
17 | public function createIndex(string $indexName): void |
||
18 | { |
||
19 | $indexes = new Indexes(); |
||
20 | |||
21 | $index = $indexes->getIndex($indexName); |
||
22 | |||
23 | $singleton = \singleton($index->getClass()); |
||
24 | |||
25 | // ['ID', 'CreatedAt', 'LastEdited']; |
||
26 | $fields = ['CreatedAt']; |
||
27 | |||
28 | // @todo different field types |
||
29 | foreach ($index->getFields() as $field) { |
||
30 | $fields[] = $field; |
||
31 | } |
||
32 | |||
33 | foreach ($index->getTokens() as $token) { |
||
34 | $fields[] = $token; |
||
35 | } |
||
36 | |||
37 | /** @var \SilverStripe\ORM\DataObjectSchema $schema */ |
||
38 | $schema = $singleton->getSchema(); |
||
39 | $specs = $schema->fieldSpecs($index->getClass(), DataObjectSchema::DB_ONLY); |
||
40 | |||
41 | $columns = []; |
||
42 | foreach ($fields as $field) { |
||
43 | $fieldType = $specs[$field]; |
||
44 | |||
45 | // fix likes of varchar(255) |
||
46 | $fieldType = \explode('(', $fieldType)[0]; |
||
47 | |||
48 | // this will be the most common |
||
49 | $indexType = 'text'; |
||
50 | |||
51 | // @todo configure index to strip HTML |
||
52 | switch ($fieldType) { |
||
53 | case 'Int': |
||
54 | $indexType = 'integer'; |
||
55 | |||
56 | break; |
||
57 | case 'Float': |
||
58 | $indexType = 'float'; |
||
59 | |||
60 | break; |
||
61 | } |
||
62 | |||
63 | $options = []; |
||
64 | if ($indexType === 'text') { |
||
65 | $options = ['indexed', 'stored']; |
||
66 | } |
||
67 | $columns[$field] = ['type' => $indexType, 'options' => $options]; |
||
68 | } |
||
69 | |||
70 | |||
71 | |||
72 | $client = new Client(); |
||
73 | $manticoreClient = $client->getConnection(); |
||
74 | |||
75 | |||
76 | $settings = [ |
||
77 | 'rt_mem_limit' => '256M', |
||
78 | 'dict' => 'keywords', |
||
79 | 'min_infix_len' => 2, |
||
80 | 'html_strip' => 1, |
||
81 | ]; |
||
82 | |||
83 | \error_log('INDEX CREATION PAYLOAD: $columns'); |
||
84 | \error_log(\print_r($columns, 1)); |
||
85 | |||
86 | $manticoreIndex = new \Manticoresearch\Index($manticoreClient, $indexName); |
||
87 | |||
88 | $manticoreIndex->create( |
||
89 | $columns, |
||
90 | $settings, |
||
91 | true, |
||
92 | ); |
||
95 |