Total Complexity | 110 |
Total Lines | 487 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 1 |
Complex classes like SyncObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SyncObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class SyncObject extends Streamer { |
||
13 | public const STREAMER_CHECKS = 6; |
||
14 | public const STREAMER_CHECK_REQUIRED = 7; |
||
15 | public const STREAMER_CHECK_ZEROORONE = 8; |
||
16 | public const STREAMER_CHECK_NOTALLOWED = 9; |
||
17 | public const STREAMER_CHECK_ONEVALUEOF = 10; |
||
18 | public const STREAMER_CHECK_SETZERO = "setToValue0"; |
||
19 | public const STREAMER_CHECK_SETONE = "setToValue1"; |
||
20 | public const STREAMER_CHECK_SETTWO = "setToValue2"; |
||
21 | public const STREAMER_CHECK_SETEMPTY = "setToValueEmpty"; |
||
22 | public const STREAMER_CHECK_CMPLOWER = 13; |
||
23 | public const STREAMER_CHECK_CMPHIGHER = 14; |
||
24 | public const STREAMER_CHECK_LENGTHMAX = 15; |
||
25 | public const STREAMER_CHECK_EMAIL = 16; |
||
26 | |||
27 | protected $unsetVars; |
||
28 | protected $supportsPrivateStripping; |
||
29 | protected $checkedParameters; |
||
30 | |||
31 | public function __construct($mapping) { |
||
32 | $this->unsetVars = []; |
||
33 | $this->supportsPrivateStripping = false; |
||
34 | $this->checkedParameters = false; |
||
35 | parent::__construct($mapping); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Sets all supported but not transmitted variables |
||
40 | * of this SyncObject to an "empty" value, so they are deleted when being saved. |
||
41 | * |
||
42 | * @param array $supportedFields array with all supported fields, if available |
||
43 | * |
||
44 | * @return bool |
||
45 | */ |
||
46 | public function emptySupported($supportedFields) { |
||
47 | // Some devices do not send supported tag. In such a case remove all not set properties. |
||
48 | if ($supportedFields === false || !is_array($supportedFields) || (empty($supportedFields))) { |
||
|
|||
49 | if (defined('UNSET_UNDEFINED_PROPERTIES') && |
||
50 | UNSET_UNDEFINED_PROPERTIES && |
||
51 | ( |
||
52 | $this instanceof SyncContact || |
||
53 | $this instanceof SyncAppointment || |
||
54 | $this instanceof SyncTask |
||
55 | )) { |
||
56 | SLog::Write(LOGLEVEL_INFO, sprintf("%s->emptySupported(): no supported list available, emptying all not set parameters", get_class($this))); |
||
57 | $supportedFields = array_keys($this->mapping); |
||
58 | } |
||
59 | else { |
||
60 | return false; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | foreach ($supportedFields as $field) { |
||
65 | if (!isset($this->mapping[$field])) { |
||
66 | SLog::Write(LOGLEVEL_WARN, sprintf("Field '%s' is supposed to be emptied but is not defined for '%s'", $field, get_class($this))); |
||
67 | |||
68 | continue; |
||
69 | } |
||
70 | $var = $this->mapping[$field][self::STREAMER_VAR]; |
||
71 | // add var to $this->unsetVars if $var is not set |
||
72 | if (!isset($this->{$var})) { |
||
73 | $this->unsetVars[] = $var; |
||
74 | } |
||
75 | } |
||
76 | SLog::Write(LOGLEVEL_DEBUG, sprintf("Supported variables to be unset: %s", implode(',', $this->unsetVars))); |
||
77 | |||
78 | return true; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Compares this a SyncObject to another. |
||
83 | * In case that all available mapped fields are exactly EQUAL, it returns true. |
||
84 | * |
||
85 | * @see SyncObject |
||
86 | * |
||
87 | * @param SyncObject $odo other SyncObject |
||
88 | * @param bool $log flag to turn on logging |
||
89 | * @param bool $strictTypeCompare to enforce type matching |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function equals($odo, $log = false, $strictTypeCompare = false) { |
||
94 | if ($odo === false) { |
||
95 | return false; |
||
96 | } |
||
97 | |||
98 | // check objecttype |
||
99 | if (!$odo instanceof SyncObject) { |
||
100 | SLog::Write(LOGLEVEL_DEBUG, "SyncObject->equals() the target object is not a SyncObject"); |
||
101 | |||
102 | return false; |
||
103 | } |
||
104 | |||
105 | // check for mapped fields |
||
106 | foreach ($this->mapping as $v) { |
||
107 | $val = $v[self::STREAMER_VAR]; |
||
108 | // array of values? |
||
109 | if (isset($v[self::STREAMER_ARRAY])) { |
||
110 | // if neither array is created then don't fail the comparison |
||
111 | if (!isset($this->{$val}) && !isset($odo->{$val})) { |
||
112 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() array '%s' is NOT SET in either object", $val)); |
||
113 | |||
114 | continue; |
||
115 | } |
||
116 | if (is_array($this->{$val}) && is_array($odo->{$val})) { |
||
117 | // if both arrays exist then seek for differences in the arrays |
||
118 | if (count(array_diff($this->{$val}, $odo->{$val})) + count(array_diff($odo->{$val}, $this->{$val})) > 0) { |
||
119 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() items in array '%s' differ", $val)); |
||
120 | |||
121 | return false; |
||
122 | } |
||
123 | } |
||
124 | else { |
||
125 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() array '%s' is set in one but not the other object", $val)); |
||
126 | |||
127 | return false; |
||
128 | } |
||
129 | } |
||
130 | else { |
||
131 | if (isset($this->{$val}, $odo->{$val})) { |
||
132 | if ($strictTypeCompare) { |
||
133 | if ($this->{$val} !== $odo->{$val}) { |
||
134 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() false on field '%s': '%s' != '%s' using strictTypeCompare", $val, Utils::PrintAsString($this->{$val}), Utils::PrintAsString($odo->{$val}))); |
||
135 | |||
136 | return false; |
||
137 | } |
||
138 | } |
||
139 | else { |
||
140 | if ($this->{$val} != $odo->{$val}) { |
||
141 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() false on field '%s': '%s' != '%s'", $val, Utils::PrintAsString($this->{$val}), Utils::PrintAsString($odo->{$val}))); |
||
142 | |||
143 | return false; |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | elseif (!isset($this->{$val}) && !isset($odo->{$val})) { |
||
148 | continue; |
||
149 | } |
||
150 | else { |
||
151 | SLog::Write(LOGLEVEL_DEBUG, sprintf("SyncObject->equals() false because field '%s' is only defined at one obj: '%s' != '%s'", $val, Utils::PrintAsString(isset($this->{$val})), Utils::PrintAsString(isset($odo->{$val})))); |
||
152 | |||
153 | return false; |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | return true; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * String representation of the object. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function __toString() { |
||
167 | $str = get_class($this) . " (\n"; |
||
168 | |||
169 | $streamerVars = []; |
||
170 | foreach ($this->mapping as $k => $v) { |
||
171 | $streamerVars[$v[self::STREAMER_VAR]] = (isset($v[self::STREAMER_TYPE])) ? $v[self::STREAMER_TYPE] : false; |
||
172 | } |
||
173 | |||
174 | foreach (get_object_vars($this) as $k => $v) { |
||
175 | if ($k == "mapping") { |
||
176 | continue; |
||
177 | } |
||
178 | |||
179 | if (array_key_exists($k, $streamerVars)) { |
||
180 | $strV = "(S) "; |
||
181 | } |
||
182 | else { |
||
183 | $strV = ""; |
||
184 | } |
||
185 | |||
186 | // self::STREAMER_ARRAY ? |
||
187 | if (is_array($v)) { |
||
188 | $str .= "\t" . $strV . $k . "(Array) size: " . count($v) . "\n"; |
||
189 | foreach ($v as $value) { |
||
190 | $str .= "\t\t" . Utils::PrintAsString($value) . "\n"; |
||
191 | } |
||
192 | } |
||
193 | elseif ($v instanceof SyncObject) { |
||
194 | $str .= "\t" . $strV . $k . " => " . str_replace("\n", "\n\t\t\t", $v->__toString()) . "\n"; |
||
195 | } |
||
196 | else { |
||
197 | $str .= "\t" . $strV . $k . " => " . (isset($this->{$k}) ? Utils::PrintAsString($this->{$k}) : "null") . "\n"; |
||
198 | } |
||
199 | } |
||
200 | $str .= ")"; |
||
201 | |||
202 | return $str; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Returns the properties which have to be unset on the server. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getUnsetVars() { |
||
211 | return $this->unsetVars; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Removes not necessary data from the object. |
||
216 | * |
||
217 | * @param mixed $flags |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function StripData($flags = 0) { |
||
222 | if ($flags === 0 && isset($this->unsetVars)) { |
||
223 | unset($this->unsetVars); |
||
224 | } |
||
225 | |||
226 | return parent::StripData($flags); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Indicates if a SyncObject supports the private flag and stripping of private data. |
||
231 | * If an object does not support it, it will not be sent to the client but permanently be excluded from the sync. |
||
232 | * |
||
233 | * @return bool - default false defined in constructor - overwritten by implementation |
||
234 | */ |
||
235 | public function SupportsPrivateStripping() { |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Indicates the amount of parameters that were set before Checks were executed and potentially set other parameters. |
||
241 | * |
||
242 | * @return bool/int - returns false if Check() was not executed |
||
243 | */ |
||
244 | public function getCheckedParameters() { |
||
245 | return $this->checkedParameters; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Method checks if the object has the minimum of required parameters |
||
250 | * and fulfills semantic dependencies. |
||
251 | * |
||
252 | * General checks: |
||
253 | * STREAMER_CHECK_REQUIRED may have as value false (do not fix, ignore object!) or set-to-values: STREAMER_CHECK_SETZERO/ONE/TWO, STREAMER_CHECK_SETEMPTY |
||
254 | * STREAMER_CHECK_ZEROORONE may be 0 or 1, if none of these, set-to-values: STREAMER_CHECK_SETZERO or STREAMER_CHECK_SETONE |
||
255 | * STREAMER_CHECK_NOTALLOWED fails if is set |
||
256 | * STREAMER_CHECK_ONEVALUEOF expects an array with accepted values, fails if value is not in array |
||
257 | * |
||
258 | * Comparison: |
||
259 | * STREAMER_CHECK_CMPLOWER compares if the current parameter is lower as a literal or another parameter of the same object |
||
260 | * STREAMER_CHECK_CMPHIGHER compares if the current parameter is higher as a literal or another parameter of the same object |
||
261 | * |
||
262 | * @param bool $logAsDebug (opt) default is false, so messages are logged in WARN log level |
||
263 | * |
||
264 | * @return bool |
||
265 | */ |
||
266 | public function Check($logAsDebug = false) { |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Returns human friendly property name from its value if a mapping is available. |
||
478 | * |
||
479 | * @param array $v |
||
480 | * @param mixed $val |
||
481 | * |
||
482 | * @return mixed |
||
483 | */ |
||
484 | public function GetNameFromPropertyValue($v, $val) { |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Called after the SyncObject was unserialized. |
||
494 | * |
||
495 | * @return bool |
||
496 | */ |
||
497 | public function postUnserialize() { |
||
499 | } |
||
500 | } |
||
501 |