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 DisAllowParser implements ParserInterface, RobotsTxtInterface |
||
| 14 | { |
||
| 15 | use DirectiveParserCommons; |
||
| 16 | use UrlParser; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Directive alternatives |
||
| 20 | */ |
||
| 21 | const DIRECTIVE = [ |
||
| 22 | self::DIRECTIVE_ALLOW, |
||
| 23 | self::DIRECTIVE_DISALLOW, |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Sub directives white list |
||
| 28 | */ |
||
| 29 | const SUB_DIRECTIVES = [ |
||
| 30 | self::DIRECTIVE_CLEAN_PARAM, |
||
| 31 | self::DIRECTIVE_HOST, |
||
| 32 | ]; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Directive |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $directive; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Base Uri |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $base; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * User-agent |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $userAgent; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Rule array |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $array = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Sub-directive Clean-param |
||
| 60 | * @var CleanParamParser |
||
| 61 | */ |
||
| 62 | private $cleanParam; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Sub-directive Host |
||
| 66 | * @var HostParser |
||
| 67 | */ |
||
| 68 | private $host; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * DisAllow constructor |
||
| 72 | * |
||
| 73 | * @param string $base |
||
| 74 | * @param string $userAgent |
||
| 75 | * @param string $directive |
||
| 76 | */ |
||
| 77 | public function __construct($base, $userAgent, $directive) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Add |
||
| 88 | * |
||
| 89 | * @param string $line |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function add($line) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Add plain path to allow/disallow |
||
| 106 | * |
||
| 107 | * @param string $rule |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | protected function addPath($rule) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Check |
||
| 121 | * |
||
| 122 | * @param string $url |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function check($url) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get path and query |
||
| 137 | * |
||
| 138 | * @param string $url |
||
| 139 | * @return string |
||
| 140 | * @throws Exceptions\ClientException |
||
| 141 | */ |
||
| 142 | protected function getPath($url) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Rule array |
||
| 161 | * |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function getRules() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Render |
||
| 176 | * |
||
| 177 | * @return string[] |
||
| 178 | */ |
||
| 179 | public function render() |
||
| 198 | } |
||
| 199 |
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.