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 GoogleAnalytics |
||
| 10 | { |
||
| 11 | use Utils\HtmlInjectorTrait; |
||
| 12 | use Utils\AttributeTrait; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string The site's ID |
||
| 16 | */ |
||
| 17 | private $siteId; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor. Set the site's ID. |
||
| 21 | * |
||
| 22 | * @param string $siteId |
||
| 23 | */ |
||
| 24 | public function __construct($siteId) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Execute the middleware. |
||
| 31 | * |
||
| 32 | * @param ServerRequestInterface $request |
||
| 33 | * @param ResponseInterface $response |
||
| 34 | * @param callable $next |
||
| 35 | * |
||
| 36 | * @return ResponseInterface |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
| 39 | { |
||
| 40 | $response = $next($request, $response); |
||
| 41 | |||
| 42 | if (Utils\Helpers::getMimeType($response) === 'text/html' && !Utils\Helpers::isAjax($request)) { |
||
| 43 | return $this->inject($response, $this->getCode()); |
||
| 44 | } |
||
| 45 | |||
| 46 | return $response; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Returns the google code. |
||
| 51 | * https://github.com/h5bp/html5-boilerplate/blob/master/src/index.html. |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | private function getCode() |
||
| 68 | } |
||
| 69 |