Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
101 | class CounterCacheBehavior extends Behavior |
||
102 | { |
||
103 | /** |
||
104 | * Store the fields which should be ignored |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $_ignoreDirty = []; |
||
109 | |||
110 | /** |
||
111 | * beforeSave callback. |
||
112 | * |
||
113 | * Check if a field, which should be ignored, is dirty |
||
114 | * |
||
115 | * @param \Cake\Event\Event $event The beforeSave event that was fired |
||
116 | * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved |
||
117 | * @param \ArrayObject $options The options for the query |
||
118 | * @return void |
||
119 | */ |
||
120 | public function beforeSave(Event $event, EntityInterface $entity, $options) |
||
121 | { |
||
122 | if (isset($options['ignoreCounterCache']) && $options['ignoreCounterCache'] === true) { |
||
123 | return; |
||
124 | } |
||
125 | |||
126 | foreach ($this->_config as $assoc => $settings) { |
||
127 | $assoc = $this->_table->getAssociation($assoc); |
||
128 | foreach ($settings as $field => $config) { |
||
129 | if (is_int($field)) { |
||
130 | continue; |
||
131 | } |
||
132 | |||
133 | $registryAlias = $assoc->getTarget()->getRegistryAlias(); |
||
134 | $entityAlias = $assoc->getProperty(); |
||
135 | |||
136 | if ( |
||
137 | !is_callable($config) && |
||
138 | isset($config['ignoreDirty']) && |
||
139 | $config['ignoreDirty'] === true && |
||
140 | $entity->$entityAlias->isDirty($field) |
||
141 | ) { |
||
142 | $this->_ignoreDirty[$registryAlias][$field] = true; |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * afterSave callback. |
||
150 | * |
||
151 | * Makes sure to update counter cache when a new record is created or updated. |
||
152 | * |
||
153 | * @param \Cake\Event\Event $event The afterSave event that was fired. |
||
154 | * @param \Cake\Datasource\EntityInterface $entity The entity that was saved. |
||
155 | * @param \ArrayObject $options The options for the query |
||
156 | * @return void |
||
157 | */ |
||
158 | View Code Duplication | public function afterSave(Event $event, EntityInterface $entity, $options) |
|
167 | |||
168 | /** |
||
169 | * afterDelete callback. |
||
170 | * |
||
171 | * Makes sure to update counter cache when a record is deleted. |
||
172 | * |
||
173 | * @param \Cake\Event\Event $event The afterDelete event that was fired. |
||
174 | * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted. |
||
175 | * @param \ArrayObject $options The options for the query |
||
176 | * @return void |
||
177 | */ |
||
178 | View Code Duplication | public function afterDelete(Event $event, EntityInterface $entity, $options) |
|
186 | |||
187 | /** |
||
188 | * Iterate all associations and update counter caches. |
||
189 | * |
||
190 | * @param \Cake\Event\Event $event Event instance. |
||
191 | * @param \Cake\Datasource\EntityInterface $entity Entity. |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function _processAssociations(Event $event, EntityInterface $entity) |
||
201 | |||
202 | /** |
||
203 | * Updates counter cache for a single association |
||
204 | * |
||
205 | * @param \Cake\Event\Event $event Event instance. |
||
206 | * @param \Cake\Datasource\EntityInterface $entity Entity |
||
207 | * @param \Cake\ORM\Association $assoc The association object |
||
208 | * @param array $settings The settings for for counter cache for this association |
||
209 | * @return void |
||
210 | * @throws \RuntimeException If invalid callable is passed. |
||
211 | */ |
||
212 | protected function _processAssociation(Event $event, EntityInterface $entity, Association $assoc, array $settings) |
||
264 | |||
265 | /** |
||
266 | * Fetches and returns the count for a single field in an association |
||
267 | * |
||
268 | * @param array $config The counter cache configuration for a single field |
||
269 | * @param array $conditions Additional conditions given to the query |
||
270 | * @return int The number of relations matching the given config and conditions |
||
271 | */ |
||
272 | protected function _getCount(array $config, array $conditions) |
||
288 | } |
||
289 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.