Skip to content

Commit 120d3b3

Browse files
committed
Indent switch statements.
1 parent 7d10746 commit 120d3b3

File tree

3 files changed

+171
-3
lines changed

3 files changed

+171
-3
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": ">=7.1.0",
13-
"setbased/helper-code-store": "^2.0.0"
13+
"setbased/helper-code-store": "^2.1.0"
1414
},
1515
"require-dev": {
1616
"phpunit/phpunit": "^7.0.0 || ^8.0.0",

src/PhpCodeStore.php

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
*/
99
class PhpCodeStore extends CodeStore
1010
{
11+
//--------------------------------------------------------------------------------------------------------------------
12+
/**
13+
* The levels of nested switch statements.
14+
*
15+
* @var int[]
16+
*/
17+
private $switchLevel = [];
18+
1119
//--------------------------------------------------------------------------------------------------------------------
1220
/**
1321
* Object constructor.
@@ -31,23 +39,121 @@ public function __construct(int $indentation = 2, int $width = 120)
3139
*/
3240
protected function indentationMode(string $line): int
3341
{
42+
$line = trim($line);
43+
3444
$mode = 0;
45+
$mode |= $this->indentationModeSwitch($line);
46+
$mode |= $this->indentationModeBLock($line);
3547

36-
$line = trim($line);
48+
return $mode;
49+
}
50+
51+
//--------------------------------------------------------------------------------------------------------------------
52+
/**
53+
* Returns the indentation mode based blocks of code.
54+
*
55+
* @param string $line The line of code.
56+
*
57+
* @return int
58+
*/
59+
protected function indentationModeBlock(string $line): int
60+
{
61+
$mode = 0;
3762

3863
if (substr($line, -1, 1)=='{')
3964
{
4065
$mode |= self::C_INDENT_INCREMENT_AFTER;
66+
67+
$this->switchLevelIncrement();
4168
}
4269

4370
if (substr($line, 0, 1)=='}')
4471
{
45-
$mode |= self::C_INDENT_DECREMENT_BEFORE;
72+
$this->switchLevelDecrement();
73+
74+
if ($this->switchLevelIsZero())
75+
{
76+
$mode |= self::C_INDENT_DECREMENT_BEFORE_DOUBLE;
77+
78+
array_pop($this->switchLevel);
79+
}
80+
else
81+
{
82+
$mode |= self::C_INDENT_DECREMENT_BEFORE;
83+
}
84+
}
85+
86+
return $mode;
87+
}
88+
89+
//--------------------------------------------------------------------------------------------------------------------
90+
/**
91+
* Returns the indentation mode based on a line of code for switch statements.
92+
*
93+
* @param string $line The line of code.
94+
*
95+
* @return int
96+
*/
97+
private function indentationModeSwitch(string $line): int
98+
{
99+
$mode = 0;
100+
101+
if (substr($line, 0, 7)=='switch ')
102+
{
103+
$this->switchLevel[] = 0;
104+
}
105+
106+
if (substr($line, 0, 5)=='case ')
107+
{
108+
$mode |= self::C_INDENT_INCREMENT_AFTER;
109+
}
110+
111+
if (substr($line, 0, 8)=='default:')
112+
{
113+
$mode |= self::C_INDENT_INCREMENT_AFTER;
114+
}
115+
116+
if (substr($line, 0, 6)=='break;')
117+
{
118+
$mode |= self::C_INDENT_DECREMENT_AFTER;
46119
}
47120

48121
return $mode;
49122
}
50123

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+
51157
//--------------------------------------------------------------------------------------------------------------------
52158
}
53159

test/CodeStoreTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,37 @@ public function testIndentationLevel(): void
4040
$store->append('}');
4141
$store->append('');
4242
$store->appendSeparator();
43+
$store->append('public function testSwitch($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('default:');
56+
$store->append('switch ($i)');
57+
$store->append('{');
58+
$store->append('case 4:');
59+
$store->append("echo 'four';");
60+
$store->append('break;');
61+
$store->append('');
62+
$store->append('case 5:');
63+
$store->append("echo 'five';");
64+
$store->append('break;');
65+
$store->append('');
66+
$store->append('default:');
67+
$store->append('echo $i;');
68+
$store->append('{;}');
69+
$store->append('}');
70+
$store->append('}');
71+
$store->append('}');
72+
$store->append('');
73+
$store->appendSeparator();
4374
$store->append('}');
4475
$store->append('');
4576
$store->appendSeparator();
@@ -65,6 +96,37 @@ public function testIndentationLevel()
6596
}
6697
}
6798
99+
//----------------------------------------------------------------------------
100+
public function testSwitch(\$i)
101+
{
102+
switch (\$i)
103+
{
104+
case 1:
105+
echo 'one';
106+
break;
107+
108+
case 2:
109+
echo 'two';
110+
break;
111+
112+
default:
113+
switch (\$i)
114+
{
115+
case 4:
116+
echo 'four';
117+
break;
118+
119+
case 5:
120+
echo 'five';
121+
break;
122+
123+
default:
124+
echo \$i;
125+
{;}
126+
}
127+
}
128+
}
129+
68130
//----------------------------------------------------------------------------
69131
}
70132

0 commit comments

Comments
 (0)