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 |
||
| 18 | class MarkupValidator extends Module |
||
| 19 | { |
||
| 20 | |||
| 21 | const COMPONENT_CLASS_CONFIG_KEY = 'class'; |
||
| 22 | |||
| 23 | const COMPONENT_CONFIG_CONFIG_KEY = 'config'; |
||
| 24 | |||
| 25 | const PROVIDER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupProviderInterface'; |
||
| 26 | const PROVIDER_CONFIG_KEY = 'provider'; |
||
| 27 | |||
| 28 | const VALIDATOR_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorInterface'; |
||
| 29 | const VALIDATOR_CONFIG_KEY = 'validator'; |
||
| 30 | |||
| 31 | const FILTER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MessageFilterInterface'; |
||
| 32 | const FILTER_CONFIG_KEY = 'filter'; |
||
| 33 | |||
| 34 | const PRINTER_INTERFACE = 'Kolyunya\Codeception\Lib\MarkupValidator\MessagePrinterInterface'; |
||
| 35 | const PRINTER_CONFIG_KEY = 'printer'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritDoc} |
||
| 39 | */ |
||
| 40 | protected $config = array( |
||
| 41 | self::PROVIDER_CONFIG_KEY => array( |
||
| 42 | self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMarkupProvider', |
||
| 43 | ), |
||
| 44 | self::VALIDATOR_CONFIG_KEY => array( |
||
| 45 | self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\W3CMarkupValidator', |
||
| 46 | ), |
||
| 47 | self::FILTER_CONFIG_KEY => array( |
||
| 48 | self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessageFilter', |
||
| 49 | ), |
||
| 50 | self::PRINTER_CONFIG_KEY => array( |
||
| 51 | self::COMPONENT_CLASS_CONFIG_KEY => 'Kolyunya\Codeception\Lib\MarkupValidator\DefaultMessagePrinter', |
||
| 52 | ), |
||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Markup provider. |
||
| 57 | * |
||
| 58 | * @var MarkupProviderInterface |
||
| 59 | */ |
||
| 60 | private $markupProvider; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Markup validator. |
||
| 64 | * |
||
| 65 | * @var MarkupValidatorInterface |
||
| 66 | */ |
||
| 67 | private $markupValidator; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Message filter. |
||
| 71 | * |
||
| 72 | * @var MessageFilterInterface |
||
| 73 | */ |
||
| 74 | private $messageFilter; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Message printer. |
||
| 78 | * |
||
| 79 | * @var MessagePrinterInterface |
||
| 80 | */ |
||
| 81 | private $messagePrinter; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | */ |
||
| 86 | public function __construct(ModuleContainer $moduleContainer, $config = null) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Validates page markup via a markup validator. |
||
| 98 | * Allows to recongigure message filter component. |
||
| 99 | * |
||
| 100 | * @param array $messageFilterConfiguration Message filter configuration. |
||
| 101 | */ |
||
| 102 | public function validateMarkup(array $messageFilterConfiguration = array()) |
||
| 103 | { |
||
| 104 | $markup = $this->markupProvider->getMarkup(); |
||
| 105 | $messages = $this->markupValidator->validate($markup); |
||
| 106 | |||
| 107 | $this->messageFilter->setConfiguration($messageFilterConfiguration); |
||
| 108 | $filteredMessages = $this->messageFilter->filterMessages($messages); |
||
| 109 | |||
| 110 | if (empty($filteredMessages) === false) { |
||
| 111 | $messagesString = $this->messagePrinter->getMessagesString($filteredMessages); |
||
| 112 | $this->fail($messagesString); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Validation succeeded. |
||
| 116 | $this->assertTrue(true); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Initializes markup provider. |
||
| 121 | */ |
||
| 122 | private function initializeMarkupProvider() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Initializes markup validator. |
||
| 135 | */ |
||
| 136 | private function initializeMarkupValidator() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Initializes message filter. |
||
| 146 | */ |
||
| 147 | private function initializeMessageFilter() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Initializes message printer. |
||
| 157 | */ |
||
| 158 | private function initializeMessagePrinter() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Instantiates and returns a module component. |
||
| 168 | * |
||
| 169 | * @param string $componentName Component name. |
||
| 170 | * @param string $interface An interface component must implement. |
||
| 171 | * @param array $arguments Component's constructor arguments. |
||
| 172 | * |
||
| 173 | * @throws Exception When component does not implement expected interface. |
||
| 174 | * |
||
| 175 | * @return ComponentInterface Instance of a module component. |
||
| 176 | */ |
||
| 177 | private function instantiateComponent($componentName, $interface, array $arguments = array()) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns component class name. |
||
| 197 | * |
||
| 198 | * @param string $componentName Component name. |
||
| 199 | * |
||
| 200 | * @return string Component class name. |
||
| 201 | */ |
||
| 202 | View Code Duplication | private function getComponentClass($componentName) |
|
| 203 | { |
||
| 204 | $componentClassKey = self::COMPONENT_CLASS_CONFIG_KEY; |
||
| 205 | if (isset($this->config[$componentName][$componentClassKey]) === false || |
||
| 206 | is_string($this->config[$componentName][$componentClassKey]) === false |
||
| 207 | ) { |
||
| 208 | $errorMessage = sprintf('Invalid class configuration of component «%s».', $componentName); |
||
| 209 | throw new Exception($errorMessage); |
||
| 210 | } |
||
| 211 | |||
| 212 | $componentClass = $this->config[$componentName][$componentClassKey]; |
||
| 213 | |||
| 214 | return $componentClass; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns component configuration parameters. |
||
| 219 | * |
||
| 220 | * @param string $componentName Component name. |
||
| 221 | * |
||
| 222 | * @return array Component configuration parameters. |
||
| 223 | */ |
||
| 224 | View Code Duplication | private function getComponentConfiguration($componentName) |
|
| 240 | } |
||
| 241 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..