We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
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 PHP_Typography_Testcase 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 PHP_Typography_Testcase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | abstract class PHP_Typography_Testcase extends \PHPUnit\Framework\TestCase { |
||
| 33 | /** |
||
| 34 | * Return encoded HTML string (everything except <>"'). |
||
| 35 | * |
||
| 36 | * @param string $html A HTML fragment. |
||
| 37 | */ |
||
| 38 | protected function clean_html( $html ) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Call protected/private method of a class. |
||
| 54 | * |
||
| 55 | * @param object $object Instantiated object that we will run method on. |
||
| 56 | * @param string $method_name Method name to call. |
||
| 57 | * @param array $parameters Array of parameters to pass into method. |
||
| 58 | * |
||
| 59 | * @return mixed Method return. |
||
| 60 | */ |
||
| 61 | View Code Duplication | protected function invokeMethod( $object, $method_name, array $parameters = [] ) { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Call protected/private method of a class. |
||
| 71 | * |
||
| 72 | * @param string $classname A class that we will run the method on. |
||
| 73 | * @param string $method_name Method name to call. |
||
| 74 | * @param array $parameters Array of parameters to pass into method. |
||
| 75 | * |
||
| 76 | * @return mixed Method return. |
||
| 77 | */ |
||
| 78 | View Code Duplication | protected function invokeStaticMethod( $classname, $method_name, array $parameters = [] ) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Sets the value of a private/protected property of a class. |
||
| 88 | * |
||
| 89 | * @param string $classname A class whose property we will access. |
||
| 90 | * @param string $property_name Property to set. |
||
| 91 | * @param mixed|null $value The new value. |
||
| 92 | */ |
||
| 93 | View Code Duplication | protected function setStaticValue( $classname, $property_name, $value ) { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Sets the value of a private/protected property of a class. |
||
| 102 | * |
||
| 103 | * @param object $object Instantiated object that we will run method on. |
||
| 104 | * @param string $property_name Property to set. |
||
| 105 | * @param mixed|null $value The new value. |
||
| 106 | */ |
||
| 107 | View Code Duplication | protected function setValue( $object, $property_name, $value ) { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Retrieves the value of a private/protected property of a class. |
||
| 116 | * |
||
| 117 | * @param string $classname A class whose property we will access. |
||
| 118 | * @param string $property_name Property to set. |
||
| 119 | * |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | View Code Duplication | protected function getStaticValue( $classname, $property_name ) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Retrieves the value of a private/protected property of a class. |
||
| 132 | * |
||
| 133 | * @param object $object Instantiated object that we will run method on. |
||
| 134 | * @param string $property_name Property to set. |
||
| 135 | * |
||
| 136 | * @return mixed |
||
| 137 | */ |
||
| 138 | View Code Duplication | protected function getValue( $object, $property_name ) { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Helper function to generate a valid token list from strings. |
||
| 148 | * |
||
| 149 | * @param string $value The string to tokenize. |
||
| 150 | * @param string $type Optional. Default 'word'. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function tokenize( $value, $type = \PHP_Typography\Text_Parser\Token::WORD ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Helper function to generate a valid word token list from strings. |
||
| 162 | * |
||
| 163 | * @param string $value Token value. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function tokenize_sentence( $value ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Reports an error identified by $message if the combined token values differ from the expected value. |
||
| 180 | * |
||
| 181 | * @param string|array $expected_value Either a word/sentence or a token array. |
||
| 182 | * @param array $actual_tokens A token array. |
||
| 183 | * @param string $message Optional. Default ''. |
||
| 184 | */ |
||
| 185 | protected function assertTokensSame( $expected_value, array $actual_tokens, $message = '' ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Reports an error identified by $message if the combined token values do |
||
| 217 | * not differ from the expected value. |
||
| 218 | * |
||
| 219 | * @param string|array $expected_value Either a word/sentence or a token array. |
||
| 220 | * @param array $actual_tokens A token array. |
||
| 221 | * @param string $message Optional. Default ''. |
||
| 222 | */ |
||
| 223 | protected function assertTokensNotSame( $expected_value, array $actual_tokens, $message = '' ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Reports an error identified by $message if $attribute in $object does not have the $key. |
||
| 254 | * |
||
| 255 | * @param string $key The array key. |
||
| 256 | * @param string $attribute The attribute name. |
||
| 257 | * @param object $object The object. |
||
| 258 | * @param string $message Optional. Default ''. |
||
| 259 | */ |
||
| 260 | View Code Duplication | protected function assertAttributeArrayHasKey( $key, $attribute, $object, $message = '' ) { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Reports an error identified by $message if $attribute in $object does have the $key. |
||
| 270 | * |
||
| 271 | * @param string $key The array key. |
||
| 272 | * @param string $attribute The attribute name. |
||
| 273 | * @param object $object The object. |
||
| 274 | * @param string $message Optional. Default ''. |
||
| 275 | */ |
||
| 276 | View Code Duplication | protected function assertAttributeArrayNotHasKey( $key, $attribute, $object, $message = '' ) { |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Assert that the given quote styles match. |
||
| 286 | * |
||
| 287 | * @param string $style Style name. |
||
| 288 | * @param string $open Opening quote character. |
||
| 289 | * @param string $close Closing quote character. |
||
| 290 | */ |
||
| 291 | protected function assertSmartQuotesStyle( $style, $open, $close ) { |
||
| 372 | } |
||
| 373 |
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.