Skip to content

Commit bd442e4

Browse files
authored
Improve and optimize AST injection generation (#85)
* Enhance inheritance checks in ContractVisitor and FunctionContractInjector * Add issue templates for bug reports, feature requests, and type mismatches
1 parent 92ef74b commit bd442e4

6 files changed

Lines changed: 133 additions & 20 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Bug report
2+
description: If something isn't working as expected.
3+
title: ""
4+
labels: ["bug"]
5+
body:
6+
- type: textarea
7+
id: description
8+
attributes:
9+
label: Bug report
10+
description: Please describe your problem here
11+
placeholder: Leave a comment
12+
validations:
13+
required: true
14+
15+
- type: input
16+
id: reproduction
17+
attributes:
18+
label: Code snippet that reproduces the problem
19+
description: |
20+
Please reproduce the issue with the [on-line TypePHP playground](https://typephp-php.github.io/docs/playground), get a unique URL with the Share button and post it here.
21+
placeholder: https://typephp-php.github.io/docs/playground#code=<unique_id>
22+
validations:
23+
required: false
24+
25+
- type: textarea
26+
id: expected
27+
attributes:
28+
label: Expected output
29+
description: Was the issue reported incorrectly? Or should TypePHP detect an issue with the code but doesn't?
30+
placeholder: Leave a comment
31+
validations:
32+
required: true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Feature request
2+
description: I have a suggestion (and may want to implement it)!
3+
title: "[Feature]: "
4+
labels: ["enhancement"]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
Have an idea to improve TypePHP? We would love to hear it!
10+
- type: textarea
11+
id: proposal
12+
attributes:
13+
label: Proposed Feature / Syntax
14+
description: Describe the feature or type annotation you would like TypePHP to support.
15+
placeholder: E.g., Support for a new scalar refinement, shape feature, or CLI option...
16+
validations:
17+
required: true
18+
- type: textarea
19+
id: motivation
20+
attributes:
21+
label: Motivation / Use Case
22+
description: Why should this be added to TypePHP? What problem does it solve?
23+
validations:
24+
required: true
25+
- type: textarea
26+
id: example
27+
attributes:
28+
label: Code Example
29+
description: Show a code snippet demonstrating how this feature would look and behave.
30+
render: php
31+
validations:
32+
required: false
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Function signature or stub mismatch
2+
description: Some built-in PHP function or vendor stub expects or returns a different type than reported.
3+
title: "[Type Mismatch]: "
4+
labels: ["types", "stubs"]
5+
body:
6+
- type: markdown
7+
attributes:
8+
value: |
9+
Use this template if TypePHP reports an invalid type on a built-in PHP function, or if a DocBlock contract contradicts real PHP behavior.
10+
- type: input
11+
id: target
12+
attributes:
13+
label: Affected Function, Method, or Class
14+
placeholder: E.g., array_map(), DateTime::format(), or Vendor\Package\Service
15+
validations:
16+
required: true
17+
- type: textarea
18+
id: current-vs-expected
19+
attributes:
20+
label: Current vs Expected Type Contract
21+
description: What does TypePHP currently enforce versus what does PHP actually allow/return?
22+
validations:
23+
required: true
24+
- type: input
25+
id: playground
26+
attributes:
27+
label: Playground Link (Optional)
28+
description: If possible, provide a playground reproduction.
29+
placeholder: https://typephp-php.github.io/docs/playground#code=...
30+
validations:
31+
required: false

‎.github/ISSUE_TEMPLATE/config.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
blank_issues_enabled: false
2+
contact_links:
3+
- name: Report a security vulnerability
4+
url: https://github.com/typephp-php/typephp/security/policy
5+
about: Please review our security policy for more details.
6+
- name: Support question
7+
url: https://github.com/typephp-php/typephp/discussions/new?category=q-a
8+
about: Please open a new discussion instead. Thank you.

‎src/Internal/Ast/ContractVisitor.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
322322
{
323323
$typeName = $this->resolveQualifiedName($node->name);
324324
$hasInheritance = true;
325+
$hasRealInheritance = false;
325326
$hasPropertyWithDoc = false;
326327
$isReadonly = false;
327328
$hasTemplates = false;
@@ -350,11 +351,15 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
350351
$hasTemplates
351352
|| str_contains($doc->getText(), '@phpstan-')
352353
|| str_contains($doc->getText(), '@psalm-')
354+
|| str_contains($doc->getText(), '@method')
355+
|| str_contains($doc->getText(), '@property')
353356
);
354357

355-
$hasInheritance = $hasExtends || $hasImplements || $hasTraits || $hasClassDoc;
358+
$hasRealInheritance = $hasExtends || $hasImplements || $hasTraits;
359+
$hasInheritance = $hasRealInheritance || $hasClassDoc;
356360
} elseif ($node instanceof Node\Stmt\Enum_) {
357-
$hasInheritance = $node->implements !== [];
361+
$hasRealInheritance = $node->implements !== [];
362+
$hasInheritance = $hasRealInheritance;
358363
} else {
359364
$doc = $node->getDocComment();
360365
$hasTemplates = $doc !== null && (
@@ -368,6 +373,7 @@ private function enterClassLike(Node\Stmt\Class_|Node\Stmt\Interface_|Node\Stmt\
368373
'name' => $typeName,
369374
'isAnonymous' => ($node instanceof Node\Stmt\Class_ && $node->name === null),
370375
'hasInheritance' => $hasInheritance,
376+
'hasRealInheritance' => $hasRealInheritance,
371377
'hasPropertyWithDoc' => $hasPropertyWithDoc,
372378
'isReadonly' => $isReadonly,
373379
'hasTemplates' => $hasTemplates,

‎src/Internal/Ast/FunctionContractInjector.php‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class FunctionContractInjector
2828
];
2929

3030
/**
31-
* @param array{hasInheritance?: bool, hasPropertyWithDoc?: bool, isReadonly?: bool, hasTemplates?: bool}|null $classContext
31+
* @param array{hasInheritance?: bool, hasRealInheritance?: bool, hasPropertyWithDoc?: bool, isReadonly?: bool, hasTemplates?: bool}|null $classContext
3232
*/
3333
public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?array $classContext = null): void
3434
{
@@ -41,6 +41,7 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
4141
$docText = $doc !== null ? $doc->getText() : '';
4242

4343
$hasInheritance = $classContext['hasInheritance'] ?? true;
44+
$hasRealInheritance = $classContext['hasRealInheritance'] ?? $hasInheritance;
4445
$hasPropertyWithDoc = $classContext['hasPropertyWithDoc'] ?? true;
4546
$isReadonlyClass = $classContext['isReadonly'] ?? false;
4647
$hasClassTemplates = $classContext['hasTemplates'] ?? false;
@@ -80,7 +81,7 @@ public static function inject(Node\Stmt\Function_|Node\Stmt\ClassMethod $node, ?
8081
$hasParamOut = $byRefParams !== [] && ($hasParamOutDoc || $hasInheritance);
8182

8283
$hasSelfOutDoc = str_contains($docText, 'self-out') || str_contains($docText, 'this-out');
83-
$hasSelfOut = $isClassMethod && ! $node->isStatic() && ($hasSelfOutDoc || ($hasClassTemplates && $hasInheritance));
84+
$hasSelfOut = $isClassMethod && ! $node->isStatic() && ($hasSelfOutDoc || ($hasClassTemplates && $hasRealInheritance));
8485

8586
$hasReturnDoc = str_contains($docText, '@return')
8687
|| str_contains($docText, '@phpstan-return')
@@ -275,7 +276,7 @@ private static function resolveThisArg(bool $isClassMethod, Node\Stmt\Function_|
275276

276277
private static function isGenerator(Node\Stmt\Function_|Node\Stmt\ClassMethod $node): bool
277278
{
278-
$visitor = new class () extends NodeVisitorAbstract {
279+
$visitor = new class() extends NodeVisitorAbstract {
279280
public bool $isGen = false;
280281

281282
public function enterNode(Node $n): ?int
@@ -357,24 +358,30 @@ private static function buildSetupScopeStmt(array $params, Node\Expr $thisArg):
357358

358359
$cacheKeyExpr = new Node\Scalar\MagicConst\Method();
359360

361+
$noParamFetch = new Node\Expr\StaticPropertyFetch(
362+
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ParamChecker'),
363+
'noParamContractCache'
364+
);
365+
$noParamFetch->setAttribute('typephp_checked', true);
366+
360367
$noParamCacheCheck = new Node\Expr\BooleanNot(
361368
new Node\Expr\Isset_([
362369
new Node\Expr\ArrayDimFetch(
363-
new Node\Expr\StaticPropertyFetch(
364-
new Node\Name\FullyQualified('TypePHP\Internal\Checker\ParamChecker'),
365-
'noParamContractCache'
366-
),
370+
$noParamFetch,
367371
$cacheKeyExpr
368372
),
369373
])
370374
);
371375

376+
$hasTemplatesFetch = new Node\Expr\StaticPropertyFetch(
377+
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker'),
378+
'hasMethodTemplatesCache'
379+
);
380+
$hasTemplatesFetch->setAttribute('typephp_checked', true);
381+
372382
$hasTemplatesCheck = new Node\Expr\BinaryOp\Coalesce(
373383
new Node\Expr\ArrayDimFetch(
374-
new Node\Expr\StaticPropertyFetch(
375-
new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker'),
376-
'hasMethodTemplatesCache'
377-
),
384+
$hasTemplatesFetch,
378385
$cacheKeyExpr
379386
),
380387
new Node\Expr\ConstFetch(new Node\Name('false'))
@@ -671,10 +678,8 @@ public static function buildWrappedYieldNode(Node\Expr\Yield_ $n, Node\Expr $thi
671678
private static function wrapGeneratorReturns(array $stmts, Node\Expr $thisArg): array
672679
{
673680
$traverser = new NodeTraverser();
674-
$traverser->addVisitor(new class ($thisArg) extends NodeVisitorAbstract {
675-
public function __construct(private Node\Expr $thisArg)
676-
{
677-
}
681+
$traverser->addVisitor(new class($thisArg) extends NodeVisitorAbstract {
682+
public function __construct(private Node\Expr $thisArg) {}
678683

679684
public function enterNode(Node $n): int|Node|null
680685
{
@@ -733,7 +738,7 @@ private static function wrapNonGeneratorReturns(
733738
bool $hasSelfOut = false
734739
): array {
735740
$traverser = new NodeTraverser();
736-
$traverser->addVisitor(new class ($thisArg, $isNativeVoid, $needsReturnVars, $hasReturn, $byRefParams, $hasSelfOut) extends NodeVisitorAbstract {
741+
$traverser->addVisitor(new class($thisArg, $isNativeVoid, $needsReturnVars, $hasReturn, $byRefParams, $hasSelfOut) extends NodeVisitorAbstract {
737742
/**
738743
* @param array<string> $byRefParams
739744
*/
@@ -744,8 +749,7 @@ public function __construct(
744749
private bool $hasReturn,
745750
private array $byRefParams,
746751
private bool $hasSelfOut
747-
) {
748-
}
752+
) {}
749753

750754
public function enterNode(Node $n): int|array|null
751755
{

0 commit comments

Comments
 (0)