Skip to content

Commit 2874e86

Browse files
committed
Add debug probes in ContractParser, ParamChecker, and StreamWrapper for enhanced tracing; improve readability by standardizing arrow function syntax
1 parent eb93b50 commit 2874e86

3 files changed

Lines changed: 49 additions & 28 deletions

File tree

src/Contract/ContractParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ public static function reset(): void
6161
TypeValidatorRegistry::reset();
6262
}
6363

64-
/**
65-
* Parses PHPDoc contracts for a function or class method.
66-
*
67-
* @return array{types: array<string, TypeNode>, templates: array<string, TemplateTagValueNode>, classTemplates: array<string, TemplateTagValueNode>, return: ?TypeNode, aliases: array<string, TypeNode>}
68-
*/
6964
public static function parse(string $function): array
7065
{
7166
if (isset(self::$cache[$function])) {
@@ -105,6 +100,11 @@ public static function parse(string $function): array
105100
$contract = ['types' => [], 'templates' => [], 'classTemplates' => [], 'return' => null, 'aliases' => []];
106101
}
107102

103+
// >>> DEBUG PROBE <<<
104+
if (str_contains($function, 'Container') || str_contains($function, 'Producer')) {
105+
fwrite(STDERR, "[TYPEPHP_DEBUG ContractParser] Func: $function | Types: " . json_encode(array_keys($contract['types'])) . " | ClassTemplates: " . json_encode(array_keys($contract['classTemplates'])) . " | MethodTemplates: " . json_encode(array_keys($contract['templates'])) . "\n");
106+
}
107+
108108
return self::$cache[$function] = $contract;
109109
}
110110

@@ -732,7 +732,7 @@ public static function substituteAliases(TypeNode $node, array $aliases): TypeNo
732732

733733
if ($node instanceof CallableTypeNode) {
734734
$parameters = array_map(
735-
fn (CallableTypeParameterNode $param) => new CallableTypeParameterNode(
735+
fn(CallableTypeParameterNode $param) => new CallableTypeParameterNode(
736736
self::substituteAliases($param->type, $aliases),
737737
$param->isReference,
738738
$param->isVariadic,
@@ -766,7 +766,7 @@ public static function substituteAliases(TypeNode $node, array $aliases): TypeNo
766766
if ($node instanceof GenericTypeNode) {
767767
$genericType = self::substituteAliases($node->type, $aliases);
768768
$genericTypes = array_map(
769-
fn ($t) => self::substituteAliases($t, $aliases),
769+
fn($t) => self::substituteAliases($t, $aliases),
770770
$node->genericTypes
771771
);
772772

@@ -783,14 +783,14 @@ public static function substituteAliases(TypeNode $node, array $aliases): TypeNo
783783

784784
if ($node instanceof UnionTypeNode) {
785785
return new UnionTypeNode(array_map(
786-
fn ($t) => self::substituteAliases($t, $aliases),
786+
fn($t) => self::substituteAliases($t, $aliases),
787787
$node->types
788788
));
789789
}
790790

791791
if ($node instanceof IntersectionTypeNode) {
792792
return new IntersectionTypeNode(array_map(
793-
fn ($t) => self::substituteAliases($t, $aliases),
793+
fn($t) => self::substituteAliases($t, $aliases),
794794
$node->types
795795
));
796796
}

src/Internal/Checker/ParamChecker.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ private static function validateSingleParam(
333333
return self::resolveTemplateParam($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $registry);
334334
}
335335

336+
if (str_contains($effectiveFunction, 'Container') || str_contains($effectiveFunction, 'Producer')) {
337+
fwrite(STDERR, "[TYPEPHP_DEBUG validateSingleParam] Param: \$$paramName | ResolvedTypeNode: " . (string)$typeNode . " | Val: " . get_debug_type($val) . "\n");
338+
}
339+
336340
return $registry->validate($val, $typeNode, $effectiveFunction . '(): Argument $' . $paramName);
337341
}
338342

src/Internal/StreamWrapper.php

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ public static function transformSource(string $source, string $filePath = ''): s
123123

124124
$parser = (new ParserFactory())->createForNewestSupportedVersion();
125125

126-
try {
126+
try {
127127
$oldStmts = $parser->parse($source);
128128
if ($oldStmts === null) {
129129
return $source;
130130
}
131131
} catch (\Throwable $e) {
132+
fwrite(STDERR, "[TYPEPHP_DEBUG AST_PARSE_ERROR] File: $filePath | Error: " . $e->getMessage() . " on line " . $e->getLine() . "\n");
132133
return $source;
133134
}
134135

@@ -191,14 +192,30 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
191192
}
192193

193194
self::unregister();
194-
$exists = self::silent(fn () => file_exists($path));
195-
$resolvedPath = $exists ? realpath($path) : '';
195+
$exists = self::silent(fn() => file_exists($path));
196+
$resolvedPath = $exists ? realpath($path) : false;
196197
self::register();
197198

198-
$isAppFile = $exists && ! self::isReadOnlyCall() && self::isApplicationFile($path, $resolvedPath);
199+
if (! $exists || $resolvedPath === false) {
200+
return $this->openDirectHandle($path, $mode);
201+
}
202+
203+
$normalizedResolved = str_replace('\\', '/', $resolvedPath);
204+
205+
$isApp = self::isApplicationFile($path, $resolvedPath);
206+
$isReadOnly = self::isReadOnlyCall();
199207

200-
if (! $isAppFile || $resolvedPath === false) {
201-
return $this->openDirectHandle(($resolvedPath !== false && $resolvedPath !== '') ? $resolvedPath : $path, $mode);
208+
// >>> DEBUG PROBE <<<
209+
if (str_contains($path, 'Container') || str_contains($path, 'Producer') || str_contains($path, 'GenericsAndInheritance')) {
210+
fwrite(STDERR, "[TYPEPHP_DEBUG stream_open] Path: $path | isApp: " . ($isApp ? 'TRUE' : 'FALSE') . " | isReadOnly: " . ($isReadOnly ? 'TRUE' : 'FALSE') . " | CacheEnabled: " . (self::$cacheEnabled ? 'TRUE' : 'FALSE') . "\n");
211+
}
212+
213+
if (! $isApp) {
214+
return $this->openDirectHandle($normalizedResolved, $mode);
215+
}
216+
217+
if ($isReadOnly) {
218+
return $this->openDirectHandle($normalizedResolved, $mode);
202219
}
203220

204221
self::unregister();
@@ -219,7 +236,7 @@ private function openDirectHandle(string $targetFile, string $mode): bool
219236
{
220237
self::unregister();
221238
/** @var resource|false $handle */
222-
$handle = self::silent(fn () => fopen($targetFile, $mode));
239+
$handle = self::silent(fn() => fopen($targetFile, $mode));
223240
$this->handle = $handle !== false ? $handle : null;
224241
self::register();
225242

@@ -359,7 +376,7 @@ public function url_stat(string $path, int $flags): array|false
359376

360377
self::unregister();
361378
/** @var array<int|string, int>|false $result */
362-
$result = self::silent(fn () => stat($path));
379+
$result = self::silent(fn() => stat($path));
363380
self::register();
364381

365382
// Only cache positive results (existing files/dirs)
@@ -382,11 +399,11 @@ public function stream_metadata(string $path, int $option, mixed $value): bool
382399
$valueArray = \is_array($value) ? $value : [];
383400
$time = $valueArray[0] ?? time();
384401
$atime = $valueArray[1] ?? $time;
385-
$result = (bool) self::silent(fn () => touch($path, (int) $time, (int) $atime));
402+
$result = (bool) self::silent(fn() => touch($path, (int) $time, (int) $atime));
386403
} elseif ($option === STREAM_META_ACCESS) {
387404
/** @var int $mode */
388405
$mode = \is_int($value) ? $value : 0777;
389-
$result = (bool) self::silent(fn () => chmod($path, $mode));
406+
$result = (bool) self::silent(fn() => chmod($path, $mode));
390407
}
391408
self::register();
392409

@@ -397,7 +414,7 @@ public function dir_opendir(string $path, int $options): bool
397414
{
398415
self::unregister();
399416
/** @var resource|false $dh */
400-
$dh = self::silent(fn () => opendir($path));
417+
$dh = self::silent(fn() => opendir($path));
401418
$this->dirHandle = $dh !== false ? $dh : null;
402419
self::register();
403420

@@ -440,7 +457,7 @@ public function mkdir(string $path, int $mode, int $options): bool
440457
unset(self::$statCache[$normalized]);
441458

442459
self::unregister();
443-
$result = (bool) self::silent(fn () => mkdir($path, $mode, (bool) ($options & STREAM_MKDIR_RECURSIVE)));
460+
$result = (bool) self::silent(fn() => mkdir($path, $mode, (bool) ($options & STREAM_MKDIR_RECURSIVE)));
444461
self::register();
445462

446463
return $result;
@@ -452,7 +469,7 @@ public function rmdir(string $path, int $options): bool
452469
unset(self::$statCache[$normalized]);
453470

454471
self::unregister();
455-
$result = (bool) self::silent(fn () => rmdir($path));
472+
$result = (bool) self::silent(fn() => rmdir($path));
456473
self::register();
457474

458475
return $result;
@@ -464,7 +481,7 @@ public function unlink(string $path): bool
464481
unset(self::$statCache[$normalized]);
465482

466483
self::unregister();
467-
$result = (bool) self::silent(fn () => unlink($path));
484+
$result = (bool) self::silent(fn() => unlink($path));
468485
self::register();
469486

470487
return $result;
@@ -477,7 +494,7 @@ public function rename(string $pathFrom, string $pathTo): bool
477494
unset(self::$statCache[$normFrom], self::$statCache[$normTo]);
478495

479496
self::unregister();
480-
$result = (bool) self::silent(fn () => rename($pathFrom, $pathTo));
497+
$result = (bool) self::silent(fn() => rename($pathFrom, $pathTo));
481498
self::register();
482499

483500
return $result;
@@ -490,7 +507,7 @@ public function rename(string $pathFrom, string $pathTo): bool
490507
private static function isReadOnlyCall(): bool
491508
{
492509
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
493-
510+
494511
$caller1 = strtolower($trace[1]['function'] ?? '');
495512
$caller2 = strtolower($trace[2]['function'] ?? '');
496513

@@ -508,7 +525,7 @@ private static function isReadOnlyCall(): bool
508525
*/
509526
private static function silent(callable $callback): mixed
510527
{
511-
set_error_handler(fn () => true);
528+
set_error_handler(fn() => true);
512529

513530
try {
514531
return $callback();
@@ -581,7 +598,7 @@ private function openCachedStream(string $resolvedPath, string $mode): bool
581598
{
582599
$cacheDir = self::$cacheDir;
583600
if (! is_dir($cacheDir)) {
584-
self::silent(fn () => mkdir($cacheDir, 0777, true));
601+
self::silent(fn() => mkdir($cacheDir, 0777, true));
585602
}
586603

587604
$cachedFile = CacheManager::getCachedFilePath($resolvedPath);
@@ -663,4 +680,4 @@ private static function extractAndSeedFileMetadata(array $stmts, string $filePat
663680

664681
SpecialTypeResolver::seedFileMetadata($filePath, $namespace, $imports, $classTraitUseDocs);
665682
}
666-
}
683+
}

0 commit comments

Comments
 (0)