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 | 21 | public function __construct(RequestStack $requestStack, array $mimeTypes = array()) |
|
| 30 | { |
||
| 31 | 21 | $this->requestStack = $requestStack; |
|
| 32 | 21 | $this->mimeTypes = $mimeTypes; |
|
|
|
|||
| 33 | 21 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param RequestMatcherInterface $requestMatcher |
||
| 37 | * @param array $options |
||
| 38 | */ |
||
| 39 | 20 | 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 | 18 | public function getBest($header, array $priorities = []) |
|
| 51 | { |
||
| 52 | 18 | $request = $this->getRequest(); |
|
| 53 | 18 | $header = $header ?: $request->headers->get('Accept'); |
|
| 54 | |||
| 55 | 18 | foreach ($this->map as $elements) { |
|
| 56 | // Check if the current RequestMatcherInterface matches the current request |
||
| 57 | 17 | if (!$elements[0]->matches($request)) { |
|
| 58 | 1 | continue; |
|
| 59 | } |
||
| 60 | 17 | $options = &$elements[1]; // Do not reallow memory for this variable |
|
| 61 | |||
| 62 | 17 | if (!empty($options['stop'])) { |
|
| 63 | 1 | throw new StopFormatListenerException('Stopped format listener'); |
|
| 64 | } |
||
| 65 | 16 | if (empty($options['priorities']) && empty($priorities)) { |
|
| 66 | 2 | if (!empty($options['fallback_format'])) { |
|
| 67 | 2 | return new Accept($request->getMimeType($options['fallback_format'])); |
|
| 68 | } |
||
| 69 | 1 | continue; |
|
| 70 | } |
||
| 71 | |||
| 72 | 14 | if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) { |
|
| 73 | 9 | $extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION); |
|
| 74 | |||
| 75 | 9 | if (!empty($extension)) { |
|
| 76 | // $extensionHeader will now be either a non empty string or an empty string |
||
| 77 | 1 | $extensionHeader = $request->getMimeType($extension); |
|
| 78 | 1 | if ($header && $extensionHeader) { |
|
| 79 | $header .= ','; |
||
| 80 | } |
||
| 81 | 1 | $header .= $extensionHeader.'; q='.$options['prefer_extension']; |
|
| 82 | 1 | } |
|
| 83 | 9 | } |
|
| 84 | |||
| 85 | 14 | if ($header) { |
|
| 86 | 13 | $mimeTypes = $this->normalizePriorities($request, |
|
| 87 | 13 | empty($priorities) ? $options['priorities'] : $priorities |
|
| 88 | 13 | ); |
|
| 89 | |||
| 90 | 13 | $mimeType = parent::getBest($header, $mimeTypes); |
|
| 91 | |||
| 92 | 13 | if ($mimeType !== null) { |
|
| 93 | 12 | return $mimeType; |
|
| 94 | } |
||
| 95 | 1 | } |
|
| 96 | |||
| 97 | 2 | if (isset($options['fallback_format'])) { |
|
| 98 | // if false === fallback_format then we fail here instead of considering more rules |
||
| 99 | 2 | if (false === $options['fallback_format']) { |
|
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // stop looking at rules since we have a fallback defined |
||
| 104 | 2 | return new Accept($request->getMimeType($options['fallback_format'])); |
|
| 105 | } |
||
| 106 | 3 | } |
|
| 107 | 3 | } |
|
| 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 | 13 | private function normalizePriorities(Request $request, array $priorities) |
|
| 130 | { |
||
| 131 | 13 | $priorities = $this->sanitize($priorities); |
|
| 132 | |||
| 133 | 13 | $mimeTypes = array(); |
|
| 134 | 13 | foreach ($priorities as $priority) { |
|
| 135 | 13 | if (strpos($priority, '/')) { |
|
| 136 | 2 | $mimeTypes[] = $priority; |
|
| 137 | 2 | continue; |
|
| 138 | } |
||
| 139 | |||
| 140 | 12 | if (method_exists(Request::class, 'getMimeTypes')) { |
|
| 141 | 12 | $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority)); |
|
| 142 | 12 | } 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 | 12 | if (isset($this->mimeTypes[$priority])) { |
|
| 149 | 3 | foreach ($this->mimeTypes[$priority] as $mimeType) { |
|
| 150 | 3 | $mimeTypes[] = $mimeType; |
|
| 151 | 3 | } |
|
| 152 | 3 | } |
|
| 153 | 13 | } |
|
| 154 | |||
| 155 | 13 | return $mimeTypes; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @throws \RuntimeException |
||
| 160 | * |
||
| 161 | * @return Request |
||
| 162 | */ |
||
| 163 | 18 | 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: