Skip to content

Commit d27d1b0

Browse files
committed
Add visibility and type to constant declarations.
1 parent 7bbda2d commit d27d1b0

File tree

2 files changed

+35
-65
lines changed

2 files changed

+35
-65
lines changed

src/Backend/MySqlConstantWorker.php

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private function executeColumnWidths(): void
125125

126126
//--------------------------------------------------------------------------------------------------------------------
127127
/**
128-
* Creates constants declarations in a class.
128+
* Creates constant declarations in a class.
129129
*/
130130
private function executeCreateConstants(): void
131131
{
@@ -159,94 +159,64 @@ private function executeEnabled(): void
159159
* * The first line of the doc block with the annotation '@setbased.stratum.constants'.
160160
* * The last line of this doc block.
161161
* * The last line of continuous constant declarations directly after the doc block.
162-
* If one of these line can not be found the line number will be set to null.
162+
* If one of these lines cannot be found, the line number will be set to null.
163163
*
164164
* @param string $source The source code of the constant class.
165165
*/
166166
private function extractLines(string $source): array
167167
{
168-
$tokens = token_get_all($source);
168+
$lines = explode("\n", $source);
169169

170170
$line1 = null;
171171
$line2 = null;
172172
$line3 = null;
173173

174-
// Find annotation @constants
175174
$step = 1;
176-
foreach ($tokens as $token)
175+
foreach ($lines as $index => $line)
177176
{
178177
switch ($step)
179178
{
180179
case 1:
181180
// Step 1: Find doc comment with annotation.
182-
if (is_array($token) && $token[0]==T_DOC_COMMENT)
181+
if (str_contains($line, '@setbased.stratum.constants'))
183182
{
184-
if (str_contains($token[1], '@setbased.stratum.constants'))
185-
{
186-
$line1 = $token[2];
187-
$step = 2;
188-
}
183+
$line1 = $index;
184+
$step = 2;
189185
}
190186
break;
191187

192188
case 2:
193189
// Step 2: Find end of doc block.
194-
if (is_array($token))
190+
if (trim($line)==='*/')
195191
{
196-
if ($token[0]==T_WHITESPACE)
197-
{
198-
$line2 = $token[2];
199-
if (substr_count($token[1], "\n")>1)
200-
{
201-
// Whitespace contains new line: end doc block without constants.
202-
$step = 4;
203-
}
204-
}
205-
else
206-
{
207-
if ($token[0]==T_CONST)
208-
{
209-
$line3 = $token[2];
210-
$step = 3;
211-
}
212-
else
213-
{
214-
$step = 4;
215-
}
216-
}
192+
$step = 3;
217193
}
218194
break;
219195

220196
case 3:
221-
// Step 4: Find en of constants declarations.
222-
if (is_array($token))
197+
// Step 3: Start of the constant declarations or not.
198+
$line2 = $index;
199+
if (preg_match('/^\s*(public\s+)?const/', $line))
223200
{
224-
if ($token[0]==T_WHITESPACE)
225-
{
226-
if (substr_count($token[1], "\n")<=1)
227-
{
228-
// Ignore whitespace.
229-
$line3 = $token[2];
230-
}
231-
else
232-
{
233-
// Whitespace contains new line: end of const declarations.
234-
$step = 4;
235-
}
236-
}
237-
elseif ($token[0]==T_CONST || $token[2]==$line3)
238-
{
239-
$line3 = $token[2];
240-
}
241-
else
242-
{
243-
$step = 4;
244-
}
201+
$step = 4;
202+
}
203+
else
204+
{
205+
$step = 5;
245206
}
246207
break;
247208

248209
case 4:
249-
// Leave loop.
210+
// Step 4: Find the end of the constant declarations.
211+
if (!preg_match('/^\s*(public\s+)?const/', $line))
212+
{
213+
$line3 = $index;
214+
$step = 5;
215+
}
216+
break;
217+
218+
case 5:
219+
// Leave the loop.
250220
break;
251221
}
252222
}
@@ -385,7 +355,7 @@ private function makeConstantStatements(): array
385355
$width2 = max(mb_strlen((string)$value), $width2);
386356
}
387357

388-
$format = sprintf(' const %%-%ds = %%%dd;', $width1, $width2);
358+
$format = sprintf(' public const int %%-%ds = %%%dd;', $width1, $width2);
389359
foreach ($this->constants as $constant => $value)
390360
{
391361
$constants[] = sprintf($format, $constant, $value);

test/C.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
declare(strict_types=1);
3+
34
namespace SetBased\Stratum\MySql\Test;
45

56
/**
@@ -10,12 +11,11 @@ class C
1011
/**
1112
* @setbased.stratum.constants
1213
*
13-
* Below this doc block constants will be inserted by PhpStratum.
14+
* PhpStratum will insert below this doc block constants.
1415
*/
15-
const TST_ID_BUNNY = 3;
16-
const TST_ID_CAT = 4;
17-
const TST_ID_EGGS = 2;
18-
const TST_ID_ELEPHANT = 5;
19-
const TST_ID_SPAM = 1;
20-
16+
public const int TST_ID_BUNNY = 3;
17+
public const int TST_ID_CAT = 4;
18+
public const int TST_ID_EGGS = 2;
19+
public const int TST_ID_ELEPHANT = 5;
20+
public const int TST_ID_SPAM = 1;
2121
}

0 commit comments

Comments
 (0)