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 |
||
| 9 | class Parameter |
||
| 10 | { |
||
| 11 | const ROUTE_NAME_DEFAULT = 'default'; |
||
| 12 | |||
| 13 | const PARAMETER_ID = 'id'; |
||
| 14 | const PARAMETER_SIGNATURE = 's'; |
||
| 15 | const PARAMETER_VERSION = 'v'; |
||
| 16 | const PARAMETER_ANTI_CACHE = 'ac'; // allow cache busting by adding &ac=<random> |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Router |
||
| 20 | */ |
||
| 21 | protected $router; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $routeNames; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $secret; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $destinationPrefix; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $hashAlgorithm = 'sha256'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Router $router |
||
| 45 | * @param array $routeNames |
||
| 46 | * @param string $secret |
||
| 47 | * @param string $destinationPrefix |
||
| 48 | */ |
||
| 49 | public function __construct(Router $router, array $routeNames, $secret, $destinationPrefix = 'images/') |
||
| 50 | { |
||
| 51 | $this->router = $router; |
||
| 52 | $this->secret = $secret; |
||
| 53 | $this->destinationPrefix = $destinationPrefix; |
||
| 54 | |||
| 55 | $this->setRouteNames($routeNames); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param array $routeNames |
||
| 60 | * @throws \Exception |
||
| 61 | */ |
||
| 62 | protected function setRouteNames(array $routeNames) |
||
| 63 | { |
||
| 64 | if (!array_key_exists(self::ROUTE_NAME_DEFAULT, $routeNames)) { |
||
| 65 | throw new \Exception(sprintf('Route name "%s" is required', self::ROUTE_NAME_DEFAULT)); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->routeNames = $routeNames; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param MediaInterface $media |
||
| 73 | * @param array $parameters |
||
| 74 | * @param string $routeName |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | View Code Duplication | public function generateUrl(MediaInterface $media, $parameters, $routeName = null) |
|
|
|
|||
| 78 | { |
||
| 79 | if (empty($routeName)) { |
||
| 80 | $routeName = self::ROUTE_NAME_DEFAULT; |
||
| 81 | } |
||
| 82 | |||
| 83 | return $this->router->generate( |
||
| 84 | $this->routeNames[$routeName], |
||
| 85 | $this->signParameters($media->getDefaultUrlParameters() + $parameters), |
||
| 86 | UrlGeneratorInterface::ABSOLUTE_URL |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param array $parameters |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | protected function signParameters(array $parameters) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $parameters |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | protected function calculateSignature(array $parameters) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param array $parameters |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | protected function normalizeParameters(array $parameters) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $parameters |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function isValid(array $parameters) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param array $parameters |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | protected function getFormat(array $parameters) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $source |
||
| 160 | * @param $parameters |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getDestinationFilename($source, $parameters) |
||
| 175 | } |
||
| 176 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.