| Total Complexity | 68 |
| Total Lines | 260 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MarkdownTestHelper 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.
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 MarkdownTestHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class MarkdownTestHelper |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * Takes an input directory containing .text and .(x)html files, and returns an array |
||
| 9 | * of .text files and the corresponding output xhtml or html file. Can be used in a unit test data provider. |
||
| 10 | * |
||
| 11 | * @param string $directory Input directory |
||
| 12 | * |
||
| 13 | * @return array |
||
| 14 | */ |
||
| 15 | public static function getInputOutputPaths($directory) { |
||
| 16 | $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
||
| 17 | $regexIterator = new RegexIterator( |
||
| 18 | $iterator, |
||
| 19 | '/^.+\.text$/', |
||
| 20 | RecursiveRegexIterator::GET_MATCH |
||
| 21 | ); |
||
| 22 | |||
| 23 | $dataValues = []; |
||
| 24 | |||
| 25 | /** @var SplFileInfo $inputFile */ |
||
| 26 | foreach ($regexIterator as $inputFiles) { |
||
| 27 | foreach ($inputFiles as $inputMarkdownPath) { |
||
| 28 | $xhtml = true; |
||
| 29 | $expectedHtmlPath = substr($inputMarkdownPath, 0, -4) . 'xhtml'; |
||
| 30 | if (!file_exists($expectedHtmlPath)) { |
||
| 31 | $expectedHtmlPath = substr($inputMarkdownPath, 0, -4) . 'html'; |
||
| 32 | $xhtml = false; |
||
| 33 | } |
||
| 34 | $dataValues[] = [$inputMarkdownPath, $expectedHtmlPath, $xhtml]; |
||
| 35 | } |
||
| 36 | } |
||
| 37 | |||
| 38 | return $dataValues; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Applies PHPUnit's assertSame after normalizing both strings (e.g. ignoring whitespace differences). |
||
| 43 | * Uses logic found originally in MDTest. |
||
| 44 | * |
||
| 45 | * @param string $string1 |
||
| 46 | * @param string $string2 |
||
| 47 | * @param string $message Positive message to print when test fails (e.g. "String1 matches String2") |
||
| 48 | * @param bool $xhtml |
||
| 49 | */ |
||
| 50 | public static function assertSameNormalized($string1, $string2, $message, $xhtml = true) { |
||
| 51 | |||
| 52 | $t_result = $string1; |
||
| 53 | $t_output = $string2; |
||
| 54 | |||
| 55 | // DOMDocuments |
||
| 56 | if ($xhtml) { |
||
| 57 | $document = new DOMDocument(); |
||
| 58 | $doc_result = $document->loadXML('<!DOCTYPE html>' . |
||
| 59 | "<html xmlns='http://www.w3.org/1999/xhtml'>" . |
||
| 60 | "<body>$t_result</body></html>"); |
||
| 61 | |||
| 62 | $document2 = new DOMDocument(); |
||
| 63 | $doc_output = $document2->loadXML('<!DOCTYPE html>' . |
||
| 64 | "<html xmlns='http://www.w3.org/1999/xhtml'>" . |
||
| 65 | "<body>$t_output</body></html>"); |
||
| 66 | |||
| 67 | if ($doc_result) { |
||
| 68 | static::normalizeElementContent($document->documentElement, false); |
||
| 69 | $n_result = $document->saveXML(); |
||
| 70 | } else { |
||
| 71 | $n_result = '--- Expected Result: XML Parse Error ---'; |
||
| 72 | } |
||
| 73 | if ($doc_output) { |
||
| 74 | static::normalizeElementContent($document2->documentElement, false); |
||
| 75 | $n_output = $document2->saveXML(); |
||
| 76 | } else { |
||
| 77 | $n_output = '--- Output: XML Parse Error ---'; |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | |||
| 81 | // '@' suppressors used because some tests have invalid HTML (multiple elements with the same id attribute) |
||
| 82 | // Perhaps isolate to a separate test and remove this? |
||
| 83 | |||
| 84 | $document = new DOMDocument(); |
||
| 85 | $doc_result = @$document->loadHTML($t_result); |
||
| 86 | |||
| 87 | $document2 = new DOMDocument(); |
||
| 88 | $doc_output = @$document2->loadHTML($t_output); |
||
| 89 | |||
| 90 | if ($doc_result) { |
||
| 91 | static::normalizeElementContent($document->documentElement, false); |
||
| 92 | $n_result = $document->saveHTML(); |
||
| 93 | } else { |
||
| 94 | $n_result = '--- Expected Result: HTML Parse Error ---'; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($doc_output) { |
||
| 98 | static::normalizeElementContent($document2->documentElement, false); |
||
| 99 | $n_output = $document2->saveHTML(); |
||
| 100 | } else { |
||
| 101 | $n_output = '--- Output: HTML Parse Error ---'; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | $n_result = preg_replace('{^.*?<body>|</body>.*?$}is', '', $n_result); |
||
| 106 | $n_output = preg_replace('{^.*?<body>|</body>.*?$}is', '', $n_output); |
||
| 107 | |||
| 108 | $c_result = $n_result; |
||
| 109 | $c_output = $n_output; |
||
| 110 | |||
| 111 | $c_result = trim($c_result) . "\n"; |
||
| 112 | $c_output = trim($c_output) . "\n"; |
||
| 113 | |||
| 114 | // This will throw a test exception if the strings don't exactly match |
||
| 115 | TestCase::assertSame($c_result, $c_output, $message); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param DOMElement $element Modifies this element by reference |
||
| 120 | * @param bool $whitespace_preserve Preserve Whitespace |
||
| 121 | * @return void |
||
| 122 | */ |
||
| 123 | protected static function normalizeElementContent($element, $whitespace_preserve) { |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param DOMElement $element Modifies this element by reference |
||
| 246 | */ |
||
| 247 | protected static function normalizeElementAttributes (DOMElement $element) |
||
| 265 | } |
||
| 266 | } |
||
| 268 |