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 |
||
| 13 | class UserAgentParser implements ParserInterface, RobotsTxtInterface |
||
| 14 | { |
||
| 15 | use DirectiveParserCommons; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Sub directives white list |
||
| 19 | */ |
||
| 20 | const SUB_DIRECTIVES = [ |
||
| 21 | self::DIRECTIVE_ALLOW, |
||
| 22 | self::DIRECTIVE_CACHE_DELAY, |
||
| 23 | self::DIRECTIVE_COMMENT, |
||
| 24 | self::DIRECTIVE_CRAWL_DELAY, |
||
| 25 | self::DIRECTIVE_DISALLOW, |
||
| 26 | self::DIRECTIVE_REQUEST_RATE, |
||
| 27 | self::DIRECTIVE_ROBOT_VERSION, |
||
| 28 | self::DIRECTIVE_VISIT_TIME, |
||
| 29 | ]; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Directive |
||
| 33 | */ |
||
| 34 | const DIRECTIVE = self::DIRECTIVE_USER_AGENT; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Base Uri |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $base; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * User-agent handler |
||
| 44 | * @var SubDirectiveHandler[] |
||
| 45 | */ |
||
| 46 | private $handler = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * User-agent(s) |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | private $userAgent = [self::USER_AGENT]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * User-agent client cache |
||
| 56 | * @var UserAgentClient |
||
| 57 | */ |
||
| 58 | private $client; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * UserAgent constructor. |
||
| 62 | * |
||
| 63 | * @param string $base |
||
| 64 | */ |
||
| 65 | public function __construct($base) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Set new User-agent |
||
| 73 | * |
||
| 74 | * @param array $array |
||
| 75 | * @return bool |
||
| 76 | */ |
||
| 77 | public function set(array $array = [self::USER_AGENT]) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add |
||
| 90 | * |
||
| 91 | * @param string $line |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | public function add($line) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Client |
||
| 131 | * |
||
| 132 | * @param string $userAgent |
||
| 133 | * @param int|null $statusCode |
||
| 134 | * @return UserAgentClient |
||
| 135 | */ |
||
| 136 | public function client($userAgent = self::USER_AGENT, $statusCode = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * User-agent list |
||
| 151 | * |
||
| 152 | * @return string[] |
||
| 153 | */ |
||
| 154 | public function getUserAgents() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Rule array |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getRules() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Render |
||
| 187 | * |
||
| 188 | * @return string[] |
||
| 189 | */ |
||
| 190 | public function render() |
||
| 212 | } |
||
| 213 |
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.