Skip to content

Commit ecdb424

Browse files
Merge branch '9.2'
2 parents dd30c61 + 74b7413 commit ecdb424

File tree

4 files changed

+297
-1
lines changed

4 files changed

+297
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
2525
### Fixed
2626

2727
* [#935](https://github.com/sebastianbergmann/php-code-coverage/pull/935): Cobertura package name attribute is always empty
28+
* [#946](https://github.com/sebastianbergmann/php-code-coverage/issues/946): `return` with multiline constant expression must only contain the last line
2829

2930
## [9.2.17] - 2022-08-30
3031

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ private function computeReturns(): void
135135
*/
136136
private function getLines(Node $node): array
137137
{
138+
if ($node instanceof BinaryOp) {
139+
if (($node->left instanceof Node\Scalar ||
140+
$node->left instanceof Node\Expr\ConstFetch) &&
141+
($node->right instanceof Node\Scalar ||
142+
$node->right instanceof Node\Expr\ConstFetch)) {
143+
return [$node->right->getStartLine()];
144+
}
145+
146+
return [];
147+
}
148+
138149
if ($node instanceof Cast ||
139150
$node instanceof PropertyFetch ||
140151
$node instanceof NullsafePropertyFetch ||
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
<?php
2+
3+
class Foo
4+
{
5+
public function BitwiseAnd(): int
6+
{
7+
return
8+
1
9+
&
10+
1
11+
;
12+
}
13+
14+
public function BitwiseOr(): int
15+
{
16+
return
17+
1
18+
|
19+
1
20+
;
21+
}
22+
23+
public function BitwiseXor(): int
24+
{
25+
return
26+
1
27+
^
28+
1
29+
;
30+
}
31+
32+
public function BooleanAnd(): bool
33+
{
34+
return
35+
true
36+
&&
37+
false
38+
;
39+
}
40+
41+
public function BooleanOr(): bool
42+
{
43+
return
44+
false
45+
||
46+
true
47+
;
48+
}
49+
50+
public function Coalesce(): bool
51+
{
52+
return
53+
true
54+
??
55+
false
56+
;
57+
}
58+
59+
public function Concat(): string
60+
{
61+
return
62+
'foo'
63+
.
64+
'bar'
65+
;
66+
}
67+
68+
public function Div(): int
69+
{
70+
return
71+
2
72+
/
73+
1
74+
;
75+
}
76+
77+
public function Equal(): bool
78+
{
79+
return
80+
2
81+
==
82+
1
83+
;
84+
}
85+
86+
public function Greater(): bool
87+
{
88+
return
89+
2
90+
>
91+
1
92+
;
93+
}
94+
95+
public function GreaterOrEqual(): bool
96+
{
97+
return
98+
2
99+
>=
100+
1
101+
;
102+
}
103+
104+
public function Identical(): bool
105+
{
106+
return
107+
2
108+
===
109+
1
110+
;
111+
}
112+
113+
public function LogicalAnd(): bool
114+
{
115+
return
116+
true
117+
and
118+
false
119+
;
120+
}
121+
122+
public function LogicalOr(): bool
123+
{
124+
return
125+
true
126+
or
127+
false
128+
;
129+
}
130+
131+
public function LogicalXor(): bool
132+
{
133+
return
134+
true
135+
xor
136+
false
137+
;
138+
}
139+
140+
public function Minus(): int
141+
{
142+
return
143+
2
144+
-
145+
1
146+
;
147+
}
148+
149+
public function Mod(): int
150+
{
151+
return
152+
2
153+
%
154+
1
155+
;
156+
}
157+
158+
public function Mul(): int
159+
{
160+
return
161+
2
162+
*
163+
1
164+
;
165+
}
166+
167+
public function NotEqual(): bool
168+
{
169+
return
170+
2
171+
!=
172+
1
173+
;
174+
}
175+
176+
public function NotIdentical(): bool
177+
{
178+
return
179+
2
180+
!==
181+
1
182+
;
183+
}
184+
185+
public function Plus(): int
186+
{
187+
return
188+
2
189+
+
190+
1
191+
;
192+
}
193+
194+
public function Pow(): int
195+
{
196+
return
197+
2
198+
**
199+
1
200+
;
201+
}
202+
203+
public function ShiftLeft(): int
204+
{
205+
return
206+
2
207+
<<
208+
1
209+
;
210+
}
211+
212+
public function ShiftRight(): int
213+
{
214+
return
215+
2
216+
>>
217+
1
218+
;
219+
}
220+
221+
public function Smaller(): bool
222+
{
223+
return
224+
2
225+
<
226+
1
227+
;
228+
}
229+
230+
public function SmallerOrEqual(): bool
231+
{
232+
return
233+
2
234+
<=
235+
1
236+
;
237+
}
238+
239+
public function Spaceship(): int
240+
{
241+
return
242+
2
243+
<=>
244+
1
245+
;
246+
}
247+
}

tests/tests/Data/RawCodeCoverageDataTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ public function testHeavyIndentationIsHandledCorrectly(): void
318318
99,
319319
101,
320320
116,
321-
117,
322321
120,
323322
123,
324323
125, // This shouldn't be marked as LoC, but it's high unlikely to happen IRL to have $var = []; on multiple lines
@@ -385,6 +384,44 @@ public function testReturnStatementWithOnlyAnArrayWithScalarReturnsTheFirstEleme
385384
);
386385
}
387386

387+
public function testReturnStatementWithConstantExprOnlyReturnTheLineOfLast(): void
388+
{
389+
$file = TEST_FILES_PATH . 'source_with_multiline_constant_return.php';
390+
391+
$this->assertEquals(
392+
[
393+
10,
394+
19,
395+
28,
396+
37,
397+
46,
398+
55,
399+
64,
400+
73,
401+
82,
402+
91,
403+
100,
404+
109,
405+
118,
406+
127,
407+
136,
408+
145,
409+
154,
410+
163,
411+
172,
412+
181,
413+
190,
414+
199,
415+
208,
416+
217,
417+
226,
418+
235,
419+
244,
420+
],
421+
array_keys(RawCodeCoverageData::fromUncoveredFile($file, new ParsingFileAnalyser(true, true))->lineCoverage()[$file])
422+
);
423+
}
424+
388425
public function testCoverageForFileWithInlineAnnotations(): void
389426
{
390427
$filename = TEST_FILES_PATH . 'source_with_oneline_annotations.php';

0 commit comments

Comments
 (0)