Skip to content

Commit 13bc953

Browse files
committed
add more testcases and rm arrayDimFetch code
1 parent 9fc6582 commit 13bc953

File tree

3 files changed

+146
-38
lines changed

3 files changed

+146
-38
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpParser\Node;
1313
use PhpParser\Node\Expr\Array_;
1414
use PhpParser\Node\Expr\ArrayDimFetch;
15-
use PhpParser\Node\Expr\ArrayItem;
1615
use PhpParser\Node\Expr\Assign;
1716
use PhpParser\Node\Expr\BinaryOp;
1817
use PhpParser\Node\Expr\CallLike;
@@ -72,11 +71,6 @@ final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
7271
*/
7372
private $returns = [];
7473

75-
/**
76-
* @psalm-var array<int, bool>
77-
*/
78-
private $arrayDimFetchVars = [];
79-
8074
public function enterNode(Node $node): void
8175
{
8276
$this->savePropertyLines($node);
@@ -151,8 +145,6 @@ private function getLines(Node $node): array
151145
}
152146

153147
if ($node instanceof ArrayDimFetch) {
154-
$this->arrayDimFetchVars[spl_object_id($node->var)] = true;
155-
156148
if (null === $node->dim) {
157149
return [];
158150
}
@@ -161,23 +153,11 @@ private function getLines(Node $node): array
161153
}
162154

163155
if ($node instanceof Array_) {
164-
if (isset($this->arrayDimFetchVars[spl_object_id($node)])) {
165-
return [];
166-
}
167-
168-
$startLine = $node->getStartLine();
169-
170-
if (isset($this->executableLines[$startLine])) {
171-
return [];
172-
}
173-
174156
if ([] === $node->items) {
175157
return [$node->getEndLine()];
176158
}
177159

178-
if ($node->items[0] instanceof ArrayItem) {
179-
return [$node->items[0]->getStartLine()];
180-
}
160+
return [];
181161
}
182162

183163
if ($node instanceof ClassMethod) {
@@ -248,7 +228,7 @@ private function getLines(Node $node): array
248228
private function getNodeStartLine(NodeAbstract $node): int
249229
{
250230
if ($node instanceof Node\Expr\BooleanNot) {
251-
return $node->getEndLine();
231+
return $this->getNodeStartLine($node->expr);
252232
}
253233

254234
if ($node instanceof BinaryOp) {

tests/_files/source_with_multiline_constant_return.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,117 @@ public function multilineHeredoc(): string
380380
EOF;
381381

382382
}
383+
384+
public function unaryLogicalNotWithNotConstInTheMiddle(): string
385+
{
386+
return !
387+
(
388+
''
389+
.
390+
phpversion()
391+
.
392+
''
393+
);
394+
}
395+
396+
public function unaryMinusWithNotConstInTheMiddle(): string
397+
{
398+
return -
399+
(
400+
''
401+
.
402+
phpversion()
403+
.
404+
''
405+
);
406+
}
407+
408+
public function unaryCastWithNotConstInTheMiddle(): int
409+
{
410+
return (
411+
int
412+
)
413+
(
414+
''
415+
.
416+
phpversion()
417+
.
418+
''
419+
);
420+
}
421+
422+
public function complexArrayWithNotConstInTheMiddle(): string
423+
{
424+
return [
425+
[
426+
1,
427+
phpversion(),
428+
1,
429+
],
430+
[
431+
[
432+
1,
433+
],
434+
],
435+
[
436+
phpversion(),
437+
1,
438+
],
439+
[
440+
1,
441+
phpversion(),
442+
],
443+
phpversion(),
444+
];
445+
}
446+
447+
public function constFromArrayWithNotConstInTheMiddle(): string
448+
{
449+
return [
450+
'foo',
451+
'bar',
452+
'ro',
453+
'fi' => 'fi',
454+
'omega',
455+
]
456+
[
457+
'f'
458+
. 'i'
459+
]
460+
;
461+
}
462+
463+
public function emptyArray(): string
464+
{
465+
return
466+
(
467+
[
468+
]
469+
)
470+
;
471+
}
472+
473+
public function complexAssociativityNa2(): bool
474+
{
475+
return
476+
!
477+
!
478+
!
479+
<<<'EOF'
480+
foo
481+
foo
482+
EOF
483+
;
484+
}
485+
486+
public function unaryMinusNowdoc(): bool
487+
{
488+
return
489+
-
490+
<<<'EOF'
491+
foo
492+
foo
493+
EOF
494+
;
495+
}
383496
}

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ public function testUseStatementsAreUncovered(): void
272272
[
273273
12,
274274
14,
275-
15,
276275
16,
277276
18,
278277
],
@@ -300,7 +299,6 @@ public function testInterfacesAreUncovered(): void
300299
[
301300
7,
302301
9,
303-
10,
304302
11,
305303
13,
306304
],
@@ -333,7 +331,7 @@ public function testHeavyIndentationIsHandledCorrectly(): void
333331

334332
$this->assertEquals(
335333
[
336-
9,
334+
9, // TODO not in xdebug output
337335
12,
338336
16,
339337
18,
@@ -342,35 +340,40 @@ public function testHeavyIndentationIsHandledCorrectly(): void
342340
25,
343341
28,
344342
31,
345-
36,
346-
40,
343+
// 36,
344+
40, // TODO not in xdebug output
347345
46,
348-
48,
346+
48, // TODO not in xdebug output
349347
54,
350348
60,
351349
64,
352350
71,
353-
83,
351+
83, // TODO not in xdebug output
354352
85,
355-
87,
353+
87, // TODO not in xdebug output
356354
89,
357355
91,
358356
93,
359-
95,
357+
95, // TODO not in xdebug output
360358
97,
361-
99,
359+
99, // TODO not in xdebug output
362360
101,
363-
116,
364-
120,
365-
123,
361+
116, // TODO not in xdebug output
362+
120, // TODO not in xdebug output
363+
123, // TODO not in xdebug output
366364
125, // This shouldn't be marked as LoC, but it's high unlikely to happen IRL to have $var = []; on multiple lines
367365
132,
368366
135,
369-
139,
367+
139, // TODO not in xdebug output
370368
143,
371369
149,
372370
153,
373371
159,
372+
373+
// xdebug lines:
374+
// 12, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 31, 33, 36, 38, 42, 46,
375+
// 50, 52, 54, 60, 64, 71, 72, 73, 74, 85, 89, 91, 93, 97, 101, 103, 118, 125,
376+
// 132, 135, 143, 149, 153, 159, 162
374377
],
375378
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
376379
);
@@ -414,9 +417,9 @@ public function testReturnStatementWithOnlyAnArrayWithScalarReturnsTheFirstEleme
414417

415418
$this->assertEquals(
416419
[
417-
8,
420+
8, // TODO this line is correct and the only one reported by xdebug
418421
15,
419-
24,
422+
24, // TODO this line is correct and the only one reported by xdebug
420423
30,
421424
40,
422425
47,
@@ -476,6 +479,18 @@ public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): vo
476479
351,
477480
370,
478481
377,
482+
390,
483+
402,
484+
411,
485+
416,
486+
427,
487+
436,
488+
441,
489+
443,
490+
458,
491+
468,
492+
480,
493+
491,
479494
],
480495
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
481496
);

0 commit comments

Comments
 (0)