Skip to content

Commit 73ee351

Browse files
authored
Reorganize src folders and Major Performance Optimization (#60)
* Started Reorganizing folders * Progress on Reorganizing files and folders * Organizing folders * Organizing folders * fix code styling * fix phpstan errors * Refactor internal directory patterns and update path normalization logic * fix failing test * Fix class trait use documentation initialization in SpecialTypeResolver * Reorganize Test files * Add unit tests for CommandRunner functionality * Enhance StreamWrapper functionality and add tests for string literal preservation * Major performance optimization * Enhance DocblockNormalizer and SpecialTypeResolver with additional checks; refactor TypeValidatorRegistry for improved validation structure * Rename ContractParser to DocblockParser * bump php doc parser version * Update phpstan/phpdoc-parser version to ^2.3.5 in composer.json * trace failing 8.2 ci test * Remove redundant --verbose flag from Pest runtime type checking command * Refactor Node name usage to use FullyQualified for consistency * Enhance debugging output in ClassConstKeyShapeTest and StreamWrapper for better traceability * Refactor ParamChecker to return validation error immediately instead of logging * Remove manual debug checks from TypeError test in ClassConstKeyShapeTest * Fix formatting and remove redundant debug statements across multiple files * Update CI workflow to run full test suite with coverage and adjust PHPStan parser version * Update phpdoc-parser version and fix namespace usage in NodeBuilder * Update nikic/php-parser version to ^5.8 in composer.json * Reorganize CI workflow steps and update nikic/php-parser version to ^5.3 * Enhance CI workflow to run full test suite with coverage and streamline Pest test execution; reset Config and StreamWrapper in tests * fix code styling * Update phpstan/phpdoc-parser version to ^2.3 in composer.json
1 parent 9e1692e commit 73ee351

122 files changed

Lines changed: 1010 additions & 556 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/typephp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ foreach ($autoloadFiles as $file) {
1515
}
1616
}
1717

18-
use TypePHP\Command\CommandRunner;
18+
use TypePHP\Internal\Cli\CommandRunner;
1919

2020
$exitCode = CommandRunner::run(array_slice($argv, 1));
2121
exit($exitCode);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"require": {
2828
"php": "^8.1",
29-
"phpstan/phpdoc-parser": "^2.0",
29+
"phpstan/phpdoc-parser": "^2.3",
3030
"nikic/php-parser": "^5.3"
3131
},
3232
"require-dev": {
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88
use PhpParser\NodeVisitorAbstract;
9-
use TypePHP\Contract\DocblockExtractor;
10-
use TypePHP\Internal\Visitor\FunctionContractInjector;
11-
use TypePHP\Internal\Visitor\NodeBuilder;
12-
use TypePHP\Internal\Visitor\PropertyHookInjector;
13-
use TypePHP\Internal\Visitor\ScopeManager;
9+
use TypePHP\Internal\Docblock\DocblockExtractor;
1410

1511
/**
1612
* @internal AST Node Visitor that injects contract checks, scope tracking, property hook validation, and parameter/return wrappers into functions and methods.
@@ -31,7 +27,8 @@ public function __construct()
3127
*/
3228
public function enterNode(Node $node): ?array
3329
{
34-
if ($node instanceof Node\Stmt\Function_
30+
if (
31+
$node instanceof Node\Stmt\Function_
3532
|| $node instanceof Node\Stmt\ClassMethod
3633
|| $node instanceof Node\Expr\Closure
3734
|| $node instanceof Node\Expr\ArrowFunction
@@ -165,12 +162,12 @@ public function leaveNode(Node $node): Node|null
165162
$node->setAttribute('typephp_wrapped', value: true);
166163

167164
return new Node\Expr\FuncCall(
168-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::cloneInstance'),
165+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::cloneInstance'),
169166
[
170167
new Node\Arg(
171168
new Node\Expr\Clone_(
172169
new Node\Expr\FuncCall(
173-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::prepareClone'),
170+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::prepareClone'),
174171
[new Node\Arg($node->expr)]
175172
)
176173
)
@@ -180,7 +177,8 @@ public function leaveNode(Node $node): Node|null
180177
);
181178
}
182179

183-
if ($node instanceof Node\Stmt\Function_
180+
if (
181+
$node instanceof Node\Stmt\Function_
184182
|| $node instanceof Node\Stmt\ClassMethod
185183
|| $node instanceof Node\Expr\Closure
186184
|| $node instanceof Node\Expr\ArrowFunction

src/Internal/Visitor/FunctionContractInjector.php renamed to src/Internal/Ast/FunctionContractInjector.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal\Visitor;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88
use PhpParser\NodeTraverser;
@@ -204,7 +204,7 @@ private static function buildSetupScopeStmt(array $params, Node\Expr $thisArg):
204204
);
205205

206206
$checkCall = new Node\Expr\FuncCall(
207-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::setupScope'),
207+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::setupScope'),
208208
[
209209
new Node\Arg(new Node\Scalar\MagicConst\Method()),
210210
new Node\Arg($argsExpr),
@@ -217,7 +217,7 @@ private static function buildSetupScopeStmt(array $params, Node\Expr $thisArg):
217217
$ifStmt = new Node\Stmt\If_(
218218
new Node\Expr\Instanceof_(
219219
new Node\Expr\Assign(new Node\Expr\Variable('__typephpErr'), $checkCall),
220-
new Node\Name('\TypePHP\Internal\ErrorMessage')
220+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
221221
),
222222
['stmts' => [$throwStmt]]
223223
);
@@ -242,7 +242,7 @@ private static function buildCallableParamWrappers(array $params, string $docTex
242242
new Node\Expr\Assign(
243243
new Node\Expr\Variable($paramName),
244244
new Node\Expr\FuncCall(
245-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::wrapCallable'),
245+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::wrapCallable'),
246246
[
247247
new Node\Arg(new Node\Scalar\MagicConst\Method()),
248248
new Node\Arg(new Node\Scalar\String_($paramName)),
@@ -275,7 +275,7 @@ private static function buildIterableParamWrappers(array $params, string $docTex
275275
new Node\Expr\Assign(
276276
new Node\Expr\Variable($paramName),
277277
new Node\Expr\FuncCall(
278-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
278+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
279279
[
280280
new Node\Arg(new Node\Scalar\MagicConst\Method()),
281281
new Node\Arg(new Node\Scalar\String_($paramName)),
@@ -373,12 +373,12 @@ public static function buildTypeErrorThrowStmt(Node\Expr $errorVar): Node\Stmt\E
373373
return new Node\Stmt\Expression(
374374
new Node\Expr\Throw_(
375375
new Node\Expr\StaticCall(
376-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
376+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
377377
'prepareException',
378378
[
379379
new Node\Arg(
380380
new Node\Expr\New_(
381-
new Node\Name('\TypePHP\Exception\TypeError'),
381+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
382382
[
383383
new Node\Arg(
384384
new Node\Expr\MethodCall($errorVar, 'getMessage')
@@ -399,7 +399,7 @@ public static function buildReturnCheckCall(Node\Expr $exprToWrap, Node\Expr $th
399399
: new Node\Expr\Array_();
400400

401401
return new Node\Expr\FuncCall(
402-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::checkReturn'),
402+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::checkReturn'),
403403
[
404404
new Node\Arg(new Node\Scalar\MagicConst\Method()),
405405
new Node\Arg($exprToWrap),
@@ -417,7 +417,7 @@ public static function buildVoidReturnGuard(Node\Expr\FuncCall $checkCall): arra
417417
$ifStmt = new Node\Stmt\If_(
418418
new Node\Expr\Instanceof_(
419419
new Node\Expr\Assign(new Node\Expr\Variable('__typephpRet'), $checkCall),
420-
new Node\Name('\TypePHP\Internal\ErrorMessage')
420+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
421421
),
422422
['stmts' => [self::buildTypeErrorThrowStmt(new Node\Expr\Variable('__typephpRet'))]]
423423
);
@@ -434,16 +434,16 @@ public static function buildTernaryReturnExpr(Node\Expr\FuncCall $checkCall): No
434434
return new Node\Expr\Ternary(
435435
new Node\Expr\Instanceof_(
436436
new Node\Expr\Assign(new Node\Expr\Variable('__typephpRet'), $checkCall),
437-
new Node\Name('\TypePHP\Internal\ErrorMessage')
437+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
438438
),
439439
new Node\Expr\Throw_(
440440
new Node\Expr\StaticCall(
441-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
441+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
442442
'prepareException',
443443
[
444444
new Node\Arg(
445445
new Node\Expr\New_(
446-
new Node\Name('\TypePHP\Exception\TypeError'),
446+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
447447
[
448448
new Node\Arg(
449449
new Node\Expr\MethodCall(new Node\Expr\Variable('__typephpRet'), 'getMessage')
@@ -461,7 +461,7 @@ public static function buildTernaryReturnExpr(Node\Expr\FuncCall $checkCall): No
461461
public static function buildWrappedYieldNode(Node\Expr\Yield_ $n, Node\Expr $thisArg): Node\Expr\Ternary
462462
{
463463
$checkYieldCall = new Node\Expr\FuncCall(
464-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::checkYield'),
464+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::checkYield'),
465465
[
466466
new Node\Arg(new Node\Scalar\MagicConst\Method()),
467467
new Node\Arg($n->key ?? new Node\Expr\ConstFetch(new Node\Name('null'))),
@@ -473,16 +473,16 @@ public static function buildWrappedYieldNode(Node\Expr\Yield_ $n, Node\Expr $thi
473473
$n->value = new Node\Expr\Ternary(
474474
new Node\Expr\Instanceof_(
475475
new Node\Expr\Assign(new Node\Expr\Variable('__typephpYld'), $checkYieldCall),
476-
new Node\Name('\TypePHP\Internal\ErrorMessage')
476+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
477477
),
478478
new Node\Expr\Throw_(
479479
new Node\Expr\StaticCall(
480-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
480+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
481481
'prepareException',
482482
[
483483
new Node\Arg(
484484
new Node\Expr\New_(
485-
new Node\Name('\TypePHP\Exception\TypeError'),
485+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
486486
[
487487
new Node\Arg(
488488
new Node\Expr\MethodCall(new Node\Expr\Variable('__typephpYld'), 'getMessage')
@@ -498,7 +498,7 @@ public static function buildWrappedYieldNode(Node\Expr\Yield_ $n, Node\Expr $thi
498498
);
499499

500500
$checkSendCall = new Node\Expr\FuncCall(
501-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::checkSend'),
501+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::checkSend'),
502502
[
503503
new Node\Arg(new Node\Scalar\MagicConst\Method()),
504504
new Node\Arg($n),
@@ -509,16 +509,16 @@ public static function buildWrappedYieldNode(Node\Expr\Yield_ $n, Node\Expr $thi
509509
return new Node\Expr\Ternary(
510510
new Node\Expr\Instanceof_(
511511
new Node\Expr\Assign(new Node\Expr\Variable('__typephpSnd'), $checkSendCall),
512-
new Node\Name('\TypePHP\Internal\ErrorMessage')
512+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
513513
),
514514
new Node\Expr\Throw_(
515515
new Node\Expr\StaticCall(
516-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
516+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
517517
'prepareException',
518518
[
519519
new Node\Arg(
520520
new Node\Expr\New_(
521-
new Node\Name('\TypePHP\Exception\TypeError'),
521+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
522522
[
523523
new Node\Arg(
524524
new Node\Expr\MethodCall(new Node\Expr\Variable('__typephpSnd'), 'getMessage')
@@ -570,7 +570,7 @@ public function enterNode(Node $n): int|Node|null
570570
$n->setAttribute('typephp_wrapped', true);
571571

572572
$n->expr = new Node\Expr\FuncCall(
573-
new Node\Name('\TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
573+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker::wrapIterable'),
574574
[
575575
new Node\Arg(new Node\Scalar\MagicConst\Method()),
576576
new Node\Arg(new Node\Scalar\String_('return')),
@@ -649,4 +649,4 @@ public function enterNode(Node $n): int|array|null
649649

650650
return $newStmts;
651651
}
652-
}
652+
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal\Visitor;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88

@@ -42,7 +42,7 @@ public static function createTernaryThrowExpr(Node\Expr\FuncCall $checkCall, int
4242
$args = [
4343
new Node\Arg(
4444
new Node\Expr\New_(
45-
new Node\Name('\TypePHP\Exception\TypeError'),
45+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
4646
[
4747
new Node\Arg(
4848
new Node\Expr\MethodCall(
@@ -65,11 +65,11 @@ public static function createTernaryThrowExpr(Node\Expr\FuncCall $checkCall, int
6565
new Node\Expr\Variable('__typephpVal'),
6666
$checkCall
6767
),
68-
new Node\Name('\TypePHP\Internal\ErrorMessage')
68+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
6969
),
7070
new Node\Expr\Throw_(
7171
new Node\Expr\StaticCall(
72-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
72+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
7373
'prepareException',
7474
$args
7575
)

src/Internal/Visitor/PropertyHookInjector.php renamed to src/Internal/Ast/PropertyHookInjector.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal\Visitor;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88
use PhpParser\NodeTraverser;
99
use PhpParser\NodeVisitorAbstract;
10-
use TypePHP\Internal\Config;
10+
use TypePHP\Internal\Util\Config;
1111

1212
/**
1313
* @internal Injects property contract checks into PHP 8.4 get and set property hooks.
@@ -90,16 +90,16 @@ private static function buildExpressionSetHookTernary(Node\Expr\FuncCall $checkC
9090
return new Node\Expr\Ternary(
9191
new Node\Expr\Instanceof_(
9292
new Node\Expr\Assign(new Node\Expr\Variable('__typephpVal'), $checkCall),
93-
new Node\Name('\TypePHP\Internal\ErrorMessage')
93+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorMessage')
9494
),
9595
new Node\Expr\Throw_(
9696
new Node\Expr\StaticCall(
97-
new Node\Name('\TypePHP\Internal\ErrorFactory'),
97+
new Node\Name\FullyQualified('TypePHP\Internal\Diagnostic\ErrorFactory'),
9898
'prepareException',
9999
[
100100
new Node\Arg(
101101
new Node\Expr\New_(
102-
new Node\Name('\TypePHP\Exception\TypeError'),
102+
new Node\Name\FullyQualified('TypePHP\Exception\TypeError'),
103103
[
104104
new Node\Arg(
105105
new Node\Expr\MethodCall(new Node\Expr\Variable('__typephpVal'), 'getMessage')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal;
5+
namespace TypePHP\Internal\Ast;
66

7-
use TypePHP\Resolver\TemplateManager;
7+
use TypePHP\Internal\Generics\TemplateManager;
88

99
/**
1010
* @internal ensures proper scope cleanup for variable tracking.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal\Visitor;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88
use PHPStan\PhpDocParser\Parser\TokenIterator;
9-
use TypePHP\Contract\DocblockExtractor;
10-
use TypePHP\Internal\DocblockNormalizer;
9+
use TypePHP\Internal\Docblock\DocblockExtractor;
10+
use TypePHP\Internal\Docblock\DocblockNormalizer;
1111

1212
/**
1313
* @internal Manages lexical scope stack frames and extracts local @var variable annotations.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace TypePHP\Internal;
5+
namespace TypePHP\Internal\Ast;
66

77
use PhpParser\Node;
88
use PhpParser\PrettyPrinter\Standard;

src/Internal/Checker/GeneratorChecker.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
99
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1010
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
11-
use TypePHP\Contract\ContractParser;
12-
use TypePHP\Resolver\SpecialTypeResolver;
13-
use TypePHP\Resolver\TemplateManager;
14-
use TypePHP\Resolver\TemplateSubstitutor;
15-
use TypePHP\Validator\TypeValidatorRegistry;
11+
use TypePHP\Internal\Docblock\DocblockParser;
12+
use TypePHP\Internal\Generics\TemplateManager;
13+
use TypePHP\Internal\Generics\TemplateSubstitutor;
14+
use TypePHP\Internal\Resolver\SpecialTypeResolver;
15+
use TypePHP\Internal\Validator\TypeValidatorRegistry;
1616

1717
/**
1818
* @internal Evaluates generator yield and send (TSend) type validations.
@@ -86,7 +86,7 @@ public static function checkYield(
8686
*/
8787
private static function resolveGeneratorReturnType(string $function, object|string|null $thisOrClass): ?TypeNode
8888
{
89-
$contract = ContractParser::parse($function);
89+
$contract = DocblockParser::parse($function);
9090
$returnTypeNode = $contract['return'] ?? null;
9191

9292
if ($returnTypeNode === null) {

0 commit comments

Comments
 (0)