Complex classes like ProductStreamService 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 ProductStreamService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ProductStreamService |
||
| 16 | { |
||
| 17 | const DYNAMIC_STREAM = 1; |
||
| 18 | const STATIC_STREAM = 2; |
||
| 19 | const STATUS_EXPORT = 'export'; |
||
| 20 | const STATUS_DELETE = 'delete'; |
||
| 21 | const STATUS_ERROR = 'error'; |
||
| 22 | const STATUS_SYNCED = 'synced'; |
||
| 23 | const STATUS_PENDING = 'pending'; |
||
| 24 | const PRODUCT_LIMIT = 100; |
||
| 25 | |||
| 26 | //Available statuses for exported stream |
||
| 27 | const EXPORTED_STATUSES = [ |
||
| 28 | self::STATUS_EXPORT, |
||
| 29 | self::STATUS_SYNCED, |
||
| 30 | self::STATUS_ERROR, |
||
| 31 | self::STATUS_PENDING, |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ProductStreamRepository |
||
| 36 | */ |
||
| 37 | private $productStreamRepository; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ProductStreamAttributeRepository |
||
| 41 | */ |
||
| 42 | private $streamAttrRepository; |
||
| 43 | |||
| 44 | /** @var Config */ |
||
| 45 | private $config; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param ProductStreamRepository $productStreamRepository |
||
| 49 | * @param ProductStreamAttributeRepository $streamAttrRepository |
||
| 50 | * @param Config $config |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $streamId |
||
| 64 | * @param bool $appendCurrent |
||
| 65 | * @return ProductStreamsAssignments |
||
| 66 | */ |
||
| 67 | public function prepareStreamsAssignments($streamId, $appendCurrent = true) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param $streamId |
||
| 89 | * @param bool $appendCurrent |
||
|
|
|||
| 90 | * @return ProductStreamsAssignments |
||
| 91 | */ |
||
| 92 | public function getAssignmentsForStream($streamId) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $streamId |
||
| 114 | * @throws \Exception |
||
| 115 | * @return ProductStreamsAssignments |
||
| 116 | */ |
||
| 117 | public function getStreamAssignments($streamId) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param ProductStream $stream |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function isConnectStream(ProductStream $stream) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param ProductStream $stream |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function activateConnectProductsByStream(ProductStream $stream) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param $streamId |
||
| 157 | * @return mixed |
||
| 158 | */ |
||
| 159 | public function findStream($streamId) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $streamIds |
||
| 166 | * @return \Shopware\Models\ProductStream\ProductStream[] |
||
| 167 | */ |
||
| 168 | public function findStreams(array $streamIds) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Filter the stream ids, it will return only the exported ones |
||
| 175 | * |
||
| 176 | * @param array $streamIds |
||
| 177 | * @return array |
||
| 178 | */ |
||
| 179 | public function filterExportedStreams(array $streamIds) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param array $articleIds |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | public function collectRelatedStreamsAssignments(array $articleIds) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ProductStreamsAssignments $assignments |
||
| 214 | * @param $streamId |
||
| 215 | * @param $articleId |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | public function allowToRemove(ProductStreamsAssignments $assignments, $streamId, $articleId) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param ProductStream $stream |
||
| 235 | * @return array |
||
| 236 | */ |
||
| 237 | public function getArticlesIds(ProductStream $stream) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param ProductStream $productStream |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isStatic(ProductStream $productStream) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param null $start |
||
| 261 | * @param null $limit |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function getList($start = null, $limit = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param $type |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | public function getAllExportedStreams($type) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param ProductStream $stream |
||
| 296 | * @return ProductStreamAttribute |
||
| 297 | */ |
||
| 298 | public function createStreamAttribute(ProductStream $stream) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param $streamId |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function countProductsInStaticStream($streamId) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $streamId |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isStreamExported($streamId) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param $streamId |
||
| 330 | * @return \Doctrine\DBAL\Driver\Statement|int |
||
| 331 | */ |
||
| 332 | public function markProductsToBeRemovedFromStream($streamId) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param $streamId |
||
| 339 | * @param array $articleIds |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public function createStreamRelation($streamId, array $articleIds) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return \Doctrine\DBAL\Driver\Statement|int |
||
| 349 | */ |
||
| 350 | public function removeMarkedStreamRelations() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | public function getConnectStreamIds() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param int $streamId |
||
| 365 | * @param string $status |
||
| 366 | * @param string $exportMessage |
||
| 367 | */ |
||
| 368 | public function changeStatus($streamId, $status, $exportMessage = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $streamId |
||
| 386 | * @param $message |
||
| 387 | * @deprecated Use changeStatus instead. Will be removed in version 1.2.0 |
||
| 388 | */ |
||
| 389 | public function log($streamId, $message) |
||
| 397 | } |
||
| 398 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.