PHPackages                             gm314/nitria - 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. gm314/nitria

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

gm314/nitria
============

PHP 7 Code Generator

0.2.4(7mo ago)110.2k↓77.8%4MITPHPPHP &gt;=8.1.0

Since Feb 27Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/gperler/nitria)[ Packagist](https://packagist.org/packages/gm314/nitria)[ RSS](/packages/gm314-nitria/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (2)Versions (25)Used By (4)

Nitria PHP7 Code Generator
==========================

[](#nitria-php7-code-generator)

[![Build Status](https://camo.githubusercontent.com/d3a6e63543a20dbbe655173d3e106b162fe054fb7a82ab7c7a6d3396ba993528/68747470733a2f2f7472617669732d63692e6f72672f677065726c65722f6e69747269612e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gperler/nitria)[![License](https://camo.githubusercontent.com/815401c54c082939e356e03201d638dcaf95e9ee3399987cb9b2175e2163e532/68747470733a2f2f706f7365722e707567782e6f72672f676d3331342f6e69747269612f6c6963656e7365)](https://packagist.org/packages/gm314/siesta)[![Latest Stable Version](https://camo.githubusercontent.com/a760552875fb7a62fdc2f108915c78ca308a1776f832d1b6710ac1717f3ae7df/68747470733a2f2f706f7365722e707567782e6f72672f676d3331342f6e69747269612f762f737461626c65)](https://packagist.org/packages/gm314/siesta)[![Total Downloads](https://camo.githubusercontent.com/1e0189bd287659e54af2ba49a773173b0ceb7cc89c5343f5542caeda312a7d14/68747470733a2f2f706f7365722e707567782e6f72672f676d3331342f6e69747269612f646f776e6c6f616473)](https://packagist.org/packages/gm314/siesta)[![Latest Unstable Version](https://camo.githubusercontent.com/c10d6ff0d4d468a5e785be0c603a927ea94529e20df0ea2393746fc119fd7a84/68747470733a2f2f706f7365722e707567782e6f72672f676d3331342f6e69747269612f762f756e737461626c65)](https://packagist.org/packages/gm314/siesta)

Class generator for PHP7 code. It will take care of indentation, use statements, php doc creation.

Installation
============

[](#installation)

```
{
    "require": {
        "gm314/nitria": "*"
    }
}
```

Usage
=====

[](#usage)

Create a class and set extends and implements

```
$classGenerator = new ClassGenerator("Generated\\MyClass", true);

// add extends statement, a use statement will be added automatically
$classGenerator->setExtends("BaseClass\\ClassName");

// add implement statement
$classGenerator->addImplements("\\Serializable");

// add a constant
$classGenerator->addConstant("CONSTANT_STRING", '"hello"');
```

Properties
==========

[](#properties)

Properties have a name, a type, a modifier and an optional default value. The example will generate a `static` `private` property with the name `myName` and the default value `19.08`.

```
 $classGenerator->addProperty("myName","float","private", false, '19.08', 'doc bloc comment');
```

This will result in the following code

```
    /**
     * @var float doc bloc comment
     */
    private $myName = 19.08;
```

Or you can use the short version for non static properties.

```
// add property (for classes use statement will be added - unless the class is in the same namespace)
$classGenerator->addPrivateProperty("iAmPrivat", 'MyPackage\MyClass');
$classGenerator->addProtectedProperty("iAmProtected", "array", []);
$classGenerator->addPublicProperty("iAmPublic", "float");
```

And for the static properties.

```
// add static property
$classGenerator->addPrivateStaticProperty("iAmPrivatStatic", "int");
$classGenerator->addProtectedStaticProperty("iAmProtectedStatic", "bool");
$classGenerator->addPublicStaticProperty("iAmPublicStatic", "array");
```

Methods
=======

[](#methods)

```
$method = $classGenerator->addPublicMethod("myFunction");
$method->addParameter("string", "parameterName", '"defaultValue!"');
$method->addParameter("\\DateTime", "datetime");

// the method will have a return type string that is not nullable
$method->setReturnType("string", false);
$method->addCodeLine('return $parameterName;');
```

the above code will generate the following method

```
/**
 * @param string $parameterName
 * @param \DateTime $datetime
 * @return string
 */
public function myFunction(string $parameterName = "defaultValue!", \DateTime $datetime) : string {
    return $parameterName;
}
```

```
// method generation
$classGenerator->addPrivateMethod("iAmPrivate");
$classGenerator->addProtectedMethod("iAmProtected");
$classGenerator->addPublicMethod("iAmPublic");

// static method generation
$classGenerator->addPrivateStaticMethod("iAmPrivateStatic");
$classGenerator->addProtectedStaticMethod("iAmProtectedStatic");
$classGenerator->addPublicStaticMethod("iAmPublicStatic");
```

Method Content generation
=========================

[](#method-content-generation)

Code
----

[](#code)

```
$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "intParam");
$method->setReturnType("int", false);

// add a simple line of code
$method->addCodeLine('return $intParam * $intParam;');
```

If Statement
------------

[](#if-statement)

```
$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "int");
$method->setReturnType("int", false);

// start if statement >> if ($int ===1) {
$method->addIfStart('$int === 1');
$method->addCodeLine('return 1;');

// add if else statement >> } else if ($int === 2) {
$method->addIfElseIf('$int === 2');
$method->addCodeLine('return 2;');

// add else statement >> } else {
$method->addIfElse();
$method->addCodeLine('return 3;');

// close if statement >> }
$method->addIfEnd();
```

While Statement
---------------

[](#while-statement)

```
$method = $classGenerator->addPublicMethod("sayWhile");
$method->addParameter("int", "int");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start while statement >> while($int++ < 10) {
$method->addWhileStart('$int++ < 10');
$method->addCodeLine('$string .= "x";');

// end while statement >> }
$method->addWhileEnd();
$method->addCodeLine('return $string;');
```

Foreach Statement
-----------------

[](#foreach-statement)

```
$method = $classGenerator->addPublicMethod("sayForeach");
$method->addParameter("array", "list");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start foreach >> foreach($list as $item) {
$method->addForeachStart('$list as $item');
$method->addCodeLine('$string .= $item;');

// end foreach >> }
$method->addForeachEnd();
$method->addCodeLine('return $string;');
```

Switch Statement
----------------

[](#switch-statement)

```
$method = $classGenerator->addPublicMethod("saySwitch");
$method->addParameter("string", "value");
$method->setReturnType("string", false);

// start switch statement >> switch($value) {
$method->addSwitch('$value');

// case statement >> case "a":
$method->addSwitchCase('"a"');
$method->addCodeLine('return "a";');

// case break >> break;
$method->addSwitchBreak();

// default >> default:
$method->addSwitchDefault();
$method->addCodeLine('return "c";');
$method->addSwitchBreak();

$method->addSwitchEnd();
```

Tests / More examples
=====================

[](#tests--more-examples)

See under tests/End2End

- GeneratorTest
- CodeTest

License
=======

[](#license)

MIT

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance64

Regular maintenance activity

Popularity20

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~151 days

Recently: every ~307 days

Total

22

Last Release

220d ago

PHP version history (3 changes)0.1.0PHP &gt;=7.0.0

0.1.11PHP &gt;=7.1.0

0.2.0PHP &gt;=8.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/201e6d6e88070d43f5dfaf3f9052efa0d2e9703d3cdb529c95029c32f8973ffb?d=identicon)[gperler](/maintainers/gperler)

---

Top Contributors

[![gperler](https://avatars.githubusercontent.com/u/4823172?v=4)](https://github.com/gperler "gperler (46 commits)")

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/gm314-nitria/health.svg)

```
[![Health](https://phpackages.com/badges/gm314-nitria/health.svg)](https://phpackages.com/packages/gm314-nitria)
```

###  Alternatives

[amirezaeb/heroqr

A Powerful QR Code Management Library For PHP

9812.8k](/packages/amirezaeb-heroqr)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14114.1k13](/packages/netgen-content-browser)[jantinnerezo/livewire-range-slider

A Tall Stack wrapper for noUiSlider Range Slider

1511.6k1](/packages/jantinnerezo-livewire-range-slider)[agence-adeliom/easy-editor-bundle

A Symfony bundle for EasyAdmin that provide a flexible content editor

1111.3k1](/packages/agence-adeliom-easy-editor-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
