PHPackages                             ruudk/code-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ruudk/code-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ruudk/code-generator
====================

Yield your code line by line: let generators handle indentation, formatting, and structure for you

0.4.6(2mo ago)5130.1k—4.7%2MITPHPPHP ^8.4CI passing

Since Aug 8Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/ruudk/code-generator)[ Packagist](https://packagist.org/packages/ruudk/code-generator)[ GitHub Sponsors](https://github.com/ruudk)[ RSS](/packages/ruudk-code-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (21)Versions (18)Used By (2)

 **Code Generator for PHP**
 *Yield your code line by line: let generators handle indentation, formatting, and structure for you*

 [![Latest Stable Version](https://camo.githubusercontent.com/acd9bf388b9997f2acb11892c3fa1d8596d320504174f73782f987566d7e8591/68747470733a2f2f706f7365722e707567782e6f72672f727575646b2f636f64652d67656e657261746f722f763f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/ruudk/code-generator) [![PHP Version Require](https://camo.githubusercontent.com/e51d6f89189d345475a986da64fabb689d864415f693316bb4526d46c0c95ecc/68747470733a2f2f706f7365722e707567782e6f72672f727575646b2f636f64652d67656e657261746f722f726571756972652f7068703f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/ruudk/code-generator) [![Total Downloads](https://camo.githubusercontent.com/9110334069cad945d4ccc2d0208a44317a4170e20cbe2f9796c0562312056b8e/68747470733a2f2f706f7365722e707567782e6f72672f727575646b2f636f64652d67656e657261746f722f646f776e6c6f6164733f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/ruudk/code-generator) [![License](https://camo.githubusercontent.com/36a7b237e1b910a30492745b7150fcb6f98bdc53e3321ea1e9b1c5faf3b55ff1/68747470733a2f2f706f7365722e707567782e6f72672f727575646b2f636f64652d67656e657261746f722f6c6963656e73653f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/ruudk/code-generator)

---

Code Generator
==============

[](#code-generator)

Transform the way you generate PHP code! This library makes code generation feel natural and elegant - just yield your lines and watch as perfectly formatted, beautifully structured PHP emerges with automatic namespace imports, proper indentation, and clean syntax.

✨ Why This Library?
-------------------

[](#-why-this-library)

Ever struggled with generating PHP code? String concatenation getting messy? Indentation driving you crazy? **This is your solution!**

🎯 **Write code that writes code** - but make it beautiful
🎯 **Yield line by line** - no string concatenation nightmares
🎯 **Automatic everything** - imports, indentation, formatting handled for you
🎯 **Composable generators** - chain, wrap, and transform with ease
🎯 **Zero boilerplate** - focus on what to generate, not how

🚀 Key Features
--------------

[](#-key-features)

### 🎨 Beautiful Output, Every Time

[](#-beautiful-output-every-time)

- **Smart namespace management** - imports are automatically collected and organized
- **Perfect indentation** - just yield your lines, nesting is handled automatically
- **Clean formatting** - proper spacing, consistent style, readable code

### 🔧 Powerful Generator Toolkit

[](#-powerful-generator-toolkit)

- **`indent()`** - Auto-indent nested code blocks
- **`join()`, `wrap()`, `prefix()`, `suffix()`** - Transform and compose generators
- **`comment()`, `blockComment()`, `docComment()`** - Generate any comment style
- **`dumpCall()`, `dumpAttribute()`** - Smart method calls and attributes
- **`statement()`** - Auto-add semicolons where needed

### 🎭 Flexibility First

[](#-flexibility-first)

- **Yield strings, arrays, or generators** - mix and match as needed
- **Compose generators** - results can be wrapped, joined, and transformed
- **Conditional generation** - `maybeWrap()` for optional structures
- **Multi-line strings** - automatic HEREDOC/NOWDOC handling

📦 Get Started in Seconds!
-------------------------

[](#-get-started-in-seconds)

Install via Composer and start generating beautiful code immediately:

```
composer require ruudk/code-generator --dev
```

That's it! You're ready to transform your PHP code generation. 🚀

💡 The Magic: Before &amp; After
-------------------------------

[](#-the-magic-before--after)

### ❌ Before (The Old Way)

[](#-before-the-old-way)

```
// Manual string concatenation, tracking indentation, managing imports...
$code = "
