nette/php-generator
Generate PHP code via a fluent API: build classes, interfaces, functions, methods, properties, namespaces, and PHPDoc, then render to valid PHP. Handy for code generators, scaffolding tools, and runtime code output with strict formatting and escaping.
This release focuses on making namespaces and classes work together more naturally. Classes now know which namespace they belong to, constructors accept fully qualified names, and a new unified add() method on PhpFile ties it all together. Under the hood, the Factory got a thorough refactoring and the codebase passed its first mandatory static analysis check.
PhpNamespace::add() now automatically assigns itself as the namespace of any ClassLike passed to it. If your code relied on getNamespace() returning null after adding a class to a namespace, you'll need to update that assumption.
PhpFile::add() – a single entry point for adding classes, interfaces, traits, enums, functions, and namespaces to a file. It handles namespace creation and bracket syntax automatically, so you can build a complete file with just add() calls.
ClassLike::getFullName() – returns the fully qualified name when the class belongs to a namespace (e.g., App\Model\User), the short name otherwise, or null for anonymous classes.
ClassLike constructors now accept FQN – pass 'App\Model\User' and the namespace is extracted automatically. No need to create the namespace separately for simple cases.
PhpNamespace::add() accepts GlobalFunction – the unified add() method now handles functions alongside class-like types, making the namespace API more consistent.
The $namespace parameter in ClassLike constructors is deprecated. Use setNamespace() instead. The old signature still works for now, but will be removed in a future version.
This release focuses on making namespaces and classes work together more naturally. Classes now know which namespace they belong to, constructors accept fully qualified names, and a new unified add() method on PhpFile ties it all together. Under the hood, the Factory got a thorough refactoring and the codebase passed its first mandatory static analysis check.
PhpNamespace::add() now automatically assigns itself as the namespace of any ClassLike passed to it. If your code relied on getNamespace() returning null after adding a class to a namespace, you'll need to update that assumption.
PhpFile::add() – a single entry point for adding classes, interfaces, traits, enums, functions, and namespaces to a file. It handles namespace creation and bracket syntax automatically, so you can build a complete file with just add() calls.
ClassLike::getFullName() – returns the fully qualified name when the class belongs to a namespace (e.g., App\Model\User), the short name otherwise, or null for anonymous classes.
ClassLike constructors now accept FQN – pass 'App\Model\User' and the namespace is extracted automatically. No need to create the namespace separately for simple cases.
PhpNamespace::add() accepts GlobalFunction – the unified add() method now handles functions alongside class-like types, making the namespace API more consistent.
The $namespace parameter in ClassLike constructors is deprecated. Use setNamespace() instead. The old signature still works for now, but will be removed in a future version.
Factory::fromClassReflection() extracts property hook bodies (#172)Factory::fromClassReflection() rejects $withBodies for interfacesClassManipulator::implement() can implement abstract classesClassManipulator::implementInterface() renamed to implement()Extractor::extractAll() fixed file comment parsingisNullable() returns true when default value is null (possible BC break)PhpNamespace::getClass() & getFunction()CommentAware::removeComment()PhpFile::removeNamespace()from() accepts first-class callablesClassLike::from return type assert the subclass type (BC break) (#154)Dumper::dumpArray(), dumpArguments() optimizationaddMember(), addMethod(), addProperty(), addConstant(), addCase() #152dumpCustomObject()printFile() [Close #147]inheritMethod() & inheritProperty()ConstantsAware::getConstant()Extractor::extractAll() extracts only top-level classes and functions, fixed file comment parsinggetType() is deprecatedLiteral::new() #130getParameter() & hasParameter()hasConstant() (#133)Helpers::formatDocComment() added option $forceMultiLine__serialize()Helpers::formatDocComment(), Printer::printFunction() & etc normalizes whitespaceremoveUse() #102ClassType::__clone() clones traitsremoveUse() #102ClassType::__clone() clones traitsThis release takes advantage of all the new features of PHP 8 and cleans up some issues in the API.
Stricter behaviour:
Changes:
$thisDeprecations:
ClassType::withBodiesFrom() replaced with from(..., withBodies: true)GlobalFunction::withBodyFrom() replaced with from(..., withBody: true)removeClass() & removeFunction()This is the last 3.6.x release.
resolveName()PhpNamespace::addUse() ignores names in current namespace #96BLOG POST: https://blog.nette.org/en/phpgenerator-3-6-news-from-php-8-1-and-more
addUse() & simplifyName() is case-insensitivePhpNamespace::simplifyName() fixed collision when alias already existsPhpNamespace::addUse() removed parameter $aliasOut (BC break)PhpNamespace::addUse() simplified aliases generationgetType(), getReturnType(): added option $asObject that returns Nette\Utils\TypePhpNamespace::simplifyType() supports generics like Foo<A, B>PhpNamespace::unresolveName() renamed to simplifyName()Helpers::unindent() fixed when level = 0Dumper::format() placeholder ? must not be followed by \wPhpNamespace::addUse() added validationClassType::fromCode() & PhpFile::fromCode() #79getClasses()How can I help you explore Laravel packages today?