Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | abstract class Context implements ContextInterface |
||
| 13 | { |
||
| 14 | use Backgroundable, |
||
| 15 | Taggable; |
||
| 16 | |||
| 17 | protected $config = []; |
||
| 18 | protected $container; |
||
| 19 | |||
| 20 | public function initialize(array $config, ContainerInterface $container) |
||
| 25 | |||
| 26 | protected function getRecordBag() |
||
| 30 | |||
| 31 | protected function getEntityHydrator() |
||
| 35 | |||
| 36 | protected function getEntityResolver() |
||
| 40 | |||
| 41 | protected function getTextFormater() |
||
| 45 | |||
| 46 | protected function getAsserter() |
||
| 50 | |||
| 51 | protected function getGuesserManager() |
||
| 55 | |||
| 56 | protected function getObjectReflector() |
||
| 60 | |||
| 61 | protected function getFeatureWalker() |
||
| 65 | |||
| 66 | protected function getAliceLoader() |
||
| 70 | |||
| 71 | protected function getEntityManager() |
||
| 75 | |||
| 76 | protected function getUniqueCache() |
||
| 80 | |||
| 81 | protected function getPageClassResolver() |
||
| 85 | |||
| 86 | protected function getRequestBuilder() |
||
| 90 | |||
| 91 | protected function getHttpContentTypeGuesser() |
||
| 95 | |||
| 96 | View Code Duplication | protected function get($service) |
|
| 97 | { |
||
| 98 | if ($this->container->has($service)) { |
||
| 99 | return $this->container->get($service); |
||
| 100 | } |
||
| 101 | |||
| 102 | if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) { |
||
| 103 | return $this->getKernel()->getContainer()->get($service); |
||
| 104 | } |
||
| 105 | |||
| 106 | throw new ServiceNotFoundException($service); |
||
| 107 | } |
||
| 108 | |||
| 109 | View Code Duplication | protected function getParameter($name) |
|
| 110 | { |
||
| 111 | if ($this->container->hasParameter($name)) { |
||
| 112 | return $this->container->getParameter($name); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) { |
||
| 116 | return $this->getKernel()->getContainer()->getParameter($name); |
||
| 117 | } |
||
| 118 | |||
| 119 | throw new ParameterNotFoundException($name); |
||
| 120 | } |
||
| 121 | |||
| 122 | protected function getKernel() |
||
| 123 | { |
||
| 124 | if ($this->container->has('friendly.symfony.kernel')) { |
||
| 125 | $kernel = $this->container->get('friendly.symfony.kernel'); |
||
| 126 | $kernel->boot(); |
||
| 127 | |||
| 128 | return $kernel; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | protected function resolveEntity($name) |
||
| 133 | { |
||
| 134 | $namespaces = $this->getParameter('friendly.entities.namespaces'); |
||
| 135 | |||
| 136 | $entities = $this |
||
| 137 | ->getEntityResolver() |
||
| 138 | ->resolve( |
||
| 139 | $this->getEntityManager(), |
||
| 140 | $name, |
||
| 141 | empty($namespaces) ? '' : $namespaces |
||
| 142 | ) |
||
| 143 | ; |
||
| 144 | |||
| 145 | switch (true) { |
||
| 146 | case 1 < count($entities): |
||
| 147 | throw new \Exception( |
||
| 148 | sprintf( |
||
| 149 | 'Failed to find a unique model from the name "%s", "%s" found', |
||
| 150 | $name, |
||
| 151 | implode('" and "', array_map( |
||
| 152 | function ($rfl) { |
||
| 153 | return $rfl->getName(); |
||
| 154 | }, |
||
| 155 | $entities |
||
| 156 | )) |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | break; |
||
| 160 | case 0 === count($entities): |
||
| 161 | throw new \Exception( |
||
| 162 | sprintf( |
||
| 163 | 'Failed to find a model from the name "%s"', |
||
| 164 | $name |
||
| 165 | ) |
||
| 166 | ); |
||
| 167 | } |
||
| 168 | |||
| 169 | return current($entities); |
||
| 170 | } |
||
| 171 | |||
| 172 | protected function getDefaultOptions() |
||
| 176 | } |
||
| 177 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.