Complex classes like SMWDataValue 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SMWDataValue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | abstract class SMWDataValue { |
||
|
|||
48 | |||
49 | /** |
||
50 | * Associated data item. This is the reference to the immutable object |
||
51 | * that represents the current data content. All other data stored here |
||
52 | * is only about presentation and parsing, but is not relevant to the |
||
53 | * actual data that is represented (and stored later on). |
||
54 | * |
||
55 | * This variable must always be set to some data item, even if there |
||
56 | * have been errors in initialising the data. |
||
57 | * @var SMWDataItem |
||
58 | */ |
||
59 | protected $m_dataitem; |
||
60 | |||
61 | /** |
||
62 | * The property for which this value is constructed or null if none |
||
63 | * given. Property pages are used to make settings that affect parsing |
||
64 | * and display, hence it is sometimes needed to know them. |
||
65 | * |
||
66 | * @var SMWDIProperty |
||
67 | */ |
||
68 | protected $m_property = null; |
||
69 | |||
70 | /** |
||
71 | * Wiki page in the context of which the value is to be interpreted, or |
||
72 | * null if not given (or not on a page). This information is used to |
||
73 | * parse user values such as "#subsection" which only make sense when |
||
74 | * used on a certain page. |
||
75 | * |
||
76 | * @var SMWDIWikiPage |
||
77 | */ |
||
78 | protected $m_contextPage = null; |
||
79 | |||
80 | /** |
||
81 | * The text label to be used for output or false if none given. |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $m_caption; |
||
85 | |||
86 | /** |
||
87 | * The type id for this value object. |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $m_typeid; |
||
91 | |||
92 | /** |
||
93 | * Array of SMWInfolink objects. |
||
94 | * @var array |
||
95 | */ |
||
96 | protected $m_infolinks = array(); |
||
97 | |||
98 | /** |
||
99 | * Output formatting string, false when not set. |
||
100 | * @see setOutputFormat() |
||
101 | * @var mixed |
||
102 | */ |
||
103 | protected $m_outformat = false; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | private $languageCode = ''; |
||
109 | |||
110 | /** |
||
111 | * Used to control the addition of the standard search link. |
||
112 | * @var boolean |
||
113 | */ |
||
114 | private $mHasSearchLink; |
||
115 | |||
116 | /** |
||
117 | * Used to control service link creation. |
||
118 | * @var boolean |
||
119 | */ |
||
120 | private $mHasServiceLinks; |
||
121 | |||
122 | /** |
||
123 | * Array of error text messages. Private to allow us to track error insertion |
||
124 | * (PHP's count() is too slow when called often) by using $mHasErrors. |
||
125 | * @var array |
||
126 | */ |
||
127 | private $mErrors = array(); |
||
128 | |||
129 | /** |
||
130 | * Boolean indicating if there where any errors. |
||
131 | * Should be modified accordingly when modifying $mErrors. |
||
132 | * @var boolean |
||
133 | */ |
||
134 | private $mHasErrors = false; |
||
135 | |||
136 | /** |
||
137 | * @var boolean |
||
138 | */ |
||
139 | private $serviceLinksRenderState = true; |
||
140 | |||
141 | /** |
||
142 | * Extraneous services and object container |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | private $extraneousFunctions = array(); |
||
147 | |||
148 | /** |
||
149 | * @var Options |
||
150 | */ |
||
151 | private $options; |
||
152 | |||
153 | /** |
||
154 | * Indicates whether a value is being used by a query condition or not which |
||
155 | * can lead to a modified validation of a value. |
||
156 | * |
||
157 | * @var boolean |
||
158 | */ |
||
159 | protected $isUsedByQueryCondition = false; |
||
160 | |||
161 | /** |
||
162 | * @var boolean |
||
163 | */ |
||
164 | protected $approximateValue = false; |
||
165 | |||
166 | /** |
||
167 | * @var ValueConstraintValidator |
||
168 | */ |
||
169 | private $valueConstraintValidator = null; |
||
170 | |||
171 | /** |
||
172 | * Constructor. |
||
173 | * |
||
174 | * @param string $typeid |
||
175 | */ |
||
176 | 186 | public function __construct( $typeid ) { |
|
179 | |||
180 | ///// Set methods ///// |
||
181 | |||
182 | /** |
||
183 | * Set the user value (and compute other representations if possible). |
||
184 | * The given value is a string as supplied by some user. An alternative |
||
185 | * label for printout might also be specified. |
||
186 | * |
||
187 | * The third argument was added in SMW 1.9 and should not be used from outside SMW. |
||
188 | * |
||
189 | * @param string $value |
||
190 | * @param mixed $caption |
||
191 | * @param boolean $approximateValue |
||
192 | */ |
||
193 | 171 | public function setUserValue( $value, $caption = false, $approximateValue = false ) { |
|
223 | |||
224 | /** |
||
225 | * Set the actual data contained in this object. The method returns |
||
226 | * true if this was successful (requiring the type of the dataitem |
||
227 | * to match the data value). If false is returned, the data value is |
||
228 | * left unchanged (the data item was rejected). |
||
229 | * |
||
230 | * @note Even if this function returns true, the data value object |
||
231 | * might become invalid if the content of the data item caused errors |
||
232 | * in spite of it being of the right basic type. False is only returned |
||
233 | * if the data item is fundamentally incompatible with the data value. |
||
234 | * |
||
235 | * @param $dataitem SMWDataItem |
||
236 | * @return boolean |
||
237 | */ |
||
238 | 127 | public function setDataItem( SMWDataItem $dataItem ) { |
|
244 | |||
245 | /** |
||
246 | * @since 2.3 |
||
247 | * |
||
248 | * @param boolean $usedByQueryCondition |
||
249 | */ |
||
250 | 66 | public function setQueryConditionUsage( $usedByQueryCondition ) { |
|
253 | |||
254 | /** |
||
255 | * Specify the property to which this value refers. Property pages are |
||
256 | * used to make settings that affect parsing and display, hence it is |
||
257 | * sometimes needed to know them. |
||
258 | * |
||
259 | * @since 1.6 |
||
260 | * |
||
261 | * @param SMWDIProperty $property |
||
262 | */ |
||
263 | 168 | public function setProperty( SMWDIProperty $property ) { |
|
266 | |||
267 | /** |
||
268 | * Returns the property to which this value refers. |
||
269 | * |
||
270 | * @since 1.8 |
||
271 | * |
||
272 | * @return SMWDIProperty|null |
||
273 | */ |
||
274 | 180 | public function getProperty() { |
|
277 | |||
278 | /** |
||
279 | * @since 2.4 |
||
280 | * |
||
281 | * @param string $languageCode |
||
282 | */ |
||
283 | 65 | public function setLanguageCode( $languageCode ) { |
|
286 | |||
287 | /** |
||
288 | * @since 2.4 |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | 13 | public function getLanguageCode() { |
|
295 | |||
296 | /** |
||
297 | * Specify the wiki page to which this value refers. This information is |
||
298 | * used to parse user values such as "#subsection" which only make sense |
||
299 | * when used on a certain page. |
||
300 | * |
||
301 | * @since 1.7 |
||
302 | * |
||
303 | * @param SMWDIWikiPage $contextPage |
||
304 | */ |
||
305 | 146 | public function setContextPage( SMWDIWikiPage $contextPage ) { |
|
308 | |||
309 | /** |
||
310 | * @since 2.4 |
||
311 | * |
||
312 | * @return DIWikiPage|null |
||
313 | */ |
||
314 | 170 | public function getContextPage() { |
|
317 | |||
318 | /** |
||
319 | * @since 2.4 |
||
320 | * |
||
321 | * @return Options $options |
||
322 | */ |
||
323 | 183 | public function setOptions( Options $options ) { |
|
326 | |||
327 | /** |
||
328 | * @since 2.4 |
||
329 | * |
||
330 | * @param string $key |
||
331 | * |
||
332 | * @return mixed|false |
||
333 | */ |
||
334 | 167 | public function getOptionValueFor( $key ) { |
|
342 | |||
343 | /** |
||
344 | * @since 2.4 |
||
345 | * |
||
346 | * @param integer $feature |
||
347 | * |
||
348 | * @return boolean |
||
349 | */ |
||
350 | 157 | public function isEnabledFeature( $feature ) { |
|
353 | |||
354 | /** |
||
355 | * Change the caption (the text used for displaying this datavalue). The given |
||
356 | * value must be a string. |
||
357 | * |
||
358 | * @param string $caption |
||
359 | */ |
||
360 | 85 | public function setCaption( $caption ) { |
|
363 | |||
364 | /** |
||
365 | * @since 2.4 |
||
366 | * |
||
367 | * @param string $caption |
||
368 | */ |
||
369 | 70 | public function getCaption() { |
|
372 | |||
373 | /** |
||
374 | * Adds a single SMWInfolink object to the m_infolinks array. |
||
375 | * |
||
376 | * @param SMWInfolink $link |
||
377 | */ |
||
378 | public function addInfolink( SMWInfolink $link ) { |
||
381 | |||
382 | /** |
||
383 | * Servicelinks are special kinds of infolinks that are created from |
||
384 | * current parameters and in-wiki specification of URL templates. This |
||
385 | * method adds the current property's servicelinks found in the |
||
386 | * messages. The number and content of the parameters is depending on |
||
387 | * the datatype, and the service link message is usually crafted with a |
||
388 | * particular datatype in mind. |
||
389 | */ |
||
390 | public function addServiceLinks() { |
||
431 | |||
432 | /** |
||
433 | * Define a particular output format. Output formats are user-supplied strings |
||
434 | * that the datavalue may (or may not) use to customise its return value. For |
||
435 | * example, quantities with units of measurement may interpret the string as |
||
436 | * a desired output unit. In other cases, the output format might be built-in |
||
437 | * and subject to internationalisation (which the datavalue has to implement). |
||
438 | * In any case, an empty string resets the output format to the default. |
||
439 | * |
||
440 | * There is one predefined output format that all datavalues should respect: the |
||
441 | * format '-' indicates "plain" output that is most useful for further processing |
||
442 | * the value in a template. It should not use any wiki markup or beautification, |
||
443 | * and it should also avoid localization to the current language. When users |
||
444 | * explicitly specify an empty format string in a query, it is normalized to "-" |
||
445 | * to avoid confusion. Note that empty format strings are not interpreted in |
||
446 | * this way when directly passed to this function. |
||
447 | * |
||
448 | * @param string $formatString |
||
449 | */ |
||
450 | 85 | public function setOutputFormat( $formatString ) { |
|
453 | |||
454 | /** |
||
455 | * @since 2.4 |
||
456 | * |
||
457 | * @return string |
||
458 | */ |
||
459 | 13 | public function getOutputFormat() { |
|
462 | |||
463 | /** |
||
464 | * Add a new error string or array of such strings to the error list. |
||
465 | * |
||
466 | * @note Errors should not be escaped here in any way, in contradiction to what |
||
467 | * the docs used to say here in 1.5 and before. Escaping should happen at the output. |
||
468 | * |
||
469 | * @param mixed $error A single string, or array of strings. |
||
470 | */ |
||
471 | 103 | public function addError( $error ) { |
|
480 | |||
481 | /** |
||
482 | * @since 2.4 |
||
483 | * |
||
484 | * @param $parameters |
||
485 | * @param integer|null $type |
||
486 | * @param integer|null $language |
||
487 | */ |
||
488 | 5 | public function addErrorMsg( $parameters, $type = null, $language = null ) { |
|
491 | |||
492 | /** |
||
493 | * @since 2.4 |
||
494 | */ |
||
495 | public function clearErrors() { |
||
499 | |||
500 | ///// Abstract processing methods ///// |
||
501 | |||
502 | /** |
||
503 | * Initialise the datavalue from the given value string. |
||
504 | * The format of this strings might be any acceptable user input |
||
505 | * and especially includes the output of getWikiValue(). |
||
506 | * |
||
507 | * @param string $value |
||
508 | */ |
||
509 | abstract protected function parseUserValue( $value ); |
||
510 | |||
511 | /** |
||
512 | * Set the actual data contained in this object. The method returns |
||
513 | * true if this was successful (requiring the type of the dataitem |
||
514 | * to match the data value). If false is returned, the data value is |
||
515 | * left unchanged (the data item was rejected). |
||
516 | * |
||
517 | * @note Even if this function returns true, the data value object |
||
518 | * might become invalid if the content of the data item caused errors |
||
519 | * in spite of it being of the right basic type. False is only returned |
||
520 | * if the data item is fundamentally incompatible with the data value. |
||
521 | * |
||
522 | * @since 1.6 |
||
523 | * |
||
524 | * @param SMWDataItem $dataItem |
||
525 | * |
||
526 | * @return boolean |
||
527 | */ |
||
528 | abstract protected function loadDataItem( SMWDataItem $dataItem ); |
||
529 | |||
530 | |||
531 | ///// Query support ///// |
||
532 | |||
533 | /** |
||
534 | * @see DataValueDescriptionDeserializer::deserialize |
||
535 | * |
||
536 | * @note Descriptions of values need to know their property to be able to |
||
537 | * create a parsable wikitext version of a query condition again. Thus it |
||
538 | * might be necessary to call setProperty() before using this method. |
||
539 | * |
||
540 | * @param string $value |
||
541 | * |
||
542 | * @return Description |
||
543 | * @throws InvalidArgumentException |
||
544 | */ |
||
545 | 87 | public function getQueryDescription( $value ) { |
|
556 | |||
557 | /** |
||
558 | * Returns a DataValueFormatter that was matched and dispatched for the current |
||
559 | * DV instance. |
||
560 | * |
||
561 | * @since 2.4 |
||
562 | * |
||
563 | * @return DataValueFormatter |
||
564 | */ |
||
565 | 99 | public function getDataValueFormatter() { |
|
568 | |||
569 | /** |
||
570 | * @since 2.4 |
||
571 | * |
||
572 | * @return PropertySpecificationLookup |
||
573 | */ |
||
574 | 158 | public function getPropertySpecificationLookup() { |
|
577 | |||
578 | /** |
||
579 | * @deprecated 2.3 |
||
580 | * @see DescriptionDeserializer::prepareValue |
||
581 | * |
||
582 | * This method should no longer be used for direct public access, instead a |
||
583 | * DataValue is expected to register a DescriptionDeserializer with |
||
584 | * DVDescriptionDeserializerRegistry. |
||
585 | * |
||
586 | * FIXME as of 2.3, SMGeoCoordsValue still uses this method and requires |
||
587 | * migration before 3.0 |
||
588 | */ |
||
589 | static public function prepareValue( &$value, &$comparator ) { |
||
592 | |||
593 | ///// Get methods ///// |
||
594 | |||
595 | /** |
||
596 | * Get the actual data contained in this object or null if the data is |
||
597 | * not defined (due to errors or due to not being set at all). |
||
598 | * @note Most implementations ensure that a data item is always set, |
||
599 | * even if errors occurred, to avoid additional checks for not |
||
600 | * accessing null. Hence, one must not assume that a non-null return |
||
601 | * value here implies that isValid() returns true. |
||
602 | * |
||
603 | * @since 1.6 |
||
604 | * |
||
605 | * @return SMWDataItem|SMWDIError |
||
606 | */ |
||
607 | 174 | public function getDataItem() { |
|
615 | |||
616 | /** |
||
617 | * @since 2.2 |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public function __toString() { |
||
624 | |||
625 | /** |
||
626 | * Returns a short textual representation for this data value. If the value |
||
627 | * was initialised from a user supplied string, then this original string |
||
628 | * should be reflected in this short version (i.e. no normalisation should |
||
629 | * normally happen). There might, however, be additional parts such as code |
||
630 | * for generating tooltips. The output is in wiki text. |
||
631 | * |
||
632 | * The parameter $linked controls linking of values such as titles and should |
||
633 | * be non-NULL and non-false if this is desired. |
||
634 | */ |
||
635 | abstract public function getShortWikiText( $linked = null ); |
||
636 | |||
637 | /** |
||
638 | * Returns a short textual representation for this data value. If the value |
||
639 | * was initialised from a user supplied string, then this original string |
||
640 | * should be reflected in this short version (i.e. no normalisation should |
||
641 | * normally happen). There might, however, be additional parts such as code |
||
642 | * for generating tooltips. The output is in HTML text. |
||
643 | * |
||
644 | * The parameter $linker controls linking of values such as titles and should |
||
645 | * be some Linker object (or NULL for no linking). |
||
646 | */ |
||
647 | abstract public function getShortHTMLText( $linker = null ); |
||
648 | |||
649 | /** |
||
650 | * Return the long textual description of the value, as printed for |
||
651 | * example in the factbox. If errors occurred, return the error message |
||
652 | * The result always is a wiki-source string. |
||
653 | * |
||
654 | * The parameter $linked controls linking of values such as titles and should |
||
655 | * be non-NULL and non-false if this is desired. |
||
656 | */ |
||
657 | abstract public function getLongWikiText( $linked = null ); |
||
658 | |||
659 | /** |
||
660 | * Return the long textual description of the value, as printed for |
||
661 | * example in the factbox. If errors occurred, return the error message |
||
662 | * The result always is an HTML string. |
||
663 | * |
||
664 | * The parameter $linker controls linking of values such as titles and should |
||
665 | * be some Linker object (or NULL for no linking). |
||
666 | */ |
||
667 | abstract public function getLongHTMLText( $linker = null ); |
||
668 | |||
669 | /** |
||
670 | * Returns a short textual representation for this data value. If the value |
||
671 | * was initialised from a user supplied string, then this original string |
||
672 | * should be reflected in this short version (i.e. no normalisation should |
||
673 | * normally happen). There might, however, be additional parts such as code |
||
674 | * for generating tooltips. The output is in the specified format. |
||
675 | * |
||
676 | * The parameter $linker controls linking of values such as titles and should |
||
677 | * be some Linker object (for HTML output), or NULL for no linking. |
||
678 | */ |
||
679 | 43 | public function getShortText( $outputformat, $linker = null ) { |
|
689 | |||
690 | /** |
||
691 | * Return the long textual description of the value, as printed for |
||
692 | * example in the factbox. If errors occurred, return the error message. |
||
693 | * The output is in the specified format. |
||
694 | * |
||
695 | * The parameter $linker controls linking of values such as titles and should |
||
696 | * be some Linker object (for HTML output), or NULL for no linking. |
||
697 | */ |
||
698 | public function getLongText( $outputformat, $linker = null ) { |
||
708 | |||
709 | /** |
||
710 | * Return text serialisation of info links. Ensures more uniform layout |
||
711 | * throughout wiki (Factbox, Property pages, ...). |
||
712 | * |
||
713 | * @param integer $outputformat Element of the SMW_OUTPUT_ enum |
||
714 | * @param $linker |
||
715 | * |
||
716 | * @return string |
||
717 | */ |
||
718 | 1 | public function getInfolinkText( $outputformat, $linker = null ) { |
|
755 | |||
756 | /** |
||
757 | * Return the plain wiki version of the value, or |
||
758 | * FALSE if no such version is available. The returned |
||
759 | * string suffices to reobtain the same DataValue |
||
760 | * when passing it as an input string to setUserValue(). |
||
761 | */ |
||
762 | abstract public function getWikiValue(); |
||
763 | |||
764 | /** |
||
765 | * Return a short string that unambiguously specify the type of this |
||
766 | * value. This value will globally be used to identify the type of a |
||
767 | * value (in spite of the class it actually belongs to, which can still |
||
768 | * implement various types). |
||
769 | */ |
||
770 | 113 | public function getTypeID() { |
|
773 | |||
774 | /** |
||
775 | * @since 2.1 |
||
776 | * @param boolean $renderState |
||
777 | */ |
||
778 | 1 | public function setServiceLinksRenderState( $renderState = true ) { |
|
781 | |||
782 | /** |
||
783 | * Return an array of SMWLink objects that provide additional resources |
||
784 | * for the given value. Captions can contain some HTML markup which is |
||
785 | * admissible for wiki text, but no more. Result might have no entries |
||
786 | * but is always an array. |
||
787 | */ |
||
788 | 1 | public function getInfolinks() { |
|
803 | |||
804 | /** |
||
805 | * Overwritten by callers to supply an array of parameters that can be used for |
||
806 | * creating servicelinks. The number and content of values in the parameter array |
||
807 | * may vary, depending on the concrete datatype. |
||
808 | */ |
||
809 | protected function getServiceLinkParams() { |
||
812 | |||
813 | /** |
||
814 | * Return a string that identifies the value of the object, and that can |
||
815 | * be used to compare different value objects. |
||
816 | * Possibly overwritten by subclasses (e.g. to ensure that returned |
||
817 | * value is normalized first) |
||
818 | * |
||
819 | * @return string |
||
820 | */ |
||
821 | public function getHash() { |
||
824 | |||
825 | /** |
||
826 | * Convenience method that checks if the value that is used to sort |
||
827 | * data of this type is numeric. This only works if the value is set. |
||
828 | * |
||
829 | * @return boolean |
||
830 | */ |
||
831 | public function isNumeric() { |
||
838 | |||
839 | /** |
||
840 | * Return true if a value was defined and understood by the given type, |
||
841 | * and false if parsing errors occurred or no value was given. |
||
842 | * |
||
843 | * @return boolean |
||
844 | */ |
||
845 | 183 | public function isValid() { |
|
848 | |||
849 | /** |
||
850 | * Whether a datavalue can be used or not (can be made more restrictive then |
||
851 | * isValid). |
||
852 | * |
||
853 | * @note Validity defines a processable state without any technical restrictions |
||
854 | * while usability is determined by its accessibility to a context |
||
855 | * (permission, convention etc.) |
||
856 | * |
||
857 | * @since 2.2 |
||
858 | * |
||
859 | * @return boolean |
||
860 | */ |
||
861 | 146 | public function canUse() { |
|
864 | |||
865 | /** |
||
866 | * @note Normally set by the DataValueFactory, or during tests |
||
867 | * |
||
868 | * @since 2.3 |
||
869 | * |
||
870 | * @param array |
||
871 | */ |
||
872 | 183 | public function setExtraneousFunctions( array $extraneousFunctions ) { |
|
875 | |||
876 | /** |
||
877 | * @since 2.3 |
||
878 | * |
||
879 | * @param string $name |
||
880 | * @param array $parameters |
||
881 | * |
||
882 | * @return mixed |
||
883 | * @throws RuntimeException |
||
884 | */ |
||
885 | public function getExtraneousFunctionFor( $name, array $parameters = array() ) { |
||
893 | |||
894 | /** |
||
895 | * Return a string that displays all error messages as a tooltip, or |
||
896 | * an empty string if no errors happened. |
||
897 | * |
||
898 | * @return string |
||
899 | */ |
||
900 | 10 | public function getErrorText() { |
|
903 | |||
904 | /** |
||
905 | * Return an array of error messages, or an empty array |
||
906 | * if no errors occurred. |
||
907 | * |
||
908 | * @return array |
||
909 | */ |
||
910 | 143 | public function getErrors() { |
|
913 | |||
914 | /** |
||
915 | * Check if property is range restricted and, if so, whether the current value is allowed. |
||
916 | * Creates an error if the value is illegal. |
||
917 | */ |
||
918 | 170 | protected function checkAllowedValues() { |
|
921 | |||
922 | /** |
||
923 | * @since 2.4 |
||
924 | * |
||
925 | * @param string $value |
||
926 | * |
||
927 | * @return string |
||
928 | */ |
||
929 | 32 | protected function convertDoubleWidth( $value ) { |
|
932 | |||
933 | } |
||
934 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.