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 |
||
| 32 | abstract class TestCase extends \PHPUnit\Framework\TestCase { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Set up Brain Monkey. |
||
| 36 | */ |
||
| 37 | protected function setUp() { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Tear down Brain Monkey. |
||
| 44 | */ |
||
| 45 | protected function tearDown() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Call protected/private method of a class. |
||
| 52 | * |
||
| 53 | * @param object $object Instantiated object that we will run method on. |
||
| 54 | * @param string $method_name Method name to call. |
||
| 55 | * @param array $parameters Array of parameters to pass into method. |
||
| 56 | * @param string $classname Optional. The class to use for accessing private properties. |
||
| 57 | * |
||
| 58 | * @return mixed Method return. |
||
| 59 | */ |
||
| 60 | protected function invokeMethod( $object, $method_name, array $parameters = [], $classname = '' ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Call protected/private method of a class. |
||
| 74 | * |
||
| 75 | * @param string $classname A class that we will run the method on. |
||
| 76 | * @param string $method_name Method name to call. |
||
| 77 | * @param array $parameters Array of parameters to pass into method. |
||
| 78 | * |
||
| 79 | * @return mixed Method return. |
||
| 80 | */ |
||
| 81 | protected function invokeStaticMethod( $classname, $method_name, array $parameters = [] ) { |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Sets the value of a private/protected property of a class. |
||
| 91 | * |
||
| 92 | * @param string $classname A class whose property we will access. |
||
| 93 | * @param string $property_name Property to set. |
||
| 94 | * @param mixed|null $value The new value. |
||
| 95 | */ |
||
| 96 | View Code Duplication | protected function setStaticValue( $classname, $property_name, $value ) { |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Sets the value of a private/protected property of a class. |
||
| 105 | * |
||
| 106 | * @param object $object Instantiated object that we will run method on. |
||
| 107 | * @param string $property_name Property to set. |
||
| 108 | * @param mixed|null $value The new value. |
||
| 109 | * @param string $classname Optional. The class to use for accessing private properties. |
||
| 110 | */ |
||
| 111 | protected function setValue( $object, $property_name, $value, $classname = '' ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Retrieves the value of a private/protected property of a class. |
||
| 124 | * |
||
| 125 | * @param string $classname A class whose property we will access. |
||
| 126 | * @param string $property_name Property to set. |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | View Code Duplication | protected function getStaticValue( $classname, $property_name ) { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Retrieves the value of a private/protected property of a class. |
||
| 140 | * |
||
| 141 | * @param object $object Instantiated object that we will run method on. |
||
| 142 | * @param string $property_name Property to set. |
||
| 143 | * @param string $classname Optional. The class to use for accessing private properties. |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | View Code Duplication | protected function getValue( $object, $property_name, $classname = '' ) { |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Reports an error identified by $message if $attribute in $object does not have the $key. |
||
| 161 | * |
||
| 162 | * @param string $key The array key. |
||
| 163 | * @param string $attribute The attribute name. |
||
| 164 | * @param object $object The object. |
||
| 165 | * @param string $message Optional. Default ''. |
||
| 166 | */ |
||
| 167 | View Code Duplication | protected function assertAttributeArrayHasKey( $key, $attribute, $object, $message = '' ) { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Reports an error identified by $message if $attribute in $object does have the $key. |
||
| 177 | * |
||
| 178 | * @param string $key The array key. |
||
| 179 | * @param string $attribute The attribute name. |
||
| 180 | * @param object $object The object. |
||
| 181 | * @param string $message Optional. Default ''. |
||
| 182 | */ |
||
| 183 | View Code Duplication | protected function assertAttributeArrayNotHasKey( $key, $attribute, $object, $message = '' ) { |
|
| 190 | } |
||
| 191 |
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.