Skip to content

Commit b210808

Browse files
authored
Improve ignore-doc annoation to also work with blacklisted file to be able to selectivl ignore type checking for certain method/class (#63)
* Implement IgnoreManager for handling @typephp-ignore tags and enhance runtime type checking to improve testing experience * Refactor IgnoreManager and RuntimeTypeChecker for improved clarity and performance; update .gitignore and typephp.php configurations * Enhance IgnoreManager tests with dedicated stub-based caller and reset logic * Add runtime configuration override method to Config class
1 parent b28230b commit b210808

7 files changed

Lines changed: 554 additions & 18 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
composer.lock
66
index.php
77
benchmark.php
8-
.php-cs-fixer.cache
8+
.php-cs-fixer.cache
9+
/app

‎src/Internal/Io/StreamWrapper.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use TypePHP\Internal\Ast\TypePHPPrinter;
1515
use TypePHP\Internal\Resolver\SpecialTypeResolver;
1616
use TypePHP\Internal\Util\Config;
17+
use TypePHP\Internal\Util\IgnoreManager;
1718
use TypePHP\Internal\Util\PathMatcher;
1819

1920
/**
@@ -178,8 +179,14 @@ public static function unregister(): void
178179
*/
179180
public static function transformSource(string $source, string $filePath = ''): string
180181
{
181-
if (Config::isRespectIgnoreTagsEnabled() && (str_contains($source, '@typephp-ignore-file') || str_contains($source, '@typephp-disable-file'))) {
182-
return $source;
182+
if (str_contains($source, '@typephp-ignore-file') || str_contains($source, '@typephp-disable-file')) {
183+
if ($filePath !== '') {
184+
IgnoreManager::registerIgnoredFile($filePath);
185+
}
186+
187+
if (Config::isRespectIgnoreTagsEnabled()) {
188+
return $source;
189+
}
183190
}
184191

185192
$originalLineCount = substr_count($source, "\n");

‎src/Internal/RuntimeTypeChecker.php‎

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use TypePHP\Internal\Docblock\DocblockParser;
1616
use TypePHP\Internal\Generics\TemplateManager;
1717
use TypePHP\Internal\Util\Config;
18+
use TypePHP\Internal\Util\IgnoreManager;
1819
use TypePHP\Internal\Validator\TypeValidatorRegistry;
1920
use TypePHP\Internal\Wrapper\CallableWrapper;
2021
use TypePHP\Internal\Wrapper\IterableWrapper;
@@ -26,6 +27,20 @@ final class RuntimeTypeChecker
2627
{
2728
private static ?TypeValidatorRegistry $registry = null;
2829

30+
/**
31+
* @var array<string, bool>
32+
*/
33+
private static array $hasMethodTemplatesCache = [];
34+
35+
/**
36+
* Resets runtime caches.
37+
*/
38+
public static function reset(): void
39+
{
40+
self::$hasMethodTemplatesCache = [];
41+
IgnoreManager::reset();
42+
}
43+
2944
/**
3045
* Returns whether TypePHP is globally enabled in configuration.
3146
*/
@@ -43,7 +58,13 @@ public static function bindInstanceFromNode(object $instance, GenericTypeNode $t
4358
return null;
4459
}
4560

46-
return TemplateManager::bindInstanceFromNode($instance, $typeNode, $context, $forceBind);
61+
$err = TemplateManager::bindInstanceFromNode($instance, $typeNode, $context, $forceBind);
62+
63+
if ($err !== null && IgnoreManager::isCallerIgnored()) {
64+
return null;
65+
}
66+
67+
return $err;
4768
}
4869

4970
/**
@@ -61,7 +82,7 @@ public static function checkVariable(
6182
return $value;
6283
}
6384

64-
return InlineChecker::checkVariable(
85+
$res = InlineChecker::checkVariable(
6586
$value,
6687
$typeString,
6788
$varName,
@@ -70,6 +91,12 @@ public static function checkVariable(
7091
$caller,
7192
$thisOrClass
7293
);
94+
95+
if ($res instanceof ErrorMessage && IgnoreManager::isCallerIgnored()) {
96+
return $value;
97+
}
98+
99+
return $res;
73100
}
74101

75102
/**
@@ -86,13 +113,14 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
86113
return $value;
87114
}
88115

89-
return InlineChecker::checkProperty($value, $objectOrClass, $propName, $file, self::getRegistry());
90-
}
116+
$res = InlineChecker::checkProperty($value, $objectOrClass, $propName, $file, self::getRegistry());
91117

92-
/**
93-
* @var array<string, bool>
94-
*/
95-
private static array $hasMethodTemplatesCache = [];
118+
if ($res instanceof ErrorMessage && IgnoreManager::isCallerIgnored()) {
119+
return $value;
120+
}
121+
122+
return $res;
123+
}
96124

97125
/**
98126
* Initialises generic call frames and returns a ScopeCleaner that pops the call frame on destruction.
@@ -123,6 +151,10 @@ public static function setupScope(string $function, array $vars, object|string|n
123151
$err = ParamChecker::checkParams($function, $vars, $thisOrClass, self::getRegistry(), $effectiveFunction);
124152

125153
if ($err !== null) {
154+
if (IgnoreManager::isCallerIgnored()) {
155+
return null;
156+
}
157+
126158
TemplateManager::popCallFrame($effectiveFunction);
127159

128160
return $err;
@@ -151,7 +183,13 @@ public static function checkParams(string $function, array $vars, object|string|
151183
return null;
152184
}
153185

154-
return ParamChecker::checkParams($function, $vars, $thisOrClass, self::getRegistry());
186+
$err = ParamChecker::checkParams($function, $vars, $thisOrClass, self::getRegistry());
187+
188+
if ($err !== null && IgnoreManager::isCallerIgnored()) {
189+
return null;
190+
}
191+
192+
return $err;
155193
}
156194

157195
/**
@@ -172,14 +210,19 @@ public static function checkReturn(string $function, mixed $value, object|string
172210
$thisObj = \is_object($thisOrClass) ? $thisOrClass : null;
173211
$effectiveFunction = ParamChecker::resolveEffectiveFunction($function, $thisOrClass, $thisObj);
174212

175-
// Fast-path: if return type is unconstrained, skip validation.
176213
if (ReturnChecker::isReturnUnconstrained($effectiveFunction)) {
177214
return $value;
178215
}
179216

180217
$vars ??= [];
181218

182-
return ReturnChecker::checkReturn($function, $value, $thisOrClass, $vars, self::getRegistry(), [self::class, 'wrapIterable']);
219+
$res = ReturnChecker::checkReturn($function, $value, $thisOrClass, $vars, self::getRegistry(), [self::class, 'wrapIterable']);
220+
221+
if ($res instanceof ErrorMessage && IgnoreManager::isCallerIgnored()) {
222+
return $value;
223+
}
224+
225+
return $res;
183226
}
184227

185228
/**
@@ -191,7 +234,13 @@ public static function checkSend(string $function, mixed $sendValue, object|stri
191234
return $sendValue;
192235
}
193236

194-
return GeneratorChecker::checkSend($function, $sendValue, self::getRegistry(), $thisOrClass);
237+
$res = GeneratorChecker::checkSend($function, $sendValue, self::getRegistry(), $thisOrClass);
238+
239+
if ($res instanceof ErrorMessage && IgnoreManager::isCallerIgnored()) {
240+
return $sendValue;
241+
}
242+
243+
return $res;
195244
}
196245

197246
/**
@@ -203,7 +252,13 @@ public static function checkYield(string $function, mixed $key, mixed $value, ob
203252
return $value;
204253
}
205254

206-
return GeneratorChecker::checkYield($function, $key, $value, self::getRegistry(), $thisOrClass);
255+
$res = GeneratorChecker::checkYield($function, $key, $value, self::getRegistry(), $thisOrClass);
256+
257+
if ($res instanceof ErrorMessage && IgnoreManager::isCallerIgnored()) {
258+
return $value;
259+
}
260+
261+
return $res;
207262
}
208263

209264
/**

‎src/Internal/Util/Config.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use TypePHP\Internal\Io\StreamWrapper;
1414
use TypePHP\Internal\Resolver\HierarchyResolver;
1515
use TypePHP\Internal\Resolver\SpecialTypeResolver;
16+
use TypePHP\Internal\RuntimeTypeChecker;
1617

1718
/**
1819
* Global configuration manager for loading and dynamically overriding settings.
@@ -308,6 +309,8 @@ public static function set(array $config): void
308309
StubManager::reset();
309310
SpecialTypeResolver::reset();
310311
CacheManager::reset();
312+
IgnoreManager::reset();
313+
RuntimeTypeChecker::reset();
311314
}
312315

313316
/**
@@ -370,6 +373,8 @@ public static function reset(): void
370373
StubManager::reset();
371374
SpecialTypeResolver::reset();
372375
CacheManager::reset();
376+
IgnoreManager::reset();
377+
RuntimeTypeChecker::reset();
373378
}
374379

375380
/**

0 commit comments

Comments
 (0)