Skip to content

Commit ceadeab

Browse files
committed
Formatter: factoring out blockChildren()
1 parent ad98752 commit ceadeab

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

src/Formatter.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ protected function blockSelectors($block)
121121
. $this->open . $this->break;
122122
}
123123

124+
/**
125+
* Output block children
126+
*
127+
* @param \stdClass $block
128+
*/
129+
protected function blockChildren($block)
130+
{
131+
foreach ($block->children as $child) {
132+
$this->block($child);
133+
}
134+
}
135+
124136
/**
125137
* Output non-empty block
126138
*
@@ -144,8 +156,8 @@ protected function block($block)
144156
$this->blockLines($block);
145157
}
146158

147-
foreach ($block->children as $child) {
148-
$this->block($child);
159+
if (! empty($block->children)) {
160+
$this->blockChildren($block);
149161
}
150162

151163
if (! empty($block->selectors)) {

src/Formatter/Debug.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public function __construct()
3939
*/
4040
protected function blockLines($block)
4141
{
42+
if (empty($block->lines)) {
43+
echo "block->lines: []\n";
44+
45+
return;
46+
}
47+
4248
foreach ($block->lines as $index => $line) {
4349
echo "block->lines[{$index}]: $line\n";
4450
}
@@ -49,6 +55,12 @@ protected function blockLines($block)
4955
*/
5056
protected function blockSelectors($block)
5157
{
58+
if (empty($block->selectors)) {
59+
echo "block->selectors: []\n";
60+
61+
return;
62+
}
63+
5264
foreach ($block->selectors as $index => $selector) {
5365
echo "block->selectors[{$index}]: $selector\n";
5466
}
@@ -57,21 +69,29 @@ protected function blockSelectors($block)
5769
/**
5870
* {@inheritdoc}
5971
*/
60-
protected function block($block)
72+
protected function blockChildren($block)
6173
{
62-
echo "block->type: {$block->type}\n" .
63-
"block->depth: {$block->depth}\n";
64-
65-
if (! empty($block->selectors)) {
66-
$this->blockSelectors($block);
67-
}
74+
if (empty($block->children)) {
75+
echo "block->children: []\n";
6876

69-
if (! empty($block->lines)) {
70-
$this->blockLines($block);
77+
return;
7178
}
7279

7380
foreach ($block->children as $i => $child) {
7481
$this->block($child);
7582
}
7683
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
protected function block($block)
89+
{
90+
echo "block->type: {$block->type}\n" .
91+
"block->depth: {$block->depth}\n";
92+
93+
$this->blockSelectors($block);
94+
$this->blockLines($block);
95+
$this->blockChildren($block);
96+
}
7797
}

src/Formatter/Nested.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ protected function blockSelectors($block)
6868
. $this->open . $this->break;
6969
}
7070

71+
/**
72+
* {@inheritdoc}
73+
*/
74+
protected function blockChildren($block)
75+
{
76+
foreach ($block->children as $i => $child) {
77+
$this->block($child);
78+
79+
if ($i < count($block->children) - 1) {
80+
echo $this->break;
81+
82+
if (isset($block->children[$i + 1])) {
83+
$next = $block->children[$i + 1];
84+
85+
if ($next->depth === max($block->depth, 1) && $child->depth >= $next->depth) {
86+
echo $this->break;
87+
}
88+
}
89+
}
90+
}
91+
}
92+
7193
/**
7294
* {@inheritdoc}
7395
*/
@@ -87,20 +109,8 @@ protected function block($block)
87109
$this->blockLines($block);
88110
}
89111

90-
foreach ($block->children as $i => $child) {
91-
$this->block($child);
92-
93-
if ($i < count($block->children) - 1) {
94-
echo $this->break;
95-
96-
if (isset($block->children[$i + 1])) {
97-
$next = $block->children[$i + 1];
98-
99-
if ($next->depth === max($block->depth, 1) && $child->depth >= $next->depth) {
100-
echo $this->break;
101-
}
102-
}
103-
}
112+
if (! empty($block->children)) {
113+
$this->blockChildren($block);
104114
}
105115

106116
if (! empty($block->selectors)) {

0 commit comments

Comments
 (0)