Complex classes like ChangeNotificationFilter 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 ChangeNotificationFilter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ChangeNotificationFilter { |
||
22 | |||
23 | const VALUE_CHANGE = 'smw-value-change'; |
||
24 | const SPECIFICATION_CHANGE = 'smw-specification-change'; |
||
25 | |||
26 | /** |
||
27 | * @var DIWikiPage |
||
28 | */ |
||
29 | private $subject; |
||
30 | |||
31 | /** |
||
32 | * @var Store |
||
33 | */ |
||
34 | private $store; |
||
35 | |||
36 | /** |
||
37 | * @var DataValueFactory |
||
38 | */ |
||
39 | private $dataValueFactory; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $detectedProperties = array(); |
||
45 | |||
46 | /** |
||
47 | * In case detection matrix was stored as subobject on a property then match |
||
48 | * its pairs of property => subobjectKey so that later the Locator can find |
||
49 | * out which subobject contains the group that needs to be notified. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $subSemanticDataMatch = array(); |
||
54 | |||
55 | /** |
||
56 | * @var string|null |
||
57 | */ |
||
58 | private $type = null; |
||
59 | |||
60 | /** |
||
61 | * @var User|null |
||
62 | */ |
||
63 | private $agent = null; |
||
64 | |||
65 | /** |
||
66 | * @var boolean |
||
67 | */ |
||
68 | private $canNotify = false; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $propertyExemptionList = array(); |
||
74 | |||
75 | /** |
||
76 | * @var boolean |
||
77 | */ |
||
78 | private $isCommandLineMode = false; |
||
79 | |||
80 | /** |
||
81 | * @since 1.0 |
||
82 | * |
||
83 | * @param DIWikiPage $subject |
||
84 | * @param Store $store |
||
85 | */ |
||
86 | 8 | public function __construct( DIWikiPage $subject, Store $store ) { |
|
91 | |||
92 | /** |
||
93 | * @since 1.0 |
||
94 | * |
||
95 | * @param User $agent |
||
96 | */ |
||
97 | 4 | public function setAgent( User $agent ) { |
|
100 | |||
101 | /** |
||
102 | * @since 1.0 |
||
103 | * |
||
104 | * @param array $propertyExemptionList |
||
105 | */ |
||
106 | 1 | public function setPropertyExemptionList( array $propertyExemptionList ) { |
|
107 | 1 | $this->propertyExemptionList = array_flip( |
|
108 | 1 | str_replace( ' ', '_', $propertyExemptionList ) |
|
109 | ); |
||
110 | 1 | } |
|
111 | |||
112 | /** |
||
113 | * @since 1.0 |
||
114 | * |
||
115 | * @param boolean $isCommandLineMode |
||
116 | */ |
||
117 | public function isCommandLineMode( $isCommandLineMode ) { |
||
120 | |||
121 | /** |
||
122 | * @since 1.0 |
||
123 | * |
||
124 | * @param CompositePropertyTableDiffIterator $compositePropertyTableDiffIterator |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | public function findChangeEvent( CompositePropertyTableDiffIterator $compositePropertyTableDiffIterator ) { |
||
137 | |||
138 | /** |
||
139 | * @see EchoEvent::create |
||
140 | * |
||
141 | * @since 1.0 |
||
142 | * |
||
143 | * @param boolean|null $hasChangeToNotifyAbout |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 2 | public function getEventRecord( $hasChangeToNotifyAbout = false ) { |
|
148 | |||
149 | 2 | if ( $this->subject === null || $this->type === null || !$hasChangeToNotifyAbout ) { |
|
|
|||
150 | 1 | wfDebugLog( 'smw', 'EchoEvent was not triggered' ); |
|
151 | 1 | return array(); |
|
152 | } |
||
153 | |||
154 | return array( |
||
155 | 1 | 'agent' => $this->agent, |
|
156 | 'extra' => array( |
||
157 | 'notifyAgent' => false, |
||
158 | 1 | 'revid' => $this->subject->getTitle()->getLatestRevID(), |
|
159 | 1 | 'properties' => $this->detectedProperties, |
|
160 | 1 | 'subSemanticDataMatch' => $this->subSemanticDataMatch, |
|
161 | 1 | 'subject' => $this->subject |
|
162 | ), |
||
163 | 1 | 'title' => $this->subject->getTitle(), |
|
164 | 1 | 'type' => $this->type |
|
165 | ); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @since 1.0 |
||
170 | * |
||
171 | * @param CompositePropertyTableDiffIterator $compositePropertyTableDiffIterator |
||
172 | * |
||
173 | * @return boolean|null |
||
174 | */ |
||
175 | 6 | public function hasChangeToNotifyAbout( CompositePropertyTableDiffIterator $compositePropertyTableDiffIterator ) { |
|
176 | |||
177 | 6 | $start = microtime( true ); |
|
178 | 6 | $this->type = self::VALUE_CHANGE; |
|
179 | |||
180 | 6 | $property = new DIProperty( |
|
181 | 6 | PropertyRegistry::NOTIFICATIONS_ON |
|
182 | ); |
||
183 | |||
184 | if ( |
||
185 | 6 | $this->subject->getNamespace() === SMW_NS_PROPERTY || |
|
186 | 5 | $this->subject->getNamespace() === NS_CATEGORY || |
|
187 | 6 | $this->subject->getNamespace() === SMW_NS_CONCEPT ) { |
|
188 | 1 | $this->type = self::SPECIFICATION_CHANGE; |
|
189 | } |
||
190 | |||
191 | 6 | foreach ( $compositePropertyTableDiffIterator->getTableChangeOps() as $tableChangeOp ) { |
|
192 | |||
193 | 6 | if ( isset( $this->propertyExemptionList[$this->getFixedPropertyValueBy( $tableChangeOp, 'key' )] ) ) { |
|
194 | 1 | continue; |
|
195 | } |
||
196 | |||
197 | 5 | $this->doFilterOnFieldChangeOps( |
|
198 | 5 | $property, |
|
199 | 5 | $tableChangeOp, |
|
200 | 5 | $tableChangeOp->getFieldChangeOps( 'insert' ) |
|
201 | ); |
||
202 | |||
203 | 5 | $this->doFilterOnFieldChangeOps( |
|
204 | 5 | $property, |
|
205 | 5 | $tableChangeOp, |
|
206 | 5 | $tableChangeOp->getFieldChangeOps( 'delete' ) |
|
207 | ); |
||
208 | } |
||
209 | |||
210 | 6 | wfDebugLog( 'smw', __METHOD__ . ' ' . $this->subject->getHash() . ' in procTime (sec): ' . round( ( microtime( true ) - $start ), 7 ) ); |
|
211 | |||
212 | 6 | return $this->canNotify; |
|
213 | } |
||
214 | |||
215 | // 2.4 compat |
||
216 | 6 | private function getFixedPropertyValueBy( $tableChangeOp, $key ) { |
|
219 | |||
220 | // 2.4 compat |
||
221 | 5 | private function getDataItemById( $id ) { |
|
224 | |||
225 | 5 | private function doFilterOnFieldChangeOps( $property, $tableChangeOp, $fieldChangeOps ) { |
|
226 | |||
227 | 5 | foreach ( $fieldChangeOps as $fieldChangeOp ) { |
|
263 | |||
264 | 4 | private function doCompareNotificationsOnValuesWithOps( $property, $dataItem, $fieldChangeOp ) { |
|
300 | |||
301 | 4 | private function doCompareOnPropertyValues( $dataItem, $propertyValues, $fieldChangeOp, $subobjectName = null ) { |
|
306 | |||
307 | 4 | private function doCompareFields( $value, $fieldChangeOp, $dataItem, $subobjectName ) { |
|
360 | |||
361 | } |
||
362 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.