|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\NodeVisitorAbstract; |
9 | 9 | use TypePHP\Internal\Docblock\DocblockExtractor; |
| 10 | +use TypePHP\Internal\Util\Config; |
10 | 11 |
|
11 | 12 | /** |
12 | 13 | * @internal AST Node Visitor that injects contract checks, scope tracking, property hook validation, and parameter/return wrappers into functions and methods. |
@@ -71,6 +72,20 @@ public function enterNode(Node $node): ?array |
71 | 72 | $this->scopeManager->pushScope(); |
72 | 73 | } |
73 | 74 |
|
| 75 | + if ($node instanceof Node\Expr\Assign || $node instanceof Node\Expr\AssignOp) { |
| 76 | + $this->markWriteContext($node->var); |
| 77 | + } elseif ($node instanceof Node\Expr\PreInc || $node instanceof Node\Expr\PostInc || $node instanceof Node\Expr\PreDec || $node instanceof Node\Expr\PostDec) { |
| 78 | + $this->markWriteContext($node->var); |
| 79 | + } elseif ($node instanceof Node\Stmt\Unset_) { |
| 80 | + foreach ($node->vars as $v) { |
| 81 | + $this->markWriteContext($v); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if ($node instanceof Node\Stmt\Class_) { |
| 86 | + $this->processClassPropertyDefaults($node); |
| 87 | + } |
| 88 | + |
74 | 89 | if ($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) { |
75 | 90 | $classContext = $this->classStack !== [] ? end($this->classStack) : null; |
76 | 91 | FunctionContractInjector::inject($node, $classContext); |
@@ -135,13 +150,130 @@ public function leaveNode(Node $node): ?Node |
135 | 150 | } |
136 | 151 | } |
137 | 152 |
|
| 153 | + if ($node instanceof Node\Expr\StaticPropertyFetch && $node->name instanceof Node\VarLikeIdentifier) { |
| 154 | + $propName = $node->name->toString(); |
| 155 | + |
| 156 | + if ($node->getAttribute('typephp_checked') !== true && $node->getAttribute('typephp_write_context') !== true) { |
| 157 | + $node->setAttribute('typephp_checked', true); |
| 158 | + $classArg = $node->class instanceof Node\Name |
| 159 | + ? new Node\Expr\ClassConstFetch($node->class, 'class') |
| 160 | + : $node->class; |
| 161 | + |
| 162 | + return new Node\Expr\StaticCall( |
| 163 | + new Node\Name\FullyQualified('TypePHP\Internal\RuntimeTypeChecker'), |
| 164 | + 'checkStaticProperty', |
| 165 | + [ |
| 166 | + new Node\Arg($classArg), |
| 167 | + new Node\Arg(new Node\Scalar\String_($propName)), |
| 168 | + new Node\Arg($node), |
| 169 | + new Node\Arg(new Node\Scalar\MagicConst\File()), |
| 170 | + new Node\Arg(new Node\Scalar\LNumber($node->getStartLine())), |
| 171 | + ] |
| 172 | + ); |
| 173 | + } |
| 174 | + } |
| 175 | + |
138 | 176 | if ($this->isScopeBoundary($node)) { |
139 | 177 | $this->scopeManager->popScope(); |
140 | 178 | } |
141 | 179 |
|
142 | 180 | return null; |
143 | 181 | } |
144 | 182 |
|
| 183 | + private function markWriteContext(Node $node): void |
| 184 | + { |
| 185 | + $node->setAttribute('typephp_write_context', true); |
| 186 | + if ($node instanceof Node\Expr\ArrayDimFetch || $node instanceof Node\Expr\PropertyFetch) { |
| 187 | + $this->markWriteContext($node->var); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + private function processClassPropertyDefaults(Node\Stmt\Class_ $node): void |
| 192 | + { |
| 193 | + if (! Config::isInlinePropertiesEnabled()) { |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + $defaultProps = []; |
| 198 | + $hasConstructor = false; |
| 199 | + |
| 200 | + foreach ($node->stmts as $stmt) { |
| 201 | + if ($stmt instanceof Node\Stmt\ClassMethod && strtolower($stmt->name->toString()) === '__construct') { |
| 202 | + $hasConstructor = true; |
| 203 | + } elseif ($stmt instanceof Node\Stmt\Property && ! $stmt->isStatic()) { |
| 204 | + $doc = $stmt->getDocComment(); |
| 205 | + if ($doc !== null && str_contains($doc->getText(), '@var') && ! str_contains($doc->getText(), '@typephp-ignore')) { |
| 206 | + foreach ($stmt->props as $p) { |
| 207 | + $isExplicitNull = $p->default instanceof Node\Expr\ConstFetch && strtolower($p->default->name->toString()) === 'null'; |
| 208 | + if ($p->default !== null && ! $isExplicitNull) { |
| 209 | + $defaultProps[] = [ |
| 210 | + 'name' => $p->name->toString(), |
| 211 | + 'line' => $p->getStartLine(), |
| 212 | + ]; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + if ($defaultProps === []) { |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + if (! $hasConstructor) { |
| 224 | + $ctorStmts = []; |
| 225 | + if ($node->extends !== null) { |
| 226 | + $ctorStmts[] = new Node\Stmt\If_( |
| 227 | + new Node\Expr\FuncCall(new Node\Name('method_exists'), [ |
| 228 | + new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name('parent'), 'class')), |
| 229 | + new Node\Arg(new Node\Scalar\String_('__construct')), |
| 230 | + ]), |
| 231 | + ['stmts' => [ |
| 232 | + new Node\Stmt\Expression(new Node\Expr\StaticCall(new Node\Name('parent'), '__construct', [new Node\Arg(new Node\Expr\Variable('_typephp_ctor_args'), false, true)])), |
| 233 | + ]] |
| 234 | + ); |
| 235 | + } |
| 236 | + |
| 237 | + foreach ($defaultProps as $dp) { |
| 238 | + $checkCall = NodeBuilder::createPropertyCheckCall( |
| 239 | + new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $dp['name']), |
| 240 | + new Node\Expr\Variable('this'), |
| 241 | + $dp['name'] |
| 242 | + ); |
| 243 | + $stmt = new Node\Stmt\Expression(NodeBuilder::createTernaryThrowExpr($checkCall, $dp['line'])); |
| 244 | + $stmt->setAttribute('typephp_injected', true); |
| 245 | + $ctorStmts[] = $stmt; |
| 246 | + } |
| 247 | + |
| 248 | + $ctor = new Node\Stmt\ClassMethod('__construct', [ |
| 249 | + 'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC, |
| 250 | + 'params' => [new Node\Param(new Node\Expr\Variable('_typephp_ctor_args'), null, null, false, true)], |
| 251 | + 'stmts' => $ctorStmts, |
| 252 | + ]); |
| 253 | + $ctor->setAttribute('typephp_injected', true); |
| 254 | + $node->stmts[] = $ctor; |
| 255 | + } else { |
| 256 | + foreach ($node->stmts as $stmt) { |
| 257 | + if ($stmt instanceof Node\Stmt\ClassMethod && strtolower($stmt->name->toString()) === '__construct') { |
| 258 | + $injected = []; |
| 259 | + foreach ($defaultProps as $dp) { |
| 260 | + $checkCall = NodeBuilder::createPropertyCheckCall( |
| 261 | + new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $dp['name']), |
| 262 | + new Node\Expr\Variable('this'), |
| 263 | + $dp['name'] |
| 264 | + ); |
| 265 | + $checkStmt = new Node\Stmt\Expression(NodeBuilder::createTernaryThrowExpr($checkCall, $dp['line'])); |
| 266 | + $checkStmt->setAttribute('typephp_injected', true); |
| 267 | + $injected[] = $checkStmt; |
| 268 | + } |
| 269 | + $stmt->stmts = [...$injected, ...($stmt->stmts ?? [])]; |
| 270 | + |
| 271 | + break; |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + |
145 | 277 | private function trackDeclarationEntry(Node $node): void |
146 | 278 | { |
147 | 279 | if ($node instanceof Node\Stmt\Namespace_) { |
|
0 commit comments