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 |
||
| 26 | class FormatNegotiator extends BaseNegotiator |
||
| 27 | { |
||
| 28 | private $map = []; |
||
| 29 | private $requestStack; |
||
| 30 | private $mimeTypes; |
||
| 31 | |||
| 32 | 22 | public function __construct(RequestStack $requestStack, array $mimeTypes = array()) |
|
| 33 | { |
||
| 34 | 22 | $this->requestStack = $requestStack; |
|
| 35 | 22 | $this->mimeTypes = $mimeTypes; |
|
| 36 | 22 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @param RequestMatcherInterface $requestMatcher |
||
| 40 | * @param array $options |
||
| 41 | */ |
||
| 42 | 21 | public function add(RequestMatcherInterface $requestMatcher, array $options = []) |
|
| 43 | { |
||
| 44 | 21 | $this->map[] = [$requestMatcher, $options]; |
|
| 45 | 21 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | * The best format is also determined in function of the bundle configuration. |
||
| 50 | * |
||
| 51 | * @throws StopFormatListenerException |
||
| 52 | */ |
||
| 53 | 19 | public function getBest($header, array $priorities = []) |
|
| 54 | { |
||
| 55 | 19 | $request = $this->getRequest(); |
|
| 56 | 19 | $header = $header ?: $request->headers->get('Accept'); |
|
| 57 | |||
| 58 | 19 | foreach ($this->map as $elements) { |
|
| 59 | // Check if the current RequestMatcherInterface matches the current request |
||
| 60 | 18 | if (!$elements[0]->matches($request)) { |
|
| 61 | 1 | continue; |
|
| 62 | } |
||
| 63 | 18 | $options = &$elements[1]; // Do not reallow memory for this variable |
|
| 64 | |||
| 65 | 18 | if (!empty($options['stop'])) { |
|
| 66 | 1 | throw new StopFormatListenerException('Stopped format listener'); |
|
| 67 | } |
||
| 68 | 17 | if (empty($options['priorities']) && empty($priorities)) { |
|
| 69 | 2 | if (!empty($options['fallback_format'])) { |
|
| 70 | 2 | return new Accept($request->getMimeType($options['fallback_format'])); |
|
| 71 | } |
||
| 72 | |||
| 73 | 1 | continue; |
|
| 74 | } |
||
| 75 | |||
| 76 | 15 | if (isset($options['prefer_extension']) && $options['prefer_extension'] && !isset($extensionHeader)) { |
|
| 77 | 10 | $extension = pathinfo($request->getPathInfo(), PATHINFO_EXTENSION); |
|
| 78 | |||
| 79 | 10 | if (!empty($extension)) { |
|
| 80 | // $extensionHeader will now be either a non empty string or an empty string |
||
| 81 | 5 | $extensionHeader = $request->getMimeType($extension); |
|
| 82 | |||
| 83 | 5 | if ($extensionHeader) { |
|
|
|
|||
| 84 | 2 | $header = $extensionHeader.'; q='.$options['prefer_extension'].($header ? ','.$header : ''); |
|
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | 15 | if ($header) { |
|
| 90 | 14 | $mimeTypes = $this->normalizePriorities($request, |
|
| 91 | 14 | empty($priorities) ? $options['priorities'] : $priorities |
|
| 92 | ); |
||
| 93 | |||
| 94 | 14 | $mimeType = parent::getBest($header, $mimeTypes); |
|
| 95 | |||
| 96 | 14 | if (null !== $mimeType) { |
|
| 97 | 13 | return $mimeType; |
|
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | 2 | if (isset($options['fallback_format'])) { |
|
| 102 | // if false === fallback_format then we fail here instead of considering more rules |
||
| 103 | 2 | if (false === $options['fallback_format']) { |
|
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | // stop looking at rules since we have a fallback defined |
||
| 108 | 2 | return new Accept($request->getMimeType($options['fallback_format'])); |
|
| 109 | } |
||
| 110 | } |
||
| 111 | 3 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param array $values |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | 14 | private function sanitize(array $values) |
|
| 119 | { |
||
| 120 | return array_map(function ($value) { |
||
| 121 | 14 | return preg_replace('/\s+/', '', strtolower($value)); |
|
| 122 | 14 | }, $values); |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Transform the format (json, html, ...) to their mimeType form (application/json, text/html, ...). |
||
| 127 | * |
||
| 128 | * @param Request $request |
||
| 129 | * @param string[] $priorities |
||
| 130 | * |
||
| 131 | * @return string[] formatted priorities |
||
| 132 | */ |
||
| 133 | 14 | private function normalizePriorities(Request $request, array $priorities) |
|
| 134 | { |
||
| 135 | 14 | $priorities = $this->sanitize($priorities); |
|
| 136 | |||
| 137 | 14 | $mimeTypes = array(); |
|
| 138 | 14 | foreach ($priorities as $priority) { |
|
| 139 | 14 | if (strpos($priority, '/')) { |
|
| 140 | 4 | $mimeTypes[] = $priority; |
|
| 141 | |||
| 142 | 4 | continue; |
|
| 143 | } |
||
| 144 | |||
| 145 | 11 | $mimeTypes = array_merge($mimeTypes, Request::getMimeTypes($priority)); |
|
| 146 | |||
| 147 | 11 | if (isset($this->mimeTypes[$priority])) { |
|
| 148 | 3 | foreach ($this->mimeTypes[$priority] as $mimeType) { |
|
| 149 | 3 | $mimeTypes[] = $mimeType; |
|
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | 14 | return $mimeTypes; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @throws \RuntimeException |
||
| 159 | * |
||
| 160 | * @return Request |
||
| 161 | */ |
||
| 162 | 19 | View Code Duplication | private function getRequest() |
| 171 | } |
||
| 172 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: