| Conditions | 10 |
| Paths | 36 |
| Total Lines | 32 |
| Code Lines | 19 |
| Lines | 5 |
| Ratio | 15.63 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 69 | public function add(SS_HTTPRequest $request) |
||
| 70 | { |
||
| 71 | $quantity = ($request->requestVar('quantity')) ? intval($request->requestVar('quantity')) : 1; |
||
| 72 | $documentId = (int)$request->param('ID'); |
||
| 73 | if ($doc = DMSDocument::get()->byID($documentId)) { |
||
| 74 | if ($doc->isAllowedInCart() && $doc->canView()) { |
||
| 75 | if ($this->getCart()->getItem($documentId)) { |
||
| 76 | $this->getCart()->updateItemQuantity($documentId, $quantity); |
||
| 77 | } else { |
||
| 78 | $requestItem = DMSRequestItem::create()->setDocument($doc)->setQuantity($quantity); |
||
| 79 | $this->getCart()->addItem($requestItem); |
||
| 80 | } |
||
| 81 | $backURL = $request->getVar('BackURL'); |
||
| 82 | // make sure that backURL is a relative path (starts with /) |
||
| 83 | if (isset($backURL) && preg_match('/^\//', $backURL)) { |
||
| 84 | $this->getCart()->setBackUrl($backURL); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | View Code Duplication | if ($request->isAjax()) { |
|
| 90 | $this->response->addHeader('Content-Type', 'application/json'); |
||
| 91 | |||
| 92 | return Convert::raw2json(array('result' => true)); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($backURL = $request->getVar('BackURL')) { |
||
| 96 | return $this->redirect($backURL); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $this->redirectBack(); |
||
| 100 | } |
||
| 101 | |||
| 157 |
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.