| Total Complexity | 50 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Nested 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 Nested, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Nested extends Formatter |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | private $depth; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * {@inheritdoc} |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 35 | { |
||
| 36 | $this->indentLevel = 0; |
||
| 37 | $this->indentChar = ' '; |
||
| 38 | $this->break = "\n"; |
||
| 39 | $this->open = ' {'; |
||
| 40 | $this->close = ' }'; |
||
| 41 | $this->tagSeparator = ', '; |
||
| 42 | $this->assignSeparator = ': '; |
||
| 43 | $this->keepSemicolons = true; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | protected function indentStr() |
||
| 50 | { |
||
| 51 | $n = $this->depth - 1; |
||
| 52 | |||
| 53 | return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | protected function blockLines(OutputBlock $block) |
||
| 60 | { |
||
| 61 | $inner = $this->indentStr(); |
||
| 62 | $glue = $this->break . $inner; |
||
| 63 | |||
| 64 | foreach ($block->lines as $index => $line) { |
||
| 65 | if (substr($line, 0, 2) === '/*') { |
||
| 66 | $block->lines[$index] = preg_replace('/\r\n?|\n|\f/', $this->break, $line); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->write($inner . implode($glue, $block->lines)); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | protected function block(OutputBlock $block) |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Block has flat child |
||
| 216 | * |
||
| 217 | * @param \ScssPhp\ScssPhp\Formatter\OutputBlock $block |
||
| 218 | * |
||
| 219 | * @return boolean |
||
| 220 | */ |
||
| 221 | private function hasFlatChild($block) |
||
| 232 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.