88 */
99class 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
0 commit comments