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
19 changes: 13 additions & 6 deletions src/Internal/Ast/FunctionContractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ public static function inject(
$useDynamicMethodKey,
! $isMagicGet && ! $isMagicCall,
$currentNamespace,
$effectiveAliases
$effectiveAliases,
$isMagicGet
);
}

Expand Down Expand Up @@ -2096,7 +2097,8 @@ private static function wrapNonGeneratorReturns(
bool $useDynamicMethodKey = false,
bool $canGuardReturnCache = true,
?string $currentNamespace = null,
array $effectiveAliases = []
array $effectiveAliases = [],
bool $isMagicGet = false
): array {
self::tagPrecedingAssignments($stmts);

Expand All @@ -2117,7 +2119,8 @@ private static function wrapNonGeneratorReturns(
$useDynamicMethodKey,
$canGuardReturnCache,
$currentNamespace,
$effectiveAliases
$effectiveAliases,
$isMagicGet
) extends NodeVisitorAbstract {
/**
* @param array<string> $byRefParams
Expand All @@ -2141,7 +2144,8 @@ public function __construct(
private bool $useDynamicMethodKey,
private bool $canGuardReturnCache,
private ?string $currentNamespace,
private array $effectiveAliases
private array $effectiveAliases,
private bool $isMagicGet
) {
}

Expand Down Expand Up @@ -2241,7 +2245,10 @@ public function enterNode(Node $n): int|array|null
);
} else {
$checkCall = FunctionContractInjector::buildReturnCheckCall($exprToWrap, $this->thisArg, $this->needsReturnVars);
$n->expr = FunctionContractInjector::buildTernaryReturnExpr($checkCall, $n->getStartLine());
$n->expr = FunctionContractInjector::buildTernaryReturnExpr(
$checkCall,
$this->isMagicGet ? null : $n->getStartLine()
);
}

return $exitStmts !== [] ? [...$exitStmts, $n] : null;
Expand Down Expand Up @@ -2285,7 +2292,7 @@ public function enterNode(Node $n): int|array|null
$byRefGuardStmts = self::buildByRefReturnGuard($checkCall, $retStmt, $fallbackLine);
$newStmts = [...$newStmts, ...$exitStmts, ...$byRefGuardStmts];
} else {
$fallbackLine = $lastStmt instanceof Node\Stmt ? $lastStmt->getStartLine() : null;
$fallbackLine = (! $isMagicGet && $lastStmt instanceof Node\Stmt) ? $lastStmt->getStartLine() : null;
$ternaryExpr = $canGuardReturnCache
? self::buildGuardedTernaryReturnExpr(
new Node\Expr\ConstFetch(new Node\Name('null')),
Expand Down
9 changes: 7 additions & 2 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,10 @@ private static function validateMagicArguments(
$typeNode = new ArrayTypeNode($typeNode);
}

if ($index > 0 && $templates !== []) {
$boundTemplates = TemplateManager::getBoundTemplates($function, $thisObj, $templates);
}

$err = self::validateSingleParam(
$paramName,
$typeNode,
Expand All @@ -2647,8 +2651,9 @@ private static function validateMagicArguments(
$boundTemplates,
$declaredTemplates,
$registry,
[],
$args
$templates,
$args,
$thisObj
);

if ($err !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/Internal/Checker/ReturnChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ private static function evaluateReturn(
return $value;
}

$hasClassTemplates = ($contract['classTemplates'] ?? []) !== [];
$hasClassTemplates = $contract !== []
? ($contract['classTemplates'] ?? []) !== []
: $hasGenerics;
$hasMethodTemplates = ($contract['templates'] ?? []) !== [];

if ($thisObj !== null && ! $hasMethodTemplates) {
Expand Down
13 changes: 9 additions & 4 deletions src/Internal/Diagnostic/ErrorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ private static function filterTrace(

$filtered[] = $frame;

if ($isCallSite && $targetFile === null && isset($frame['file'], $frame['line']) && \is_string($frame['file']) && \is_int($frame['line'])) {
if (
$isCallSite
&& $targetFile === null
&& $targetLine === null
&& isset($frame['file'], $frame['line'])
&& \is_string($frame['file'])
&& \is_int($frame['line'])
) {
$targetFile = $frame['file'];
if ($targetLine === null) {
$targetLine = $frame['line'];
}
$targetLine = $frame['line'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Io/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CacheManager
/**
* Cache version prefix string. Bump this whenever AST printer/transformation rules change.
*/
public const VERSION_PREFIX = 'v0.14.2_';
public const VERSION_PREFIX = 'v0.14.4_';

/**
* Config keys that only steer runtime behaviour and never change the transformed source.
Expand Down
31 changes: 29 additions & 2 deletions src/Internal/Resolver/CallerBoundaryResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,29 @@ final class CallerBoundaryResolver
*/
public static array $nonBypassCalleeCache = [];

/**
* @var array<string, bool> rawFilePath => isVendorFile
*/
private static array $vendorFileCache = [];

public static function reset(): void
{
self::$calleeVendorCache = [];
self::$callerVendorCache = [];
self::$nonBypassCalleeCache = [];
self::$vendorFileCache = [];
}

/**
* Checks if a source file path is located inside a vendor directory (memoized).
*/
public static function isVendorFile(string $file): bool
{
if ($file === '') {
return false;
}

return self::$vendorFileCache[$file] ??= PathMatcher::isVendorPath(PathMatcher::normalizePath($file));
}

/**
Expand Down Expand Up @@ -98,11 +116,20 @@ public static function isCalleeVendor(string $function): bool

try {
if (str_contains($function, '::')) {
[$class] = explode('::', $function, 2);
[$class, $member] = explode('::', $function, 2);
if (class_exists($class) || interface_exists($class) || trait_exists($class)) {
/** @var class-string<object> $class */
$ref = new ReflectionClass($class);
$fileName = $ref->getFileName();
if (str_starts_with($member, '$')) {
$propName = substr($member, 1);
$fileName = $ref->hasProperty($propName)
? $ref->getProperty($propName)->getDeclaringClass()->getFileName()
: $ref->getFileName();
} elseif ($member !== '' && $ref->hasMethod($member)) {
$fileName = $ref->getMethod($member)->getFileName();
} else {
$fileName = $ref->getFileName();
}
}
} elseif (\function_exists($function)) {
$ref = new ReflectionFunction($function);
Expand Down
38 changes: 32 additions & 6 deletions src/Internal/RuntimeTypeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ final class RuntimeTypeChecker

public static bool $inlinePropertiesEnabled = true;

public static bool $vendorBoundaryOnly = true;

public static bool $arrayValidationHybrid = false;

private static ?TypeValidatorRegistry $registry = null;
Expand Down Expand Up @@ -600,7 +602,11 @@ public static function checkPropertyMemberAssign(

if ($res instanceof ErrorMessage) {
$className = \is_object($objectOrClass) ? $objectOrClass::class : (\is_string($objectOrClass) ? $objectOrClass : '');
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)
) {
return $value;
}

Expand Down Expand Up @@ -643,7 +649,11 @@ public static function checkMemberAssign(
);

if ($res instanceof ErrorMessage) {
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($caller ?? '')) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($caller ?? '')
) {
return $value;
}

Expand Down Expand Up @@ -685,7 +695,11 @@ public static function checkMemberUnset(
);

if ($res instanceof ErrorMessage) {
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($caller ?? '')) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($caller ?? '')
) {
return null;
}

Expand Down Expand Up @@ -729,7 +743,11 @@ public static function checkPropertyMemberUnset(
);

if ($res instanceof ErrorMessage) {
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)
) {
return null;
}

Expand Down Expand Up @@ -961,7 +979,11 @@ public static function checkVariable(
);

if ($res instanceof ErrorMessage) {
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($caller !== null ? $caller : '')) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($caller !== null ? $caller : '')
) {
return $value;
}

Expand All @@ -988,7 +1010,11 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
$res = InlineChecker::checkProperty($value, $objectOrClass, $propName, $file, self::getRegistry());

if ($res instanceof ErrorMessage) {
if (IgnoreManager::isCallerIgnored() || CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)) {
if (
IgnoreManager::isCallerIgnored()
|| (self::$vendorBoundaryOnly && CallerBoundaryResolver::isVendorFile($file))
|| CallerBoundaryResolver::shouldBypass($className . '::$' . $propName)
) {
return $value;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Util/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ public static function reset(): void
RuntimeTypeChecker::$paramsOutEnabled = true;
RuntimeTypeChecker::$selfOutEnabled = true;
RuntimeTypeChecker::$inlinePropertiesEnabled = true;
RuntimeTypeChecker::$vendorBoundaryOnly = true;
RuntimeTypeChecker::$arrayValidationHybrid = false;

DocblockParser::reset();
Expand Down Expand Up @@ -974,6 +975,7 @@ private static function syncFlags(array $config): void
RuntimeTypeChecker::$paramsOutEnabled = self::$params && self::$paramsOut;
RuntimeTypeChecker::$selfOutEnabled = self::$selfOut;
RuntimeTypeChecker::$inlinePropertiesEnabled = self::$inlineProperties;
RuntimeTypeChecker::$vendorBoundaryOnly = self::$vendorBoundaryOnly;
RuntimeTypeChecker::$arrayValidationHybrid = (self::$arrayValidation === 'hybrid');
}

Expand Down
Loading
Loading