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 |
||
49 | class WhitespaceTokenizer implements Tokenizer |
||
50 | { |
||
51 | protected $whitespaces = array( |
||
52 | '\s', // white space |
||
53 | "\xE2\x80\xAF", // non-breaking thin white space |
||
54 | "\xC2\xA0", // non-breaking space |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Split the given input into tokens using whitespace as splitter |
||
59 | * |
||
60 | * The input can be a string or a tokenRegistry. If the input is a |
||
61 | * TokenRegistry, each item will be tokenized. |
||
62 | * |
||
63 | * @param string|\Org\Heigl\Hyphenator\Tokenizer\TokenRegistry $input The |
||
64 | * input to be tokenized |
||
65 | * |
||
66 | * @return \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry |
||
67 | */ |
||
68 | View Code Duplication | public function run($input) |
|
|
|||
69 | { |
||
70 | if ($input instanceof TokenRegistry) { |
||
71 | // Tokenize a TokenRegistry |
||
72 | foreach ($input as $token) { |
||
73 | if (! $token instanceof WordToken) { |
||
74 | continue; |
||
75 | } |
||
76 | $newTokens = $this->_tokenize($token->get()); |
||
77 | if ($newTokens == array($token)) { |
||
78 | continue; |
||
79 | } |
||
80 | $input->replace($token, $newTokens); |
||
81 | } |
||
82 | |||
83 | return $input ; |
||
84 | } |
||
85 | |||
86 | // Tokenize a simple string. |
||
87 | $array = $this->_tokenize($input); |
||
88 | $registry = new TokenRegistry(); |
||
89 | foreach ($array as $item) { |
||
90 | $registry->add($item); |
||
91 | } |
||
92 | |||
93 | return $registry; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Split the given string into tokens using whitespace. |
||
98 | * |
||
99 | * Each whitespace is placed in a WhitespaceToken and everything else is |
||
100 | * placed in a WordToken-Object |
||
101 | * |
||
102 | * @param string $input The String to tokenize |
||
103 | * |
||
104 | * @return Token |
||
105 | */ |
||
106 | protected function _tokenize($input) |
||
121 | } |
||
122 |
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.