Skip to content

Commit 954f8dc

Browse files
committed
Bug fix switch statement without default clause.
1 parent 120d3b3 commit 954f8dc

File tree

2 files changed

+74
-47
lines changed

2 files changed

+74
-47
lines changed

src/PhpCodeStore.php

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class PhpCodeStore extends CodeStore
1010
{
1111
//--------------------------------------------------------------------------------------------------------------------
1212
/**
13-
* The levels of nested switch statements.
13+
* The levels of nested default clauses.
1414
*
1515
* @var int[]
1616
*/
17-
private $switchLevel = [];
17+
private $defaultLevel = [];
1818

1919
//--------------------------------------------------------------------------------------------------------------------
2020
/**
@@ -48,6 +48,39 @@ protected function indentationMode(string $line): int
4848
return $mode;
4949
}
5050

51+
//--------------------------------------------------------------------------------------------------------------------
52+
/**
53+
* Decrements indent level of the current switch statement (if any).
54+
*/
55+
private function defaultLevelDecrement(): void
56+
{
57+
if (!empty($this->defaultLevel) && $this->defaultLevel[sizeof($this->defaultLevel) - 1]>0)
58+
{
59+
$this->defaultLevel[sizeof($this->defaultLevel) - 1]--;
60+
}
61+
}
62+
63+
//--------------------------------------------------------------------------------------------------------------------
64+
/**
65+
* Increments indent level of the current switch statement (if any).
66+
*/
67+
private function defaultLevelIncrement(): void
68+
{
69+
if (!empty($this->defaultLevel))
70+
{
71+
$this->defaultLevel[sizeof($this->defaultLevel) - 1]++;
72+
}
73+
}
74+
75+
//--------------------------------------------------------------------------------------------------------------------
76+
/**
77+
* Returns true if the indent level of the current switch statement (if any) is zero. Otherwise, returns false.
78+
*/
79+
private function defaultLevelIsZero(): bool
80+
{
81+
return (!empty($this->defaultLevel) && $this->defaultLevel[sizeof($this->defaultLevel) - 1]==0);
82+
}
83+
5184
//--------------------------------------------------------------------------------------------------------------------
5285
/**
5386
* Returns the indentation mode based blocks of code.
@@ -56,26 +89,26 @@ protected function indentationMode(string $line): int
5689
*
5790
* @return int
5891
*/
59-
protected function indentationModeBlock(string $line): int
92+
private function indentationModeBlock(string $line): int
6093
{
6194
$mode = 0;
6295

6396
if (substr($line, -1, 1)=='{')
6497
{
6598
$mode |= self::C_INDENT_INCREMENT_AFTER;
6699

67-
$this->switchLevelIncrement();
100+
$this->defaultLevelIncrement();
68101
}
69102

70103
if (substr($line, 0, 1)=='}')
71104
{
72-
$this->switchLevelDecrement();
105+
$this->defaultLevelDecrement();
73106

74-
if ($this->switchLevelIsZero())
107+
if ($this->defaultLevelIsZero())
75108
{
76109
$mode |= self::C_INDENT_DECREMENT_BEFORE_DOUBLE;
77110

78-
array_pop($this->switchLevel);
111+
array_pop($this->defaultLevel);
79112
}
80113
else
81114
{
@@ -98,18 +131,15 @@ private function indentationModeSwitch(string $line): int
98131
{
99132
$mode = 0;
100133

101-
if (substr($line, 0, 7)=='switch ')
102-
{
103-
$this->switchLevel[] = 0;
104-
}
105-
106134
if (substr($line, 0, 5)=='case ')
107135
{
108136
$mode |= self::C_INDENT_INCREMENT_AFTER;
109137
}
110138

111139
if (substr($line, 0, 8)=='default:')
112140
{
141+
$this->defaultLevel[] = 0;
142+
113143
$mode |= self::C_INDENT_INCREMENT_AFTER;
114144
}
115145

@@ -121,39 +151,6 @@ private function indentationModeSwitch(string $line): int
121151
return $mode;
122152
}
123153

124-
//--------------------------------------------------------------------------------------------------------------------
125-
/**
126-
* Decrements indent level of the current switch statement (if any).
127-
*/
128-
private function switchLevelDecrement(): void
129-
{
130-
if (!empty($this->switchLevel) && $this->switchLevel[sizeof($this->switchLevel) - 1]>0)
131-
{
132-
$this->switchLevel[sizeof($this->switchLevel) - 1]--;
133-
}
134-
}
135-
136-
//--------------------------------------------------------------------------------------------------------------------
137-
/**
138-
* Increments indent level of the current switch statement (if any).
139-
*/
140-
private function switchLevelIncrement(): void
141-
{
142-
if (!empty($this->switchLevel))
143-
{
144-
$this->switchLevel[sizeof($this->switchLevel) - 1]++;
145-
}
146-
}
147-
148-
//--------------------------------------------------------------------------------------------------------------------
149-
/**
150-
* Returns true if the indent level of the current switch statement (if any) is zero. Otherwise, returns false.
151-
*/
152-
private function switchLevelIsZero(): bool
153-
{
154-
return (!empty($this->switchLevel) && $this->switchLevel[sizeof($this->switchLevel) - 1]==0);
155-
}
156-
157154
//--------------------------------------------------------------------------------------------------------------------
158155
}
159156

test/CodeStoreTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,22 @@ public function testIndentationLevel(): void
4040
$store->append('}');
4141
$store->append('');
4242
$store->appendSeparator();
43-
$store->append('public function testSwitch($i)');
43+
$store->append('public function testSwitch1($i)');
44+
$store->append('{');
45+
$store->append('switch ($i)');
46+
$store->append('{');
47+
$store->append('case 1:');
48+
$store->append("echo 'one';");
49+
$store->append('break;');
50+
$store->append('');
51+
$store->append('case 2:');
52+
$store->append("echo 'two';");
53+
$store->append('break;');
54+
$store->append('}');
55+
$store->append('}');
56+
$store->append('');
57+
$store->appendSeparator();
58+
$store->append('public function testSwitch2($i)');
4459
$store->append('{');
4560
$store->append('switch ($i)');
4661
$store->append('{');
@@ -97,7 +112,22 @@ public function testIndentationLevel()
97112
}
98113
99114
//----------------------------------------------------------------------------
100-
public function testSwitch(\$i)
115+
public function testSwitch1(\$i)
116+
{
117+
switch (\$i)
118+
{
119+
case 1:
120+
echo 'one';
121+
break;
122+
123+
case 2:
124+
echo 'two';
125+
break;
126+
}
127+
}
128+
129+
//----------------------------------------------------------------------------
130+
public function testSwitch2(\$i)
101131
{
102132
switch (\$i)
103133
{

0 commit comments

Comments
 (0)