Skip to content

Commit c49e17d

Browse files
committed
fix do statement
1 parent db9559f commit c49e17d

File tree

3 files changed

+61
-24
lines changed

3 files changed

+61
-24
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use PhpParser\Node\Stmt\Echo_;
3636
use PhpParser\Node\Stmt\ElseIf_;
3737
use PhpParser\Node\Stmt\Expression;
38-
use PhpParser\Node\Stmt\Finally_;
3938
use PhpParser\Node\Stmt\For_;
4039
use PhpParser\Node\Stmt\Foreach_;
4140
use PhpParser\Node\Stmt\Function_;
@@ -237,11 +236,16 @@ private function getLines(NodeAbstract $node, bool $fromReturns): array
237236
return [$this->getNodeStartLine($node->body)];
238237
}
239238

240-
// TODO this concept should be extended for every statement class like Foreach_
239+
// TODO this concept should be extended for every statement class like Foreach_, For_, ...
241240
if ($node instanceof If_ ||
242-
$node instanceof ElseIf_) {
241+
$node instanceof ElseIf_ ||
242+
$node instanceof While_ ||
243+
$node instanceof Do_) {
243244
return [$this->getNodeStartLine($node->cond)];
244245
}
246+
if ($node instanceof Catch_) {
247+
return [$this->getNodeStartLine($node->types[0])];
248+
}
245249

246250
if ($node instanceof Function_) {
247251
if ($node->stmts === []) {
@@ -307,7 +311,6 @@ private function isExecutable(Node $node): bool
307311
$node instanceof ElseIf_ ||
308312
$node instanceof Encapsed ||
309313
$node instanceof Expression ||
310-
$node instanceof Finally_ ||
311314
$node instanceof For_ ||
312315
$node instanceof Foreach_ ||
313316
$node instanceof Function_ ||

tests/_files/source_with_heavy_indentation.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,40 @@ public function noElse(): int
141141
return $res;
142142
}
143143

144-
public function noTry(): int
144+
public function noDo(): int
145+
{
146+
$res = 0;
147+
do {
148+
if (mt_rand() !== 0) {
149+
$res++;
150+
}
151+
} while (
152+
$res
153+
<
154+
10
155+
);
156+
157+
return $res;
158+
}
159+
160+
public function noTryNoFinally(): int
145161
{
146162
$res = 0;
147163
try {
148164
if (mt_rand() === 0) {
149165
throw new \Exception();
150166
}
151-
} catch (\Exception $e) {
167+
} catch (
168+
\x\y
169+
|
170+
\x\z
171+
$e
172+
) {
173+
$res = 1;
174+
} catch (\Error $e) {
152175
$res = 1;
176+
} finally {
177+
$res += 10;
153178
}
154179

155180
return $res;

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -370,31 +370,40 @@ public function testHeavyIndentationIsHandledCorrectly(): void
370370
138,
371371
141,
372372
146,
373-
// line 147 is try statement, not in xdebug output (only catch condition is covered)
374373
148,
375374
149,
376-
151,
377-
152,
378-
155,
375+
154,
376+
157,
377+
162,
378+
// line 163 is try statement, not in xdebug output (only catch condition is covered)
379379
164,
380-
166,
380+
165,
381381
168,
382-
170,
383-
172,
382+
173,
384383
174,
385-
176,
386-
178,
384+
175,
385+
// line 176 is finally statement, not in xdebug output (only catch condition is covered)
386+
177,
387387
180,
388-
182,
389-
204,
390-
209,
391-
213,
392-
216,
393-
220,
394-
224,
395-
230,
388+
189,
389+
191,
390+
193,
391+
195,
392+
197,
393+
199,
394+
201,
395+
203,
396+
205,
397+
207,
398+
229,
396399
234,
397-
240,
400+
238,
401+
241,
402+
245,
403+
249,
404+
255,
405+
259,
406+
265,
398407
],
399408
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
400409
);

0 commit comments

Comments
 (0)