Conditions | 13 |
Paths | 127 |
Total Lines | 71 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
63 | public static function handle_manipulation($manipulation) |
||
64 | { |
||
65 | if (!static::config()->get('enabled')) { |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | // First, extract any state that is in the manipulation itself |
||
70 | foreach ($manipulation as $table => $details) { |
||
71 | if (!isset($manipulation[$table]['class'])) { |
||
72 | $manipulation[$table]['class'] = DataObject::getSchema()->tableClass($table); |
||
73 | } |
||
74 | $manipulation[$table]['state'] = array(); |
||
75 | } |
||
76 | |||
77 | SearchVariant::call('extractManipulationState', $manipulation); |
||
78 | |||
79 | // Then combine the manipulation back into object field sets |
||
80 | |||
81 | $writes = array(); |
||
82 | |||
83 | foreach ($manipulation as $table => $details) { |
||
84 | if (!isset($details['id'])) { |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | $id = $details['id']; |
||
89 | $state = $details['state']; |
||
90 | $class = $details['class']; |
||
91 | $command = $details['command']; |
||
92 | $fields = isset($details['fields']) ? $details['fields'] : array(); |
||
93 | |||
94 | $base = DataObject::getSchema()->baseDataClass($class); |
||
95 | $key = "$id:$base:" . serialize($state); |
||
96 | |||
97 | $statefulids = array(array('id' => $id, 'state' => $state)); |
||
98 | |||
99 | // Is this the first table for this particular object? Then add an item to $writes |
||
100 | if (!isset($writes[$key])) { |
||
101 | $writes[$key] = array( |
||
102 | 'base' => $base, |
||
103 | 'class' => $class, |
||
104 | 'id' => $id, |
||
105 | 'statefulids' => $statefulids, |
||
106 | 'command' => $command, |
||
107 | 'fields' => array() |
||
108 | ); |
||
109 | } elseif (is_subclass_of($class, $writes[$key]['class'])) { |
||
110 | // Otherwise update the class label if it's more specific than the currently recorded one |
||
111 | $writes[$key]['class'] = $class; |
||
112 | } |
||
113 | |||
114 | // Update the fields |
||
115 | foreach ($fields as $field => $value) { |
||
116 | $writes[$key]['fields']["$class:$field"] = $value; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // Trim non-delete records without fields |
||
121 | foreach (array_keys($writes) as $key) { |
||
122 | if ($writes[$key]['command'] !== 'delete' && empty($writes[$key]['fields'])) { |
||
123 | unset($writes[$key]); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | // Then extract any state that is needed for the writes |
||
128 | |||
129 | SearchVariant::call('extractManipulationWriteState', $writes); |
||
130 | |||
131 | // Submit all of these writes to the search processor |
||
132 | |||
133 | static::process_writes($writes); |
||
134 | } |
||
193 |