| Total Complexity | 46 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like StrictUnifiedDiffOutputBuilder 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 StrictUnifiedDiffOutputBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 21 | final class StrictUnifiedDiffOutputBuilder implements DiffOutputBuilderInterface |
||
| 22 | { |
||
| 23 | private static $default = [ |
||
| 24 | 'collapseRanges' => true, // ranges of length one are rendered with the trailing `,1` |
||
| 25 | 'commonLineThreshold' => 6, // number of same lines before ending a new hunk and creating a new one (if needed) |
||
| 26 | 'contextLines' => 3, // like `diff: -u, -U NUM, --unified[=NUM]`, for patch/git apply compatibility best to keep at least @ 3 |
||
| 27 | 'fromFile' => null, |
||
| 28 | 'fromFileDate' => null, |
||
| 29 | 'toFile' => null, |
||
| 30 | 'toFileDate' => null, |
||
| 31 | ]; |
||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | private $changed; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $collapseRanges; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int >= 0 |
||
| 44 | */ |
||
| 45 | private $commonLineThreshold; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $header; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int >= 0 |
||
| 54 | */ |
||
| 55 | private $contextLines; |
||
| 56 | |||
| 57 | public function __construct(array $options = []) |
||
| 58 | { |
||
| 59 | $options = \array_merge(self::$default, $options); |
||
| 60 | |||
| 61 | if (!\is_bool($options['collapseRanges'])) { |
||
| 62 | throw new ConfigurationException('collapseRanges', 'a bool', $options['collapseRanges']); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (!\is_int($options['contextLines']) || $options['contextLines'] < 0) { |
||
| 66 | throw new ConfigurationException('contextLines', 'an int >= 0', $options['contextLines']); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (!\is_int($options['commonLineThreshold']) || $options['commonLineThreshold'] <= 0) { |
||
| 70 | throw new ConfigurationException('commonLineThreshold', 'an int > 0', $options['commonLineThreshold']); |
||
| 71 | } |
||
| 72 | |||
| 73 | foreach (['fromFile', 'toFile'] as $option) { |
||
| 74 | if (!\is_string($options[$option])) { |
||
| 75 | throw new ConfigurationException($option, 'a string', $options[$option]); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | foreach (['fromFileDate', 'toFileDate'] as $option) { |
||
| 80 | if (null !== $options[$option] && !\is_string($options[$option])) { |
||
| 81 | throw new ConfigurationException($option, 'a string or <null>', $options[$option]); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->header = \sprintf( |
||
| 86 | "--- %s%s\n+++ %s%s\n", |
||
| 87 | $options['fromFile'], |
||
| 88 | null === $options['fromFileDate'] ? '' : "\t" . $options['fromFileDate'], |
||
| 89 | $options['toFile'], |
||
| 90 | null === $options['toFileDate'] ? '' : "\t" . $options['toFileDate'] |
||
| 91 | ); |
||
| 92 | |||
| 93 | $this->collapseRanges = $options['collapseRanges']; |
||
| 94 | $this->commonLineThreshold = $options['commonLineThreshold']; |
||
| 95 | $this->contextLines = $options['contextLines']; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getDiff(array $diff): string |
||
| 128 | ; |
||
| 129 | } |
||
| 130 | |||
| 131 | private function writeDiffHunks($output, array $diff): void |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | private function writeHunk( |
||
| 319 |