| Conditions | 10 | 
| Paths | 21 | 
| Total Lines | 99 | 
| Code Lines | 58 | 
| Lines | 0 | 
| Ratio | 0 % | 
| 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  | 
            ||
| 47 | public function autoCompleteAction(Request $request)  | 
            ||
| 48 |     { | 
            ||
| 49 | /** @var Admin $admin */  | 
            ||
| 50 |         $admin = $this->pool->getInstance($request->get('code')); | 
            ||
| 51 | $admin->setRequest($request);  | 
            ||
| 52 | |||
| 53 | // check user permission  | 
            ||
| 54 |         if (false === $admin->isGranted('LIST')) { | 
            ||
| 55 | throw new AccessDeniedException();  | 
            ||
| 56 | }  | 
            ||
| 57 | |||
| 58 | // subject will be empty to avoid unnecessary database requests and keep auto-complete function fast  | 
            ||
| 59 | $admin->setSubject($admin->getNewInstance());  | 
            ||
| 60 |         $fieldDescription = $this->retrieveFieldDescription($admin, $request->get('field')); | 
            ||
| 61 | $formAutocomplete = $admin->getForm()->get($fieldDescription->getName());  | 
            ||
| 62 | |||
| 63 |         if ($formAutocomplete->getConfig()->getAttribute('disabled')) { | 
            ||
| 64 |             throw new AccessDeniedException('Autocomplete list can`t be retrieved because the form element is disabled or read_only.'); | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 |         $class = $formAutocomplete->getConfig()->getOption('class'); | 
            ||
| 68 |         $property = $formAutocomplete->getConfig()->getAttribute('property'); | 
            ||
| 69 |         $minimumInputLength = $formAutocomplete->getConfig()->getAttribute('minimum_input_length'); | 
            ||
| 70 |         $itemsPerPage = $formAutocomplete->getConfig()->getAttribute('items_per_page'); | 
            ||
| 71 |         $reqParamPageNumber = $formAutocomplete->getConfig()->getAttribute('req_param_name_page_number'); | 
            ||
| 72 |         $toStringCallback = $formAutocomplete->getConfig()->getAttribute('to_string_callback'); | 
            ||
| 73 | |||
| 74 |         $searchText = $request->get('q'); | 
            ||
| 75 |         if (mb_strlen($searchText, 'UTF-8') < $minimumInputLength) { | 
            ||
| 76 |             return new JsonResponse(array('status' => 'KO', 'message' => 'Too short search string.'), 403); | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | $page = $request->get($reqParamPageNumber);  | 
            ||
| 80 | $offset = ($page - 1) * $itemsPerPage;  | 
            ||
| 81 | |||
| 82 | /** @var ModelManager $modelManager */  | 
            ||
| 83 |         $modelManager = $formAutocomplete->getConfig()->getOption('model_manager'); | 
            ||
| 84 | $dm = $modelManager->getDocumentManager();  | 
            ||
| 85 | |||
| 86 |         if ($class) { | 
            ||
| 87 | /** @var $qb \Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder */  | 
            ||
| 88 |             $qb = $dm->getRepository($class)->createQueryBuilder('a'); | 
            ||
| 89 |             $qb->where()->fullTextSearch("a.$property", '*'.$searchText.'*'); | 
            ||
| 90 | $qb->setFirstResult($offset);  | 
            ||
| 91 | //fetch one more to determine if there are more pages  | 
            ||
| 92 | $qb->setMaxResults($itemsPerPage + 1);  | 
            ||
| 93 | $query = $qb->getQuery();  | 
            ||
| 94 | $results = $query->execute();  | 
            ||
| 95 |         } else { | 
            ||
| 96 | /** @var $qb \PHPCR\Util\QOM\QueryBuilder */  | 
            ||
| 97 | $qb = $dm->createPhpcrQueryBuilder();  | 
            ||
| 98 | // TODO: node type should probably be configurable  | 
            ||
| 99 |             $qb->from($qb->getQOMFactory()->selector('a', 'nt:unstructured')); | 
            ||
| 100 |             $qb->where($qb->getQOMFactory()->fullTextSearch('a', $property, '*'.$searchText.'*')); | 
            ||
| 101 | // handle attribute translation  | 
            ||
| 102 |             $qb->orWhere($qb->getQOMFactory()->fullTextSearch('a', $dm->getTranslationStrategy('attribute')->getTranslatedPropertyName($request->getLocale(), $property), '*'.$searchText.'*')); | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 103 | $qb->setFirstResult($offset);  | 
            ||
| 104 | //fetch one more to determine if there are more pages  | 
            ||
| 105 | $qb->setMaxResults($itemsPerPage + 1);  | 
            ||
| 106 | |||
| 107 | $results = $dm->getDocumentsByPhpcrQuery($qb->getQuery());  | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | //did we max out x+1  | 
            ||
| 111 | $more = (count($results) == $itemsPerPage + 1);  | 
            ||
| 112 |         $method = $request->get('_method_name'); | 
            ||
| 113 | |||
| 114 | $items = array();  | 
            ||
| 115 |         foreach ($results as $path => $document) { | 
            ||
| 116 | // handle child translation  | 
            ||
| 117 |             if (strpos(PathHelper::getNodeName($path), Translation::LOCALE_NAMESPACE.':') === 0) { | 
            ||
| 118 | $document = $dm->find(null, PathHelper::getParentPath($path));  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 |             if (!method_exists($document, $method)) { | 
            ||
| 122 | continue;  | 
            ||
| 123 | }  | 
            ||
| 124 | |||
| 125 |             $label = $document->{$method}(); | 
            ||
| 126 |             if ($toStringCallback !== null) { | 
            ||
| 127 |                 if (!is_callable($toStringCallback)) { | 
            ||
| 128 |                     throw new \RuntimeException('Option "to_string_callback" does not contain callable function.'); | 
            ||
| 129 | }  | 
            ||
| 130 | |||
| 131 | $label = call_user_func($toStringCallback, $document, $property);  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | $items[] = array(  | 
            ||
| 135 | 'id' => $admin->id($document),  | 
            ||
| 136 | 'label' => $label,  | 
            ||
| 137 | );  | 
            ||
| 138 | }  | 
            ||
| 139 | |||
| 140 | return new JsonResponse(array(  | 
            ||
| 141 | 'status' => 'OK',  | 
            ||
| 142 | 'more' => $more,  | 
            ||
| 143 | 'items' => $items,  | 
            ||
| 144 | ));  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 174 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: