Skip to content

Commit 5e4b12c

Browse files
committed
Enhance constructor validation for promoted properties and add tests for asymmetric visibility in property promotion
1 parent 73b28f4 commit 5e4b12c

4 files changed

Lines changed: 195 additions & 3 deletions

File tree

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
364364
$hasTraits = true;
365365
} elseif ($stmt instanceof Node\Stmt\Property && $stmt->getDocComment() !== null) {
366366
$hasPropertyWithDoc = true;
367+
} elseif ($stmt instanceof Node\Stmt\ClassMethod && strtolower($stmt->name->toString()) === '__construct') {
368+
foreach ($stmt->params as $param) {
369+
if ($param->isPromoted() && $param->getDocComment() !== null) {
370+
$hasPropertyWithDoc = true;
371+
372+
break;
373+
}
374+
}
367375
}
368376
}
369377

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
4848

4949
$methodName = $isClassMethod ? strtolower($node->name->toString()) : '';
5050
$isConstructor = $isClassMethod && $methodName === '__construct';
51+
52+
if ($isConstructor && ! $hasPropertyWithDoc) {
53+
foreach ($node->params as $param) {
54+
if ($param->isPromoted() && $param->getDocComment() !== null) {
55+
$hasPropertyWithDoc = true;
56+
57+
break;
58+
}
59+
}
60+
}
61+
5162
$isMagicLifecycle = $isClassMethod && \in_array($methodName, ['__construct', '__destruct', '__clone'], true);
5263
$isMagicGet = $isClassMethod && $methodName === '__get';
5364
$isMagicCall = $isClassMethod && ($methodName === '__call' || $methodName === '__callstatic');
@@ -188,7 +199,7 @@ private static function hasParamContracts(
188199
return false;
189200
}
190201

191-
if ($isPrivate && $docText === '') {
202+
if ($isPrivate && $docText === '' && ! ($isConstructor && $hasPropertyWithDoc)) {
192203
return false;
193204
}
194205

@@ -204,7 +215,7 @@ private static function hasParamContracts(
204215
return self::hasNonMixedParam($docText);
205216
}
206217

207-
return $isClassMethod && ! $isPrivate;
218+
return $isClassMethod && (! $isPrivate || ($isConstructor && $hasPropertyWithDoc));
208219
}
209220

210221
private static function hasNonMixedParam(string $docText): bool
@@ -469,7 +480,10 @@ private static function buildParamWrappers(
469480
$wrappers = [];
470481

471482
foreach ($params as $param) {
472-
if ($predicate($param, $docText) && $param->var instanceof Node\Expr\Variable && \is_string($param->var->name)) {
483+
$paramDoc = $param->getDocComment() !== null ? $param->getDocComment()->getText() : '';
484+
$effectiveDoc = $docText . ' ' . $paramDoc;
485+
486+
if ($predicate($param, $effectiveDoc) && $param->var instanceof Node\Expr\Variable && \is_string($param->var->name)) {
473487
$paramName = $param->var->name;
474488
$assignExpr = new Node\Expr\Assign(
475489
new Node\Expr\Variable($paramName),
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\Fixtures\PropertyHooks;
6+
7+
class AsymmetricPromotedVarOrder
8+
{
9+
public function __construct(
10+
/**
11+
* @var positive-int
12+
*/
13+
public private(set) int $orderId,
14+
) {
15+
}
16+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\TypeChecking\Boundaries;
6+
7+
use TypePHP\Exception\TypeError;
8+
9+
class PromotedVarUnnamedOrder
10+
{
11+
public function __construct(
12+
/**
13+
* @var positive-int
14+
*/
15+
public int $orderId,
16+
) {
17+
}
18+
}
19+
20+
class PromotedVarNamedSku
21+
{
22+
public function __construct(
23+
/**
24+
* @var non-empty-string $sku
25+
*/
26+
public string $sku,
27+
) {
28+
}
29+
}
30+
31+
class PromotedToolingVarOrder
32+
{
33+
public function __construct(
34+
/**
35+
* @phpstan-var positive-int
36+
*/
37+
public int $id,
38+
/**
39+
* @psalm-var non-empty-string
40+
*/
41+
public string $code,
42+
) {
43+
}
44+
}
45+
46+
class PromotedMixedMultipleOrder
47+
{
48+
public function __construct(
49+
/**
50+
* @var positive-int
51+
*/
52+
public int $id,
53+
/**
54+
* @var non-empty-string
55+
*/
56+
public string $name,
57+
/**
58+
* @var int<1, 100>
59+
*/
60+
public int $quantity = 1,
61+
) {
62+
}
63+
}
64+
65+
if (PHP_VERSION_ID >= 80400) {
66+
require_once __DIR__ . '/../../Fixtures/PropertyHooks/AsymmetricPromotedPropertyFixtures.php';
67+
}
68+
69+
describe('Constructor Property Promotion (CPP) with /** @var */ Annotations', function () {
70+
describe('Standard Promoted Properties (PHP 8.0+)', function () {
71+
test('accepts valid value on promoted parameter with unnamed /** @var */', function () {
72+
$order = new PromotedVarUnnamedOrder(100);
73+
74+
expect($order->orderId)->toBe(100);
75+
});
76+
77+
test('rejects invalid value on promoted parameter with unnamed /** @var */', function () {
78+
expect(fn () => new PromotedVarUnnamedOrder(-50))
79+
->toThrow(TypeError::class, 'Argument $orderId must be of type positive-int, negative int (-50) given')
80+
;
81+
});
82+
83+
test('accepts valid value on promoted parameter with named /** @var Type $name */', function () {
84+
$product = new PromotedVarNamedSku('SKU-100');
85+
86+
expect($product->sku)->toBe('SKU-100');
87+
});
88+
89+
test('rejects invalid value on promoted parameter with named /** @var Type $name */', function () {
90+
expect(fn () => new PromotedVarNamedSku(''))
91+
->toThrow(TypeError::class, 'Argument $sku must be of type non-empty-string')
92+
;
93+
});
94+
95+
test('enforces @phpstan-var and @psalm-var on promoted parameters', function () {
96+
$order = new PromotedToolingVarOrder(42, 'ORDER-42');
97+
expect($order->id)->toBe(42)->and($order->code)->toBe('ORDER-42');
98+
99+
expect(fn () => new PromotedToolingVarOrder(-1, 'ORDER-42'))
100+
->toThrow(TypeError::class, 'Argument $id must be of type positive-int')
101+
;
102+
103+
expect(fn () => new PromotedToolingVarOrder(42, ''))
104+
->toThrow(TypeError::class, 'Argument $code must be of type non-empty-string')
105+
;
106+
});
107+
108+
test('validates multiple promoted parameters with varied types', function () {
109+
$order = new PromotedMixedMultipleOrder(10, 'Widgets', 5);
110+
expect($order->id)->toBe(10)
111+
->and($order->name)->toBe('Widgets')
112+
->and($order->quantity)->toBe(5)
113+
;
114+
115+
expect(fn () => new PromotedMixedMultipleOrder(-5, 'Widgets', 5))
116+
->toThrow(TypeError::class, 'Argument $id must be of type positive-int')
117+
;
118+
119+
expect(fn () => new PromotedMixedMultipleOrder(10, '', 5))
120+
->toThrow(TypeError::class, 'Argument $name must be of type non-empty-string')
121+
;
122+
123+
expect(fn () => new PromotedMixedMultipleOrder(10, 'Widgets', 150))
124+
->toThrow(TypeError::class, 'Argument $quantity')
125+
;
126+
});
127+
});
128+
129+
describe('PHP 8.4 Asymmetric Visibility Promoted Properties', function () {
130+
test('accepts valid value on asymmetric visibility promoted parameter with /** @var */', function () {
131+
if (PHP_VERSION_ID < 80400) {
132+
expect(true)->toBeTrue();
133+
134+
return;
135+
}
136+
137+
$order = new \TypePHP\Tests\Fixtures\PropertyHooks\AsymmetricPromotedVarOrder(100);
138+
139+
expect($order->orderId)->toBe(100);
140+
});
141+
142+
test('rejects invalid value on asymmetric visibility promoted parameter with /** @var */', function () {
143+
if (PHP_VERSION_ID < 80400) {
144+
expect(true)->toBeTrue();
145+
146+
return;
147+
}
148+
149+
expect(fn () => new \TypePHP\Tests\Fixtures\PropertyHooks\AsymmetricPromotedVarOrder(-50))
150+
->toThrow(TypeError::class, 'Argument $orderId must be of type positive-int, negative int (-50) given')
151+
;
152+
});
153+
});
154+
});

0 commit comments

Comments
 (0)