| Total Complexity | 42 | 
| Total Lines | 545 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 22 | class Standard  | 
            ||
| 23 | extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base  | 
            ||
| 24 | implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface  | 
            ||
| 25 | { | 
            ||
| 26 | /**  | 
            ||
| 27 | * Copies a resource  | 
            ||
| 28 | *  | 
            ||
| 29 | * @return string|null HTML output  | 
            ||
| 30 | */  | 
            ||
| 31 | public function copy() : ?string  | 
            ||
| 32 | 	{ | 
            ||
| 33 | $view = $this->getView();  | 
            ||
| 34 | $context = $this->getContext();  | 
            ||
| 35 | |||
| 36 | try  | 
            ||
| 37 | 		{ | 
            ||
| 38 | 			if( ( $id = $view->param( 'id' ) ) === null ) { | 
            ||
| 39 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );  | 
            ||
| 40 | }  | 
            ||
| 41 | |||
| 42 | $manager = \Aimeos\MShop::create( $context, 'coupon' );  | 
            ||
| 43 | |||
| 44 | $view->item = $manager->getItem( $id );  | 
            ||
| 45 | $view->itemData = $this->toArray( $view->item, true );  | 
            ||
| 46 | $view->itemSubparts = $this->getSubClientNames();  | 
            ||
| 47 | $view->itemProviders = $this->getProviderNames();  | 
            ||
| 48 | $view->itemDecorators = $this->getDecoratorNames();  | 
            ||
| 49 | $view->itemAttributes = $this->getConfigAttributes( $view->item );  | 
            ||
| 50 | $view->itemBody = '';  | 
            ||
| 51 | |||
| 52 | foreach( $this->getSubClients() as $idx => $client )  | 
            ||
| 53 | 			{ | 
            ||
| 54 | $view->tabindex = ++$idx + 1;  | 
            ||
| 55 | $view->itemBody .= $client->copy();  | 
            ||
| 56 | }  | 
            ||
| 57 | }  | 
            ||
| 58 | catch( \Exception $e )  | 
            ||
| 59 | 		{ | 
            ||
| 60 | $this->report( $e, 'copy' );  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | return $this->render( $view );  | 
            ||
| 64 | }  | 
            ||
| 65 | |||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * Creates a new resource  | 
            ||
| 69 | *  | 
            ||
| 70 | * @return string|null HTML output  | 
            ||
| 71 | */  | 
            ||
| 72 | public function create() : ?string  | 
            ||
| 105 | }  | 
            ||
| 106 | |||
| 107 | |||
| 108 | /**  | 
            ||
| 109 | * Deletes a resource  | 
            ||
| 110 | *  | 
            ||
| 111 | * @return string|null HTML output  | 
            ||
| 112 | */  | 
            ||
| 113 | public function delete() : ?string  | 
            ||
| 153 | }  | 
            ||
| 154 | |||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * Returns a single resource  | 
            ||
| 158 | *  | 
            ||
| 159 | * @return string|null HTML output  | 
            ||
| 160 | */  | 
            ||
| 161 | public function get() : ?string  | 
            ||
| 162 | 	{ | 
            ||
| 163 | $view = $this->getView();  | 
            ||
| 164 | $context = $this->getContext();  | 
            ||
| 165 | |||
| 166 | try  | 
            ||
| 167 | 		{ | 
            ||
| 168 | 			if( ( $id = $view->param( 'id' ) ) === null ) { | 
            ||
| 169 | throw new \Aimeos\Admin\JQAdm\Exception( sprintf( 'Required parameter "%1$s" is missing', 'id' ) );  | 
            ||
| 170 | }  | 
            ||
| 171 | |||
| 172 | $manager = \Aimeos\MShop::create( $context, 'coupon' );  | 
            ||
| 173 | |||
| 174 | $view->item = $manager->getItem( $id );  | 
            ||
| 175 | $view->itemData = $this->toArray( $view->item );  | 
            ||
| 176 | $view->itemSubparts = $this->getSubClientNames();  | 
            ||
| 177 | $view->itemDecorators = $this->getDecoratorNames();  | 
            ||
| 178 | $view->itemProviders = $this->getProviderNames();  | 
            ||
| 179 | $view->itemAttributes = $this->getConfigAttributes( $view->item );  | 
            ||
| 180 | $view->itemBody = '';  | 
            ||
| 181 | |||
| 182 | foreach( $this->getSubClients() as $idx => $client )  | 
            ||
| 183 | 			{ | 
            ||
| 184 | $view->tabindex = ++$idx + 1;  | 
            ||
| 185 | $view->itemBody .= $client->get();  | 
            ||
| 186 | }  | 
            ||
| 187 | }  | 
            ||
| 188 | catch( \Exception $e )  | 
            ||
| 189 | 		{ | 
            ||
| 190 | $this->report( $e, 'get' );  | 
            ||
| 191 | }  | 
            ||
| 192 | |||
| 193 | return $this->render( $view );  | 
            ||
| 194 | }  | 
            ||
| 195 | |||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Saves the data  | 
            ||
| 199 | *  | 
            ||
| 200 | * @return string|null HTML output  | 
            ||
| 201 | */  | 
            ||
| 202 | public function save() : ?string  | 
            ||
| 203 | 	{ | 
            ||
| 204 | $view = $this->getView();  | 
            ||
| 205 | $context = $this->getContext();  | 
            ||
| 206 | |||
| 207 | $manager = \Aimeos\MShop::create( $context, 'coupon' );  | 
            ||
| 208 | $manager->begin();  | 
            ||
| 209 | |||
| 210 | try  | 
            ||
| 211 | 		{ | 
            ||
| 212 | $item = $this->fromArray( $view->param( 'item', [] ) );  | 
            ||
| 213 | $view->item = $item->getId() ? $item : $manager->saveItem( $item );  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 214 | $view->itemBody = '';  | 
            ||
| 215 | |||
| 216 | 			foreach( $this->getSubClients() as $client ) { | 
            ||
| 217 | $view->itemBody .= $client->save();  | 
            ||
| 218 | }  | 
            ||
| 219 | |||
| 220 | $manager->saveItem( clone $view->item );  | 
            ||
| 221 | $manager->commit();  | 
            ||
| 222 | |||
| 223 | $this->nextAction( $view, $view->param( 'next' ), 'coupon', $view->item->getId(), 'save' );  | 
            ||
| 224 | return null;  | 
            ||
| 225 | }  | 
            ||
| 226 | catch( \Exception $e )  | 
            ||
| 227 | 		{ | 
            ||
| 228 | $manager->rollback();  | 
            ||
| 229 | $this->report( $e, 'save' );  | 
            ||
| 230 | }  | 
            ||
| 231 | |||
| 232 | return $this->create();  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * Returns a list of resource according to the conditions  | 
            ||
| 238 | *  | 
            ||
| 239 | * @return string|null HTML output  | 
            ||
| 240 | */  | 
            ||
| 241 | public function search() : ?string  | 
            ||
| 242 | 	{ | 
            ||
| 243 | $view = $this->getView();  | 
            ||
| 244 | $context = $this->getContext();  | 
            ||
| 245 | |||
| 246 | try  | 
            ||
| 247 | 		{ | 
            ||
| 248 | $total = 0;  | 
            ||
| 249 | $params = $this->storeSearchParams( $view->param(), 'coupon' );  | 
            ||
| 250 | $manager = \Aimeos\MShop::create( $context, 'coupon' );  | 
            ||
| 251 | $search = $this->initCriteria( $manager->createSearch(), $params );  | 
            ||
| 252 | |||
| 253 | $view->items = $manager->searchItems( $search, [], $total );  | 
            ||
| 254 | $view->filterAttributes = $manager->getSearchAttributes( true );  | 
            ||
| 255 | $view->filterOperators = $search->getOperators();  | 
            ||
| 256 | $view->total = $total;  | 
            ||
| 257 | $view->itemBody = '';  | 
            ||
| 258 | |||
| 259 | 			foreach( $this->getSubClients() as $client ) { | 
            ||
| 260 | $view->itemBody .= $client->search();  | 
            ||
| 261 | }  | 
            ||
| 262 | }  | 
            ||
| 263 | catch( \Exception $e )  | 
            ||
| 264 | 		{ | 
            ||
| 265 | $this->report( $e, 'search' );  | 
            ||
| 266 | }  | 
            ||
| 267 | |||
| 268 | /** admin/jqadm/coupon/template-list  | 
            ||
| 269 | * Relative path to the HTML body template for the coupon list.  | 
            ||
| 270 | *  | 
            ||
| 271 | * The template file contains the HTML code and processing instructions  | 
            ||
| 272 | * to generate the result shown in the body of the frontend. The  | 
            ||
| 273 | * configuration string is the path to the template file relative  | 
            ||
| 274 | * to the templates directory (usually in admin/jqadm/templates).  | 
            ||
| 275 | *  | 
            ||
| 276 | * You can overwrite the template file configuration in extensions and  | 
            ||
| 277 | * provide alternative templates. These alternative templates should be  | 
            ||
| 278 | * named like the default one but with the string "default" replaced by  | 
            ||
| 279 | * an unique name. You may use the name of your project for this. If  | 
            ||
| 280 | * you've implemented an alternative client class as well, "default"  | 
            ||
| 281 | * should be replaced by the name of the new class.  | 
            ||
| 282 | *  | 
            ||
| 283 | * @param string Relative path to the template creating the HTML code  | 
            ||
| 284 | * @since 2016.04  | 
            ||
| 285 | * @category Developer  | 
            ||
| 286 | */  | 
            ||
| 287 | $tplconf = 'admin/jqadm/coupon/template-list';  | 
            ||
| 288 | $default = 'coupon/list-standard';  | 
            ||
| 289 | |||
| 290 | return $view->render( $view->config( $tplconf, $default ) );  | 
            ||
| 291 | }  | 
            ||
| 292 | |||
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * Returns the sub-client given by its name.  | 
            ||
| 296 | *  | 
            ||
| 297 | * @param string $type Name of the client type  | 
            ||
| 298 | * @param string|null $name Name of the sub-client (Default if null)  | 
            ||
| 299 | * @return \Aimeos\Admin\JQAdm\Iface Sub-client object  | 
            ||
| 300 | */  | 
            ||
| 301 | public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface  | 
            ||
| 302 | 	{ | 
            ||
| 303 | /** admin/jqadm/coupon/decorators/excludes  | 
            ||
| 304 | * Excludes decorators added by the "common" option from the coupon JQAdm client  | 
            ||
| 305 | *  | 
            ||
| 306 | * Decorators extend the functionality of a class by adding new aspects  | 
            ||
| 307 | * (e.g. log what is currently done), executing the methods of the underlying  | 
            ||
| 308 | * class only in certain conditions (e.g. only for logged in users) or  | 
            ||
| 309 | * modify what is returned to the caller.  | 
            ||
| 310 | *  | 
            ||
| 311 | * This option allows you to remove a decorator added via  | 
            ||
| 312 | * "client/jqadm/common/decorators/default" before they are wrapped  | 
            ||
| 313 | * around the JQAdm client.  | 
            ||
| 314 | *  | 
            ||
| 315 | * admin/jqadm/coupon/decorators/excludes = array( 'decorator1' )  | 
            ||
| 316 | *  | 
            ||
| 317 | * This would remove the decorator named "decorator1" from the list of  | 
            ||
| 318 | 		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via | 
            ||
| 319 | * "client/jqadm/common/decorators/default" to the JQAdm client.  | 
            ||
| 320 | *  | 
            ||
| 321 | * @param array List of decorator names  | 
            ||
| 322 | * @since 2017.07  | 
            ||
| 323 | * @category Developer  | 
            ||
| 324 | * @see admin/jqadm/common/decorators/default  | 
            ||
| 325 | * @see admin/jqadm/coupon/decorators/global  | 
            ||
| 326 | * @see admin/jqadm/coupon/decorators/local  | 
            ||
| 327 | */  | 
            ||
| 328 | |||
| 329 | /** admin/jqadm/coupon/decorators/global  | 
            ||
| 330 | * Adds a list of globally available decorators only to the coupon JQAdm client  | 
            ||
| 331 | *  | 
            ||
| 332 | * Decorators extend the functionality of a class by adding new aspects  | 
            ||
| 333 | * (e.g. log what is currently done), executing the methods of the underlying  | 
            ||
| 334 | * class only in certain conditions (e.g. only for logged in users) or  | 
            ||
| 335 | * modify what is returned to the caller.  | 
            ||
| 336 | *  | 
            ||
| 337 | * This option allows you to wrap global decorators  | 
            ||
| 338 | 		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client. | 
            ||
| 339 | *  | 
            ||
| 340 | * admin/jqadm/coupon/decorators/global = array( 'decorator1' )  | 
            ||
| 341 | *  | 
            ||
| 342 | * This would add the decorator named "decorator1" defined by  | 
            ||
| 343 | * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.  | 
            ||
| 344 | *  | 
            ||
| 345 | * @param array List of decorator names  | 
            ||
| 346 | * @since 2017.07  | 
            ||
| 347 | * @category Developer  | 
            ||
| 348 | * @see admin/jqadm/common/decorators/default  | 
            ||
| 349 | * @see admin/jqadm/coupon/decorators/excludes  | 
            ||
| 350 | * @see admin/jqadm/coupon/decorators/local  | 
            ||
| 351 | */  | 
            ||
| 352 | |||
| 353 | /** admin/jqadm/coupon/decorators/local  | 
            ||
| 354 | * Adds a list of local decorators only to the coupon JQAdm client  | 
            ||
| 355 | *  | 
            ||
| 356 | * Decorators extend the functionality of a class by adding new aspects  | 
            ||
| 357 | * (e.g. log what is currently done), executing the methods of the underlying  | 
            ||
| 358 | * class only in certain conditions (e.g. only for logged in users) or  | 
            ||
| 359 | * modify what is returned to the caller.  | 
            ||
| 360 | *  | 
            ||
| 361 | * This option allows you to wrap local decorators  | 
            ||
| 362 | 		 * ("\Aimeos\Admin\JQAdm\Coupon\Decorator\*") around the JQAdm client. | 
            ||
| 363 | *  | 
            ||
| 364 | * admin/jqadm/coupon/decorators/local = array( 'decorator2' )  | 
            ||
| 365 | *  | 
            ||
| 366 | * This would add the decorator named "decorator2" defined by  | 
            ||
| 367 | * "\Aimeos\Admin\JQAdm\Coupon\Decorator\Decorator2" only to the JQAdm client.  | 
            ||
| 368 | *  | 
            ||
| 369 | * @param array List of decorator names  | 
            ||
| 370 | * @since 2017.07  | 
            ||
| 371 | * @category Developer  | 
            ||
| 372 | * @see admin/jqadm/common/decorators/default  | 
            ||
| 373 | * @see admin/jqadm/coupon/decorators/excludes  | 
            ||
| 374 | * @see admin/jqadm/coupon/decorators/global  | 
            ||
| 375 | */  | 
            ||
| 376 | return $this->createSubClient( 'coupon/' . $type, $name );  | 
            ||
| 377 | }  | 
            ||
| 378 | |||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * Returns the backend configuration attributes of the provider and decorators  | 
            ||
| 382 | *  | 
            ||
| 383 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item incl. provider/decorator property  | 
            ||
| 384 | * @return \Aimeos\MW\Common\Critera\Attribute\Iface[] List of configuration attributes  | 
            ||
| 385 | */  | 
            ||
| 386 | public function getConfigAttributes( \Aimeos\MShop\Coupon\Item\Iface $item ) : array  | 
            ||
| 387 | 	{ | 
            ||
| 388 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );  | 
            ||
| 389 | |||
| 390 | 		try { | 
            ||
| 391 | return $manager->getProvider( $item, '' )->getConfigBE();  | 
            ||
| 392 | 		} catch( \Aimeos\MShop\Exception $e ) { | 
            ||
| 393 | return [];  | 
            ||
| 394 | }  | 
            ||
| 395 | }  | 
            ||
| 396 | |||
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * Returns the list of sub-client names configured for the client.  | 
            ||
| 400 | *  | 
            ||
| 401 | * @return array List of JQAdm client names  | 
            ||
| 402 | */  | 
            ||
| 403 | protected function getSubClientNames() : array  | 
            ||
| 439 | }  | 
            ||
| 440 | |||
| 441 | |||
| 442 | /**  | 
            ||
| 443 | * Returns the names of the available coupon decorators  | 
            ||
| 444 | *  | 
            ||
| 445 | * @return string[] List of decorator class names  | 
            ||
| 446 | */  | 
            ||
| 447 | protected function getDecoratorNames() : array  | 
            ||
| 448 | 	{ | 
            ||
| 449 | $ds = DIRECTORY_SEPARATOR;  | 
            ||
| 450 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' . $ds . 'Decorator' );  | 
            ||
| 451 | }  | 
            ||
| 452 | |||
| 453 | |||
| 454 | /**  | 
            ||
| 455 | * Returns the names of the available coupon providers  | 
            ||
| 456 | *  | 
            ||
| 457 | * @return string[] List of provider class names  | 
            ||
| 458 | */  | 
            ||
| 459 | protected function getProviderNames() : array  | 
            ||
| 460 | 	{ | 
            ||
| 461 | $ds = DIRECTORY_SEPARATOR;  | 
            ||
| 462 | return $this->getClassNames( 'MShop' . $ds . 'Coupon' . $ds . 'Provider' );  | 
            ||
| 463 | }  | 
            ||
| 464 | |||
| 465 | |||
| 466 | /**  | 
            ||
| 467 | * Creates new and updates existing items using the data array  | 
            ||
| 468 | *  | 
            ||
| 469 | * @param array $data Data array  | 
            ||
| 470 | * @return \Aimeos\MShop\Coupon\Item\Iface New coupon item object  | 
            ||
| 471 | */  | 
            ||
| 472 | protected function fromArray( array $data ) : \Aimeos\MShop\Coupon\Item\Iface  | 
            ||
| 473 | 	{ | 
            ||
| 474 | $conf = [];  | 
            ||
| 475 | |||
| 476 | if( isset( $data['config']['key'] ) )  | 
            ||
| 477 | 		{ | 
            ||
| 478 | foreach( (array) $data['config']['key'] as $idx => $key )  | 
            ||
| 479 | 			{ | 
            ||
| 480 | if( trim( $key ) !== '' && isset( $data['config']['val'][$idx] ) )  | 
            ||
| 481 | 				{ | 
            ||
| 482 | 					if( ( $val = json_decode( $data['config']['val'][$idx] ) ) === null ) { | 
            ||
| 483 | $conf[$key] = $data['config']['val'][$idx];  | 
            ||
| 484 | 					} else { | 
            ||
| 485 | $conf[$key] = $val;  | 
            ||
| 486 | }  | 
            ||
| 487 | }  | 
            ||
| 488 | }  | 
            ||
| 489 | }  | 
            ||
| 490 | |||
| 491 | $manager = \Aimeos\MShop::create( $this->getContext(), 'coupon' );  | 
            ||
| 492 | |||
| 493 | 		if( isset( $data['coupon.id'] ) && $data['coupon.id'] != '' ) { | 
            ||
| 494 | $item = $manager->getItem( $data['coupon.id'] );  | 
            ||
| 495 | 		} else { | 
            ||
| 496 | $item = $manager->createItem();  | 
            ||
| 497 | }  | 
            ||
| 498 | |||
| 499 | $item->fromArray( $data, true );  | 
            ||
| 500 | $item->setConfig( $conf );  | 
            ||
| 501 | |||
| 502 | return $item;  | 
            ||
| 503 | }  | 
            ||
| 504 | |||
| 505 | |||
| 506 | /**  | 
            ||
| 507 | * Constructs the data array for the view from the given item  | 
            ||
| 508 | *  | 
            ||
| 509 | * @param \Aimeos\MShop\Coupon\Item\Iface $item Coupon item object  | 
            ||
| 510 | * @return string[] Multi-dimensional associative list of item data  | 
            ||
| 511 | */  | 
            ||
| 512 | protected function toArray( \Aimeos\MShop\Coupon\Item\Iface $item, bool $copy = false ) : array  | 
            ||
| 513 | 	{ | 
            ||
| 514 | $config = $item->getConfig();  | 
            ||
| 515 | $data = $item->toArray( true );  | 
            ||
| 516 | $data['config'] = [];  | 
            ||
| 517 | |||
| 518 | if( $copy === true )  | 
            ||
| 519 | 		{ | 
            ||
| 520 | $data['coupon.siteid'] = $this->getContext()->getLocale()->getSiteId();  | 
            ||
| 521 | $data['coupon.id'] = '';  | 
            ||
| 522 | }  | 
            ||
| 523 | |||
| 524 | ksort( $config );  | 
            ||
| 525 | |||
| 526 | foreach( $config as $key => $value )  | 
            ||
| 527 | 		{ | 
            ||
| 528 | $data['config']['key'][] = $key;  | 
            ||
| 529 | $data['config']['val'][] = $value;  | 
            ||
| 530 | }  | 
            ||
| 531 | |||
| 532 | return $data;  | 
            ||
| 533 | }  | 
            ||
| 534 | |||
| 535 | |||
| 536 | /**  | 
            ||
| 537 | * Returns the rendered template including the view data  | 
            ||
| 538 | *  | 
            ||
| 539 | * @param \Aimeos\MW\View\Iface $view View object with data assigned  | 
            ||
| 540 | * @return string|null HTML output  | 
            ||
| 541 | */  | 
            ||
| 542 | protected function render( \Aimeos\MW\View\Iface $view ) : string  | 
            ||
| 567 | }  | 
            ||
| 568 | }  | 
            ||
| 569 | 
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.