Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 91 additions & 23 deletions src/Internal/Ast/FunctionContractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
$methodName = $isClassMethod ? strtolower($node->name->toString()) : '';
$isConstructor = $isClassMethod && $methodName === '__construct';
$isMagicLifecycle = $isClassMethod && \in_array($methodName, ['__construct', '__destruct', '__clone'], true);

$isNativeNever = $node->returnType instanceof Node\Identifier && strtolower($node->returnType->name) === 'never';
$isNativeVoid = $node->returnType instanceof Node\Identifier && strtolower($node->returnType->name) === 'void';
$isPrivate = $isClassMethod && $node->isPrivate();

$paramCount = \count($node->params);

$hasParam = self::hasParamContracts(
$docText,
$isClassMethod,
Expand Down Expand Up @@ -184,7 +184,6 @@ private static function hasNonMixedParam(string $docText): bool
break;
}
}

if (! $hasMixed) {
return true;
}
Expand Down Expand Up @@ -281,7 +280,6 @@ private static function buildParamInjections(
Node\Expr $thisArg
): array {
$injectedStmts = [self::buildSetupScopeStmt($params, $thisArg)];

$callableWrappers = self::buildParamWrappers($params, $docText, $thisArg, [self::class, 'isCallableCandidate'], 'wrapCallable');
$iterableWrappers = self::buildParamWrappers($params, $docText, $thisArg, [self::class, 'isIterableCandidate'], 'wrapIterable');

Expand All @@ -304,28 +302,65 @@ private static function buildSetupScopeStmt(array $params, Node\Expr $thisArg):
}
}

$argsExpr = new Node\Expr\Assign(
new Node\Expr\Variable('__typephpArgs'),
new Node\Expr\Array_($arrayItems)
$argsAssign = new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('_typephpArgs'),
new Node\Expr\Array_($arrayItems)
)
);

$checkCall = new Node\Expr\FuncCall(
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::setupScope'),
[
new Node\Arg(new Node\Scalar\MagicConst\Method()),
new Node\Arg($argsExpr),
new Node\Arg(new Node\Expr\Variable('_typephpArgs')),
new Node\Arg($thisArg),
]
);

$throwStmt = self::buildTypeErrorThrowStmt(new Node\Expr\Variable('__typephpErr'));

$ifStmt = new Node\Stmt\If_(
new Node\Expr\Instanceof_(
new Node\Expr\Assign(new Node\Expr\Variable('__typephpErr'), $checkCall),
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();

$noParamCacheCheck = new Node\Expr\BooleanNot(
new Node\Expr\Isset_([
new Node\Expr\ArrayDimFetch(
new Node\Expr\StaticPropertyFetch(
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ParamChecker'),
'noParamContractCache'
),
$cacheKeyExpr
),
])
);

$hasTemplatesCheck = new Node\Expr\BinaryOp\Coalesce(
new Node\Expr\ArrayDimFetch(
new Node\Expr\StaticPropertyFetch(
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker'),
'hasMethodTemplatesCache'
),
$cacheKeyExpr
),
['stmts' => [$throwStmt]]
new Node\Expr\ConstFetch(new Node\Name('false'))
);

$combinedCondition = new Node\Expr\BinaryOp\BooleanOr($noParamCacheCheck, $hasTemplatesCheck);

$ifStmt = new Node\Stmt\If_(
$combinedCondition,
[
'stmts' => [
$argsAssign,
new Node\Stmt\If_(
new Node\Expr\Instanceof_(
new Node\Expr\Assign(new Node\Expr\Variable('__typephpErr'), $checkCall),
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
),
['stmts' => [$throwStmt]]
),
],
]
);

$ifStmt->setAttribute('typephp_injected', true);
Expand All @@ -347,14 +382,15 @@ private static function buildParamWrappers(
string $wrapperMethod
): array {
$wrappers = [];

foreach ($params as $param) {
if ($predicate($param, $docText) && $param->var instanceof Node\Expr\Variable && \is_string($param->var->name)) {
$paramName = $param->var->name;
$expr = new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable($paramName),
new Node\Expr\FuncCall(
new Node\Name\FullyQualified("TypePHP\\Internal\\RuntimeTypeChecker::{$wrapperMethod}"),
new Node\Name\FullyQualified("TypePHP\Internal\RuntimeTypeChecker::{$wrapperMethod}"),
[
new Node\Arg(new Node\Scalar\MagicConst\Method()),
new Node\Arg(new Node\Scalar\String_($paramName)),
Expand Down Expand Up @@ -408,11 +444,9 @@ private static function typeMatchesName(Node\Identifier|Node\Name|Node\ComplexTy
if ($type instanceof Node\Identifier) {
return isset($targetNames[strtolower($type->name)]);
}

if ($type instanceof Node\Name) {
return isset($targetNames[strtolower($type->getLast())]);
}

if ($type instanceof Node\UnionType || $type instanceof Node\IntersectionType) {
foreach ($type->types as $t) {
if (self::typeMatchesName($t, $targetNames)) {
Expand Down Expand Up @@ -460,7 +494,7 @@ public static function buildTypeErrorThrowStmt(Node\Expr $errorVar): Node\Stmt\E
public static function buildReturnCheckCall(Node\Expr $exprToWrap, Node\Expr $thisArg, bool $needsReturnVars = false): Node\Expr\FuncCall
{
$varsArg = $needsReturnVars
? new Node\Expr\Variable('__typephpArgs')
? new Node\Expr\Variable('_typephpArgs')
: new Node\Expr\Array_();

return new Node\Expr\FuncCall(
Expand Down Expand Up @@ -569,7 +603,6 @@ public function enterNode(Node $n): int|Node|null
if ($n->getAttribute('typephp_wrapped') === true) {
return null;
}

$n->setAttribute('typephp_wrapped', true);

return FunctionContractInjector::buildWrappedYieldNode($n, $this->thisArg);
Expand All @@ -579,9 +612,7 @@ public function enterNode(Node $n): int|Node|null
if ($n->getAttribute('typephp_wrapped') === true) {
return null;
}

$n->setAttribute('typephp_wrapped', true);

$n->expr = new Node\Expr\FuncCall(
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
[
Expand Down Expand Up @@ -629,15 +660,34 @@ public function enterNode(Node $n): int|array|null
if ($n->getAttribute('typephp_var_wrapped') === true) {
return null;
}

$exprToWrap = $n->expr ?? new Node\Expr\ConstFetch(new Node\Name('null'));
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);

if ($this->isNativeVoid) {
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);

return FunctionContractInjector::buildVoidReturnGuard($checkCall);
}

$n->expr = FunctionContractInjector::buildTernaryReturnExpr($checkCall);
// Call-site cache bypass for return checks
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
$cacheCheck = new Node\Expr\Isset_([
new Node\Expr\ArrayDimFetch(
new Node\Expr\StaticPropertyFetch(
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ReturnChecker'),
'noReturnContractCache'
),
$cacheKeyExpr
),
]);

$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);
$ternaryExpr = FunctionContractInjector::buildTernaryReturnExpr($checkCall);

$n->expr = new Node\Expr\Ternary(
$cacheCheck,
$exprToWrap,
$ternaryExpr
);
}

return null;
Expand All @@ -654,7 +704,25 @@ public function enterNode(Node $n): int|array|null
if ($isNativeVoid) {
$newStmts = [...$newStmts, ...self::buildVoidReturnGuard($checkCall)];
} else {
$retStmt = new Node\Stmt\Return_(self::buildTernaryReturnExpr($checkCall));
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
$cacheCheck = new Node\Expr\Isset_([
new Node\Expr\ArrayDimFetch(
new Node\Expr\StaticPropertyFetch(
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ReturnChecker'),
'noReturnContractCache'
),
$cacheKeyExpr
),
]);

$ternaryExpr = self::buildTernaryReturnExpr($checkCall);
$fallbackExpr = new Node\Expr\Ternary(
$cacheCheck,
new Node\Expr\ConstFetch(new Node\Name('null')),
$ternaryExpr
);

$retStmt = new Node\Stmt\Return_($fallbackExpr);
$retStmt->setAttribute('typephp_injected', true);
$newStmts[] = $retStmt;
}
Expand Down
Loading
Loading