Skip to content

Commit f1ef570

Browse files
gen_stub: move handleStatements() into FileInfo
Reduce the number of global functions by moving it to instance method `FileInfo::handleStatements()`.
1 parent 202b236 commit f1ef570

File tree

1 file changed

+145
-145
lines changed

1 file changed

+145
-145
lines changed

build/gen_stub.php

Lines changed: 145 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,6 +4271,150 @@ public function getMinimumPhpVersionIdCompatibility(): ?int {
42714271
public function shouldGenerateLegacyArginfo(): bool {
42724272
return $this->minimumPhpVersionIdCompatibility !== null && $this->minimumPhpVersionIdCompatibility < PHP_80_VERSION_ID;
42734273
}
4274+
4275+
public function handleStatements(array $stmts, PrettyPrinterAbstract $prettyPrinter): void {
4276+
$conds = [];
4277+
foreach ($stmts as $stmt) {
4278+
$cond = handlePreprocessorConditions($conds, $stmt);
4279+
4280+
if ($stmt instanceof Stmt\Nop) {
4281+
continue;
4282+
}
4283+
4284+
if ($stmt instanceof Stmt\Namespace_) {
4285+
$this->handleStatements($stmt->stmts, $prettyPrinter);
4286+
continue;
4287+
}
4288+
4289+
if ($stmt instanceof Stmt\Const_) {
4290+
foreach ($stmt->consts as $const) {
4291+
$this->constInfos[] = parseConstLike(
4292+
$prettyPrinter,
4293+
new ConstName($const->namespacedName, $const->name->toString()),
4294+
$const,
4295+
0,
4296+
null,
4297+
$stmt->getComments(),
4298+
$cond,
4299+
$this->isUndocumentable,
4300+
$this->getMinimumPhpVersionIdCompatibility(),
4301+
[]
4302+
);
4303+
}
4304+
continue;
4305+
}
4306+
4307+
if ($stmt instanceof Stmt\Function_) {
4308+
$this->funcInfos[] = parseFunctionLike(
4309+
$prettyPrinter,
4310+
new FunctionName($stmt->namespacedName),
4311+
0,
4312+
0,
4313+
$stmt,
4314+
$cond,
4315+
$this->isUndocumentable,
4316+
$this->getMinimumPhpVersionIdCompatibility()
4317+
);
4318+
continue;
4319+
}
4320+
4321+
if ($stmt instanceof Stmt\ClassLike) {
4322+
$className = $stmt->namespacedName;
4323+
$constInfos = [];
4324+
$propertyInfos = [];
4325+
$methodInfos = [];
4326+
$enumCaseInfos = [];
4327+
foreach ($stmt->stmts as $classStmt) {
4328+
$cond = handlePreprocessorConditions($conds, $classStmt);
4329+
if ($classStmt instanceof Stmt\Nop) {
4330+
continue;
4331+
}
4332+
4333+
$classFlags = $stmt instanceof Class_ ? $stmt->flags : 0;
4334+
$abstractFlag = $stmt instanceof Stmt\Interface_ ? Modifiers::ABSTRACT : 0;
4335+
4336+
if ($classStmt instanceof Stmt\ClassConst) {
4337+
foreach ($classStmt->consts as $const) {
4338+
$constInfos[] = parseConstLike(
4339+
$prettyPrinter,
4340+
new ClassConstName($className, $const->name->toString()),
4341+
$const,
4342+
$classStmt->flags,
4343+
$classStmt->type,
4344+
$classStmt->getComments(),
4345+
$cond,
4346+
$this->isUndocumentable,
4347+
$this->getMinimumPhpVersionIdCompatibility(),
4348+
AttributeInfo::createFromGroups($classStmt->attrGroups)
4349+
);
4350+
}
4351+
} else if ($classStmt instanceof Stmt\Property) {
4352+
if (!($classStmt->flags & Class_::VISIBILITY_MODIFIER_MASK)) {
4353+
throw new Exception("Visibility modifier is required");
4354+
}
4355+
foreach ($classStmt->props as $property) {
4356+
$propertyInfos[] = parseProperty(
4357+
$className,
4358+
$classFlags,
4359+
$classStmt->flags,
4360+
$property,
4361+
$classStmt->type,
4362+
$classStmt->getComments(),
4363+
$prettyPrinter,
4364+
$this->getMinimumPhpVersionIdCompatibility(),
4365+
AttributeInfo::createFromGroups($classStmt->attrGroups)
4366+
);
4367+
}
4368+
} else if ($classStmt instanceof Stmt\ClassMethod) {
4369+
if (!($classStmt->flags & Class_::VISIBILITY_MODIFIER_MASK)) {
4370+
throw new Exception("Visibility modifier is required");
4371+
}
4372+
$methodInfos[] = parseFunctionLike(
4373+
$prettyPrinter,
4374+
new MethodName($className, $classStmt->name->toString()),
4375+
$classFlags,
4376+
$classStmt->flags | $abstractFlag,
4377+
$classStmt,
4378+
$cond,
4379+
$this->isUndocumentable,
4380+
$this->getMinimumPhpVersionIdCompatibility()
4381+
);
4382+
} else if ($classStmt instanceof Stmt\EnumCase) {
4383+
$enumCaseInfos[] = new EnumCaseInfo(
4384+
$classStmt->name->toString(), $classStmt->expr);
4385+
} else {
4386+
throw new Exception("Not implemented {$classStmt->getType()}");
4387+
}
4388+
}
4389+
4390+
$this->classInfos[] = parseClass(
4391+
$className,
4392+
$stmt,
4393+
$constInfos,
4394+
$propertyInfos,
4395+
$methodInfos,
4396+
$enumCaseInfos,
4397+
$cond,
4398+
$this->getMinimumPhpVersionIdCompatibility(),
4399+
$this->isUndocumentable
4400+
);
4401+
continue;
4402+
}
4403+
4404+
if ($stmt instanceof Stmt\Expression) {
4405+
$expr = $stmt->expr;
4406+
if ($expr instanceof Expr\Include_) {
4407+
$this->dependencies[] = (string)EvaluatedValue::createFromExpression($expr->expr, null, null, [])->value;
4408+
continue;
4409+
}
4410+
}
4411+
4412+
throw new Exception("Unexpected node {$stmt->getType()}");
4413+
}
4414+
if (!empty($conds)) {
4415+
throw new Exception("Unterminated preprocessor conditions");
4416+
}
4417+
}
42744418
}
42754419

42764420
class DocCommentTag {
@@ -4883,150 +5027,6 @@ function getFileDocComments(array $stmts): array {
48835027
);
48845028
}
48855029

4886-
function handleStatements(FileInfo $fileInfo, array $stmts, PrettyPrinterAbstract $prettyPrinter) {
4887-
$conds = [];
4888-
foreach ($stmts as $stmt) {
4889-
$cond = handlePreprocessorConditions($conds, $stmt);
4890-
4891-
if ($stmt instanceof Stmt\Nop) {
4892-
continue;
4893-
}
4894-
4895-
if ($stmt instanceof Stmt\Namespace_) {
4896-
handleStatements($fileInfo, $stmt->stmts, $prettyPrinter);
4897-
continue;
4898-
}
4899-
4900-
if ($stmt instanceof Stmt\Const_) {
4901-
foreach ($stmt->consts as $const) {
4902-
$fileInfo->constInfos[] = parseConstLike(
4903-
$prettyPrinter,
4904-
new ConstName($const->namespacedName, $const->name->toString()),
4905-
$const,
4906-
0,
4907-
null,
4908-
$stmt->getComments(),
4909-
$cond,
4910-
$fileInfo->isUndocumentable,
4911-
$fileInfo->getMinimumPhpVersionIdCompatibility(),
4912-
[]
4913-
);
4914-
}
4915-
continue;
4916-
}
4917-
4918-
if ($stmt instanceof Stmt\Function_) {
4919-
$fileInfo->funcInfos[] = parseFunctionLike(
4920-
$prettyPrinter,
4921-
new FunctionName($stmt->namespacedName),
4922-
0,
4923-
0,
4924-
$stmt,
4925-
$cond,
4926-
$fileInfo->isUndocumentable,
4927-
$fileInfo->getMinimumPhpVersionIdCompatibility()
4928-
);
4929-
continue;
4930-
}
4931-
4932-
if ($stmt instanceof Stmt\ClassLike) {
4933-
$className = $stmt->namespacedName;
4934-
$constInfos = [];
4935-
$propertyInfos = [];
4936-
$methodInfos = [];
4937-
$enumCaseInfos = [];
4938-
foreach ($stmt->stmts as $classStmt) {
4939-
$cond = handlePreprocessorConditions($conds, $classStmt);
4940-
if ($classStmt instanceof Stmt\Nop) {
4941-
continue;
4942-
}
4943-
4944-
$classFlags = $stmt instanceof Class_ ? $stmt->flags : 0;
4945-
$abstractFlag = $stmt instanceof Stmt\Interface_ ? Modifiers::ABSTRACT : 0;
4946-
4947-
if ($classStmt instanceof Stmt\ClassConst) {
4948-
foreach ($classStmt->consts as $const) {
4949-
$constInfos[] = parseConstLike(
4950-
$prettyPrinter,
4951-
new ClassConstName($className, $const->name->toString()),
4952-
$const,
4953-
$classStmt->flags,
4954-
$classStmt->type,
4955-
$classStmt->getComments(),
4956-
$cond,
4957-
$fileInfo->isUndocumentable,
4958-
$fileInfo->getMinimumPhpVersionIdCompatibility(),
4959-
AttributeInfo::createFromGroups($classStmt->attrGroups)
4960-
);
4961-
}
4962-
} else if ($classStmt instanceof Stmt\Property) {
4963-
if (!($classStmt->flags & Class_::VISIBILITY_MODIFIER_MASK)) {
4964-
throw new Exception("Visibility modifier is required");
4965-
}
4966-
foreach ($classStmt->props as $property) {
4967-
$propertyInfos[] = parseProperty(
4968-
$className,
4969-
$classFlags,
4970-
$classStmt->flags,
4971-
$property,
4972-
$classStmt->type,
4973-
$classStmt->getComments(),
4974-
$prettyPrinter,
4975-
$fileInfo->getMinimumPhpVersionIdCompatibility(),
4976-
AttributeInfo::createFromGroups($classStmt->attrGroups)
4977-
);
4978-
}
4979-
} else if ($classStmt instanceof Stmt\ClassMethod) {
4980-
if (!($classStmt->flags & Class_::VISIBILITY_MODIFIER_MASK)) {
4981-
throw new Exception("Visibility modifier is required");
4982-
}
4983-
$methodInfos[] = parseFunctionLike(
4984-
$prettyPrinter,
4985-
new MethodName($className, $classStmt->name->toString()),
4986-
$classFlags,
4987-
$classStmt->flags | $abstractFlag,
4988-
$classStmt,
4989-
$cond,
4990-
$fileInfo->isUndocumentable,
4991-
$fileInfo->getMinimumPhpVersionIdCompatibility()
4992-
);
4993-
} else if ($classStmt instanceof Stmt\EnumCase) {
4994-
$enumCaseInfos[] = new EnumCaseInfo(
4995-
$classStmt->name->toString(), $classStmt->expr);
4996-
} else {
4997-
throw new Exception("Not implemented {$classStmt->getType()}");
4998-
}
4999-
}
5000-
5001-
$fileInfo->classInfos[] = parseClass(
5002-
$className,
5003-
$stmt,
5004-
$constInfos,
5005-
$propertyInfos,
5006-
$methodInfos,
5007-
$enumCaseInfos,
5008-
$cond,
5009-
$fileInfo->getMinimumPhpVersionIdCompatibility(),
5010-
$fileInfo->isUndocumentable
5011-
);
5012-
continue;
5013-
}
5014-
5015-
if ($stmt instanceof Stmt\Expression) {
5016-
$expr = $stmt->expr;
5017-
if ($expr instanceof Expr\Include_) {
5018-
$fileInfo->dependencies[] = (string)EvaluatedValue::createFromExpression($expr->expr, null, null, [])->value;
5019-
continue;
5020-
}
5021-
}
5022-
5023-
throw new Exception("Unexpected node {$stmt->getType()}");
5024-
}
5025-
if (!empty($conds)) {
5026-
throw new Exception("Unterminated preprocessor conditions");
5027-
}
5028-
}
5029-
50305030
function parseStubFile(string $code): FileInfo {
50315031
$parser = new PhpParser\Parser\Php7(new PhpParser\Lexer\Emulative());
50325032
$nodeTraverser = new PhpParser\NodeTraverser;
@@ -5043,7 +5043,7 @@ protected function pName_FullyQualified(Name\FullyQualified $node): string {
50435043
$fileTags = parseDocComments(getFileDocComments($stmts));
50445044
$fileInfo = new FileInfo($fileTags);
50455045

5046-
handleStatements($fileInfo, $stmts, $prettyPrinter);
5046+
$fileInfo->handleStatements($stmts, $prettyPrinter);
50475047
return $fileInfo;
50485048
}
50495049

0 commit comments

Comments
 (0)