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 |
||
| 24 | class FormatNegotiator extends BaseNegotiator |
||
| 25 | { |
||
| 26 | private $map = []; |
||
| 27 | private $requestStack; |
||
| 28 | |||
| 29 | 20 | public function __construct(RequestStack $requestStack, array $mimeTypes = array()) |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param RequestMatcherInterface $requestMatcher |
||
| 37 | * @param array $options |
||
| 38 | */ |
||
| 39 | 19 | public function add(RequestMatcherInterface $requestMatcher, array $options = []) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | * The best format is also determined in function of the bundle configuration. |
||
| 47 | * |
||
| 48 | * @throws StopFormatListenerException |
||
| 49 | */ |
||
| 50 | 17 | public function getBest($header, array $priorities = []) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $values |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | private function sanitize(array $values) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Transform the format (json, html, ...) to their mimeType form (application/json, text/html, ...). |
||
| 123 | * |
||
| 124 | * @param Request $request |
||
| 125 | * @param string[] $priorities |
||
| 126 | * |
||
| 127 | * @return string[] formatted priorities |
||
| 128 | */ |
||
| 129 | 12 | private function normalizePriorities(Request $request, array $priorities) |
|
| 130 | { |
||
| 131 | 12 | $priorities = $this->sanitize($priorities); |
|
| 132 | |||
| 133 | 12 | $mimeTypes = array(); |
|
| 134 | 12 | foreach ($priorities as $priority) { |
|
| 135 | 12 | if (strpos($priority, '/')) { |
|
| 136 | 2 | $mimeTypes[] = $priority; |
|
| 137 | 2 | continue; |
|
| 138 | } |
||
| 139 | |||
| 140 | 11 | if (method_exists(Request::class, 'getMimeTypes')) { |
|
| 141 | 11 | $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority)); |
|
| 142 | 11 | } elseif (null !== $request->getMimeType($priority)) { |
|
| 143 | $class = new \ReflectionClass(Request::class); |
||
| 144 | $properties = $class->getStaticProperties(); |
||
| 145 | $mimeTypes = array_merge($mimeTypes, $properties['formats'][$priority]); |
||
| 146 | } |
||
| 147 | |||
| 148 | 11 | if (isset($this->mimeTypes[$priority])) { |
|
| 149 | 3 | foreach ($this->mimeTypes[$priority] as $mimeType) { |
|
| 150 | 3 | $mimeTypes[] = $mimeType; |
|
| 151 | 3 | } |
|
| 152 | 3 | } |
|
| 153 | 12 | } |
|
| 154 | |||
| 155 | 12 | return $mimeTypes; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @throws \RuntimeException |
||
| 160 | * |
||
| 161 | * @return Request |
||
| 162 | */ |
||
| 163 | 17 | View Code Duplication | private function getRequest() |
| 172 | } |
||
| 173 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: