Skip to content

Commit dcc8730

Browse files
committed
Enhance FunctionContractInjector to support promoted properties for callable constructors
1 parent d3f636f commit dcc8730

2 files changed

Lines changed: 149 additions & 12 deletions

File tree

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -464,22 +464,33 @@ private static function buildParamWrappers(
464464
foreach ($params as $param) {
465465
if ($predicate($param, $docText) && $param->var instanceof Node\Expr\Variable && \is_string($param->var->name)) {
466466
$paramName = $param->var->name;
467-
$expr = new Node\Stmt\Expression(
468-
new Node\Expr\Assign(
469-
new Node\Expr\Variable($paramName),
470-
new Node\Expr\FuncCall(
471-
new Node\Name\FullyQualified("TypePHP\Internal\RuntimeTypeChecker::{$wrapperMethod}"),
472-
[
473-
new Node\Arg(new Node\Scalar\MagicConst\Method()),
474-
new Node\Arg(new Node\Scalar\String_($paramName)),
475-
new Node\Arg(new Node\Expr\Variable($paramName)),
476-
new Node\Arg($thisArg),
477-
]
478-
)
467+
$assignExpr = new Node\Expr\Assign(
468+
new Node\Expr\Variable($paramName),
469+
new Node\Expr\FuncCall(
470+
new Node\Name\FullyQualified("TypePHP\Internal\RuntimeTypeChecker::{$wrapperMethod}"),
471+
[
472+
new Node\Arg(new Node\Scalar\MagicConst\Method()),
473+
new Node\Arg(new Node\Scalar\String_($paramName)),
474+
new Node\Arg(new Node\Expr\Variable($paramName)),
475+
new Node\Arg($thisArg),
476+
]
479477
)
480478
);
479+
480+
$expr = new Node\Stmt\Expression($assignExpr);
481481
$expr->setAttribute('typephp_injected', true);
482482
$wrappers[] = $expr;
483+
484+
if ($param->isPromoted()) {
485+
$propAssign = new Node\Stmt\Expression(
486+
new Node\Expr\Assign(
487+
new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $paramName),
488+
new Node\Expr\Variable($paramName)
489+
)
490+
);
491+
$propAssign->setAttribute('typephp_injected', true);
492+
$wrappers[] = $propAssign;
493+
}
483494
}
484495
}
485496

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypePHP\Tests\TypeChecking\CallablesAndIterators;
6+
7+
use Closure;
8+
use TypePHP\Exception\TypeError;
9+
10+
interface PromotedBase
11+
{
12+
}
13+
14+
final class PromotedA implements PromotedBase
15+
{
16+
public function __construct(public string $name = 'A')
17+
{
18+
}
19+
}
20+
21+
final class PromotedUnrelated
22+
{
23+
}
24+
25+
final class PromotedHelper
26+
{
27+
public function format(?PromotedA $a = null): PromotedBase
28+
{
29+
return new PromotedA();
30+
}
31+
}
32+
33+
final class StoredClosureClass
34+
{
35+
public Closure $factory;
36+
37+
/**
38+
* @param Closure(?PromotedA=): PromotedBase $factory
39+
*/
40+
public function __construct(Closure $factory)
41+
{
42+
$this->factory = $factory;
43+
}
44+
}
45+
46+
final class PromotedClosureClass
47+
{
48+
/**
49+
* @param Closure(?PromotedA=): PromotedBase $factory
50+
*/
51+
public function __construct(public Closure $factory)
52+
{
53+
}
54+
}
55+
56+
final class PromotedCallableClass
57+
{
58+
/**
59+
* @param callable(?PromotedA=): PromotedBase $formatter
60+
*/
61+
public function __construct(public mixed $formatter)
62+
{
63+
}
64+
}
65+
66+
describe('Promoted Property Callable Contracts & Consistency', function () {
67+
test('enforces parameter type validation when invoking closure on promoted constructor property', function () {
68+
$closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA();
69+
70+
$promoted = new PromotedClosureClass($closure);
71+
72+
expect(($promoted->factory)(new PromotedA()))->toBeInstanceOf(PromotedBase::class);
73+
74+
expect(($promoted->factory)())->toBeInstanceOf(PromotedBase::class);
75+
76+
expect(fn () => ($promoted->factory)(new PromotedUnrelated()))
77+
->toThrow(TypeError::class)
78+
;
79+
});
80+
81+
test('enforces return type validation when invoking closure on promoted constructor property', function () {
82+
$badClosure = static fn (?PromotedA $a = null): mixed => new PromotedUnrelated();
83+
84+
$promoted = new PromotedClosureClass($badClosure);
85+
86+
expect(fn () => ($promoted->factory)(new PromotedA()))
87+
->toThrow(TypeError::class)
88+
;
89+
});
90+
91+
test('behaves consistently between standard parameter assignment and promoted property', function () {
92+
$closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA();
93+
94+
$stored = new StoredClosureClass($closure);
95+
$promoted = new PromotedClosureClass($closure);
96+
97+
expect(fn () => ($stored->factory)(new PromotedUnrelated()))
98+
->toThrow(TypeError::class)
99+
;
100+
101+
expect(fn () => ($promoted->factory)(new PromotedUnrelated()))
102+
->toThrow(TypeError::class)
103+
;
104+
});
105+
106+
test('enforces contracts when callable is promoted in constructor (Closure, Array Callable, Invokable)', function () {
107+
$helper = new PromotedHelper();
108+
109+
$promotedArrayCallable = new PromotedCallableClass([$helper, 'format']);
110+
111+
expect(($promotedArrayCallable->formatter)(new PromotedA()))->toBeInstanceOf(PromotedBase::class);
112+
113+
expect(fn () => ($promotedArrayCallable->formatter)(new PromotedUnrelated()))
114+
->toThrow(TypeError::class)
115+
;
116+
117+
$closure = static fn (?PromotedA $a = null): PromotedBase => new PromotedA();
118+
$promotedClosure = new PromotedCallableClass($closure);
119+
120+
expect(($promotedClosure->formatter)(new PromotedA()))->toBeInstanceOf(PromotedBase::class);
121+
122+
expect(fn () => ($promotedClosure->formatter)(new PromotedUnrelated()))
123+
->toThrow(TypeError::class)
124+
;
125+
});
126+
});

0 commit comments

Comments
 (0)