Skip to content

Commit 6bc2941

Browse files
committed
Cover the configured range check and the analysed-version floor for PHP_VERSION_ID < X guards
A Composer constraint without a lower bound keeps the guard's full range, and an analysed version below the guard stays the floor.
1 parent dc73664 commit 6bc2941

9 files changed

Lines changed: 127 additions & 3 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Classes;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use PHPUnit\Framework\Attributes\RequiresPhp;
8+
9+
/**
10+
* @extends RuleTestCase<InvalidPromotedPropertiesRule>
11+
*/
12+
class InvalidPromotedPropertiesRulePhpVersionRangeTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new InvalidPromotedPropertiesRule();
18+
}
19+
20+
#[RequiresPhp('>= 8.0.0')]
21+
public function testBug15425(): void
22+
{
23+
$this->analyse([__DIR__ . '/data/bug-15425-range.php'], [
24+
[
25+
'Promoted properties are supported only on PHP 8.0 and later.',
26+
10,
27+
],
28+
]);
29+
}
30+
31+
public static function getAdditionalConfigFiles(): array
32+
{
33+
return [
34+
__DIR__ . '/invalidPromotedPropertiesPhpVersionRange.neon',
35+
];
36+
}
37+
38+
}

‎tests/PHPStan/Rules/Classes/InvalidPromotedPropertiesRuleTest.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ public function testConditionallyDeclaredClass(): void
144144
#[RequiresPhp('>= 8.0.0')]
145145
public function testBug15425(): void
146146
{
147-
$this->analyse([__DIR__ . '/data/bug-15425.php'], []);
147+
$this->analyse([__DIR__ . '/data/bug-15425.php'], [
148+
[
149+
'Promoted properties are supported only on PHP 8.0 and later.',
150+
19,
151+
],
152+
]);
148153
}
149154

150155
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Classes;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\Testing\RuleTestCase;
7+
use PHPUnit\Framework\Attributes\RequiresPhp;
8+
9+
/**
10+
* @extends RuleTestCase<ReadOnlyClassRule>
11+
*/
12+
class ReadOnlyClassRulePhp81Test extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new ReadOnlyClassRule();
18+
}
19+
20+
#[RequiresPhp('>= 8.2.0')]
21+
public function testBug15425(): void
22+
{
23+
$this->analyse([__DIR__ . '/data/bug-15425-readonly.php'], [
24+
[
25+
'Readonly classes are supported only on PHP 8.2 and later.',
26+
8,
27+
],
28+
]);
29+
}
30+
31+
public static function getAdditionalConfigFiles(): array
32+
{
33+
return [
34+
__DIR__ . '/readOnlyClassPhp81.neon',
35+
];
36+
}
37+
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"php": "<8.4"
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15425Range;
6+
7+
if (PHP_VERSION_ID < 80300) {
8+
final class Foo
9+
{
10+
public function __construct(public ?string $message = null)
11+
{
12+
}
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php // lint >= 8.2
2+
3+
declare(strict_types = 1);
4+
5+
namespace Bug15425Readonly;
6+
7+
if (PHP_VERSION_ID < 80400) {
8+
final readonly class Foo
9+
{
10+
}
11+
}

‎tests/PHPStan/Rules/Classes/data/bug-15425.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
namespace Bug15425;
66

77
if (PHP_VERSION_ID < 80400) {
8-
\PHPStan\dumpType(PHP_VERSION_ID);
9-
108
final class Foo
119
{
1210
public function __construct(public ?string $message = null)
1311
{
1412
}
1513
}
1614
}
15+
16+
if (PHP_VERSION_ID < 80000) {
17+
final class Bar
18+
{
19+
public function __construct(public ?string $message = null)
20+
{
21+
}
22+
}
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
# composer.json "php": "<8.4" gives a version range without a lower bound
3+
composerAutoloaderProjectPaths:
4+
- %rootDir%/tests/PHPStan/Rules/Classes/data/bug-15425-composer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
parameters:
2+
phpVersion: 80100

0 commit comments

Comments
 (0)