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