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 |
||
| 13 | class GenericHTMLConverter extends AbstractConverter |
||
| 14 | { |
||
|
|
|||
| 15 | /** |
||
| 16 | * String MAIN_TAG_KEY : An option key name for the list of HTML tags to use. |
||
| 17 | */ |
||
| 18 | const MAIN_TAG_KEY = 'tags'; |
||
| 19 | /** |
||
| 20 | * String TAG_KEY : An option key name for the HTML tag to use. |
||
| 21 | */ |
||
| 22 | const TAG_KEY = 'tag'; |
||
| 23 | /** |
||
| 24 | * String ATTRIBUTES_KEY : An option key name for the HTML attributes to use on the tag. |
||
| 25 | */ |
||
| 26 | const ATTRIBUTES_KEY = 'attributes'; |
||
| 27 | /** |
||
| 28 | * String MAIN_TAG_MERGE_STYLE : An option key name for a boolean to choose if document fonts and colors will be used in HTML rendering (true by default). |
||
| 29 | */ |
||
| 30 | const MAIN_TAG_MERGE_STYLE = 'merge_style'; |
||
| 31 | /** |
||
| 32 | * String MAIN_TAG_MERGE_STYLE : An option key name for a boolean to choose if texts set as bold and/or italic in the document will be set as it. |
||
| 33 | */ |
||
| 34 | const MAIN_TAG_MERGE_DECORATION = 'merge_decoration'; |
||
| 35 | /** |
||
| 36 | * String MAIN_ICON_KEY : An option key name for the icon tag to use. |
||
| 37 | */ |
||
| 38 | const MAIN_ICON_KEY = 'icon'; |
||
| 39 | /** |
||
| 40 | * String DISPLAY_ICON_KEY : A boolean to determine if for nodes with an icon, the icon will be added in a img tag before the text. |
||
| 41 | */ |
||
| 42 | const DISPLAY_ICON_KEY = 'display'; |
||
| 43 | /** |
||
| 44 | * String PATH_ICON_KEY : Key for options to generate the icon path. |
||
| 45 | */ |
||
| 46 | const PATH_ICON_KEY = 'path'; |
||
| 47 | /** |
||
| 48 | * String CALLBACK_PATH_ICON_KEY : A callback to generate the icon uri as wished. |
||
| 49 | * Its first parameter is the icon full name (with extension (i.e : 'button_ok.png')). |
||
| 50 | * Signature : function($fullName, $options = null); |
||
| 51 | */ |
||
| 52 | const CALLBACK_PATH_ICON_KEY = 'callback'; |
||
| 53 | /** |
||
| 54 | * String OPTIONS_PATH_ICON_KEY : An array of options given as a second parameter to the callback. |
||
| 55 | */ |
||
| 56 | const OPTIONS_PATH_ICON_KEY = 'options'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Call the conversion over the Document instance. |
||
| 60 | * |
||
| 61 | * @var Document $document : The document instance to convert. |
||
| 62 | * @var Array $options : The options for the conversion. |
||
| 63 | * It must follow this filling : |
||
| 64 | * [ |
||
| 65 | * MAIN_TAG_KEY => |
||
| 66 | * [ |
||
| 67 | * TAG_KEY => 'First wrapping HTML tag to use (ul, div, ...)', |
||
| 68 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 69 | * ], |
||
| 70 | * [ |
||
| 71 | * TAG_KEY => 'Second wrapping HTML tag to use (ul, div, ...)', |
||
| 72 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 73 | * ], |
||
| 74 | * [ |
||
| 75 | * TAG_KEY => 'Third wrapping HTML tag to use (ul, div, ...) (optionnal)', |
||
| 76 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 77 | * ], |
||
| 78 | * MAIN_TAG_MERGE_STYLE => A boolean to choose if document fonts and colors will be used in HTML rendering (true by default), |
||
| 79 | * MAIN_TAG_MERGE_DECORATION => A boolean to choose if texts set as bold and/or italic in the document will be set as it. (true by default), |
||
| 80 | * MAIN_ICON_KEY => [ |
||
| 81 | * DISPLAY_ICON_KEY => boolean determining if the icons must be displayed or not, |
||
| 82 | * PATH_ICON_KEY => [ |
||
| 83 | * 'CALLBACK_PATH_ICON_KEY' => 'the callback to use to determine uri. Its first parameter will be the file full name, and the second an optional array with additional parametrs.', |
||
| 84 | * 'OPTIONS_PATH_ICON_KEY' => [array of options for the callback], |
||
| 85 | * ], |
||
| 86 | * ], |
||
| 87 | * ]. The first two are mandatory, and the style stored in the Node instance (color, font name and font size) will be applied on the second tag). |
||
| 88 | * |
||
| 89 | * @return DOMDocument $domDocument : The DOMDocument instance created with the HTML. |
||
| 90 | */ |
||
| 91 | protected function convertFromDocumentInstance(Document $document, array $options) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Perform the conversion over the Document instance. |
||
| 101 | * Each text will be wrap in at least two HTML tags (<ul><li>, <div><span>, ...) and at most three tags (<ul><li><span>, ...). |
||
| 102 | * The style stored in the Node instance (color, font name and font size) will be applied on the second tag and override the "style" attribute if one is given. |
||
| 103 | * |
||
| 104 | * @var Document $document : The document instance to convert. |
||
| 105 | * @var Array $options : The options for the conversion. |
||
| 106 | * It must follow this filling : |
||
| 107 | * [ |
||
| 108 | * MAIN_TAG_KEY => |
||
| 109 | * [ |
||
| 110 | * TAG_KEY => 'First wrapping HTML tag to use (ul, div, ...)', |
||
| 111 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 112 | * ], |
||
| 113 | * [ |
||
| 114 | * TAG_KEY => 'Second wrapping HTML tag to use (ul, div, ...)', |
||
| 115 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 116 | * ], |
||
| 117 | * [ |
||
| 118 | * TAG_KEY => 'Third wrapping HTML tag to use (ul, div, ...) (optionnal)', |
||
| 119 | * ATTRIBUTES_KEY => ['attribute name (class, id, href, ...)' => 'attribute value', ...] |
||
| 120 | * ], |
||
| 121 | * MAIN_TAG_MERGE_STYLE => A boolean to choose if document fonts and colors will be used in HTML rendering (true by default), |
||
| 122 | * MAIN_TAG_MERGE_DECORATION => A boolean to choose if texts set as bold and/or italic in the document will be set as it. (true by default), |
||
| 123 | * MAIN_ICON_KEY => [ |
||
| 124 | * DISPLAY_ICON_KEY => boolean determining if the icons must be displayed or not, |
||
| 125 | * PATH_ICON_KEY => [ |
||
| 126 | * 'CALLBACK_PATH_ICON_KEY' => 'the callback to use to determine uri. Its first parameter will be the file full name, and the second an optional array with additional parametrs.', |
||
| 127 | * 'OPTIONS_PATH_ICON_KEY' => [array of options for the callback], |
||
| 128 | * ], |
||
| 129 | * ], |
||
| 130 | * ]. The first two are mandatory. |
||
| 131 | * |
||
| 132 | * @return DOMDocument $domDocument : The DOMDocument instance created with the HTML. |
||
| 133 | */ |
||
| 134 | private function buildHTMLTreeFromNode(DOMDocument $document, Node $node, array $options) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Build the HTML tag following its given tag name and attributes stored in $description array. |
||
| 220 | * |
||
| 221 | * @param DOMDocument $document : The current DOMDocument in creation with the HTML tree. |
||
| 222 | * @param array $description : The array describing the tag to create. |
||
| 223 | * |
||
| 224 | * @return DOMElement $domElement : The created DOMElement. |
||
| 225 | */ |
||
| 226 | private function buildElement(DOMDocument $document, array $description) { |
||
| 234 | } |