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:
Complex classes like PfPageparser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PfPageparser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class PfPageparser |
||
10 | { |
||
11 | // Build your next great package. |
||
12 | private $config; |
||
13 | private $content=""; |
||
14 | private $chunks=[]; |
||
15 | private $parsed=[]; |
||
16 | private $logger=null; |
||
17 | |||
18 | public function __construct(array $config=[], LoggerInterface $logger=null){ |
||
34 | |||
35 | public function get_config(){ |
||
38 | |||
39 | /* ------------------------------------------ |
||
40 | * LOADING THE CONTENT FROM A URL/FILE/STRING |
||
41 | */ |
||
42 | |||
43 | private function initialize(){ |
||
48 | |||
49 | /** |
||
50 | * @param string $url |
||
51 | * @param array $options |
||
52 | * @return $this |
||
53 | */ |
||
54 | public function load_from_url(string $url, array $options=[]): PfPageparser |
||
71 | |||
72 | public function load_from_file(string $filename): PfPageparser |
||
80 | |||
81 | public function load_fom_string(string $string): PfPageparser |
||
87 | |||
88 | public function cleanup_html($remove_linefeeds=true,$shrink_spaces=true ): PfPageparser |
||
94 | |||
95 | /* ------------------------------------------ |
||
96 | * GET THE RAW CONTENT |
||
97 | */ |
||
98 | public function get_content():string |
||
100 | |||
101 | public function raw(): string |
||
103 | |||
104 | /* ------------------------------------------ |
||
105 | * MODIFY THE RAW CONTENT |
||
106 | */ |
||
107 | |||
108 | public function trim_before(string $pattern,bool $is_regex=false): PfPageparser |
||
114 | |||
115 | public function trim_after(string $pattern,bool $is_regex=false): PfPageparser |
||
122 | |||
123 | public function trim(string $before="<body",string $after="</body",bool $is_regex=false): PfPageparser |
||
129 | |||
130 | /* ------------------------------------------ |
||
131 | * RAW CONTENT => CHUNKS |
||
132 | */ |
||
133 | |||
134 | /** |
||
135 | * @param $pattern |
||
136 | * @param $is_regex |
||
137 | * @return $this |
||
138 | * split the HTML content into chunks based on a text or regex separator |
||
139 | */ |
||
140 | |||
141 | public function split_chunks(string $pattern,bool $is_regex=false): PfPageparser |
||
162 | |||
163 | /** |
||
164 | * @param array $pattern_keep |
||
165 | * @param array $pattern_remove |
||
166 | * @param bool $is_regex |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function filter_chunks(array $pattern_keep=[], array $pattern_remove=[], bool $is_regex=false): PfPageparser |
||
211 | |||
212 | /** |
||
213 | * @param string $pattern |
||
214 | * @param bool $only_one |
||
215 | * @param bool $restart |
||
216 | * @return PfPageparser |
||
217 | */ |
||
218 | public function parse_fom_chunks(string $pattern,bool $only_one=false, bool $restart=false): PfPageparser |
||
249 | |||
250 | public function get_chunks(): array |
||
254 | |||
255 | public function results(bool $before_parsing=false): array |
||
263 | |||
264 | private function log(string $text,string $level = 'info') |
||
270 | |||
271 | } |
||
272 |
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.