PHPackages                             nbasnet/php-codewriter - 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. nbasnet/php-codewriter

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

nbasnet/php-codewriter
======================

Helper classes to generate code for writing to a file

0.1.0(9y ago)0450PHP

Since Feb 10Pushed 9y ago1 watchersCompare

[ Source](https://github.com/nischalbasnet/php-codewriter)[ Packagist](https://packagist.org/packages/nbasnet/php-codewriter)[ RSS](/packages/nbasnet-php-codewriter/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

php-codewriter
==============

[](#php-codewriter)

\######Generate code for writing to file using different components

##### Need to pass instance of `CodeWriterSettings` to the top most component using `setSettings()` \[inherited from BaseComponent Class\] before calling `writeComponent()` method. Exception `SettingsNotSet` is thrown if `writeComponent()` is called before settings is set.

[](#need-to-pass-instance-of-codewritersettings-to-the-top-most-component-using-setsettings-inherited-from-basecomponent-class-before-calling-writecomponent-method-exception-settingsnotset-is-thrown-if-writecomponent-is-called-before-settings-is-set)

```
      CodeWriterSettings::create(ISyntaxGrammar::PHP, $indent = 0)
```

#### 1 . Variable Component

[](#1--variable-component)

```
      $variable = VariableComponent::create("var")->setValue("Is Name", "string")->writeComponent();

OUTPUT:
      $var = 'Is Name';
```

###### Constants:

[](#constants)

```
      $constant = VariableComponent::create("TEST")->setValue("VALUE 1")->makeConstant()->writeComponent();

OUTPUT:
      const TEST = 'VALUE 1';
```

#### 2 . Array Component

[](#2--array-component)

```
      $array    = ArrayComponent::create("what_is_this", TRUE)
        ->setValue([
            "string" => "is game",
            "number" => 2,
            "bool"   => FALSE,
        ])
        ->writeComponent();

OUTPUT:
      $what_is_this = [
            'string' => 'is game',
            'number' => 2,
            'bool' => false,
        ];
```

#### 3 . Function Component

[](#3--function-component)

```
      $function = FunctionComponent::create("myFunction")
        ->setAccessIdentifier(BaseComponent::ACCESS_PUBLIC)
        ->setParameters([ArrayComponent::create("my_array"), $variable])
        ->appendComponent($array)
        ->appendComponent($variable)
        ->writeComponent();

OUTPUT:
      /**
       * @param array $my_array
       * @param string $val
       */
      public function myFunction(array $my_array, $val = 'Is Name')
      {
          $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
          ];
          $nischal = 'Is Name';
      }
```

#### 4 . Class Component

[](#4--class-component)

```
      $class = ClassComponent::create('TestController')
        ->setExtends("Controller")
        ->appendComponent($variable)
        ->appendComponent($constant)
        ->appendBlankLine()
        ->appendComponent($array)
        ->appendBlankLine()
        ->appendComponent($function)
        ->writeComponent();

OUTPUT:
      /**
       * Class TestController
       */
      class TestController extends Controller
      {
           $var = 'Is Name';
           const TEST = 'VALUE 1';

           $what_is_this = [
              'string' => 'is game',
              'number' => 2,
              'bool' => false,
           ];

           /**
            * @param array $my_array
            * @param string $val
            */
           public function myFunction(array $my_array, $val = 'Is Name')
           {
               $what_is_this = [
                   'string' => 'is game',
                   'number' => 2,
                   'bool' => false,
               ];
               $nischal = 'Is Name';
           }
      }
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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 ~6 days

Total

2

Last Release

3422d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6358712?v=4)[nbasnet](/maintainers/nbasnet)[@Nbasnet](https://github.com/Nbasnet)

---

Top Contributors

[![nischalbasnet](https://avatars.githubusercontent.com/u/11860586?v=4)](https://github.com/nischalbasnet "nischalbasnet (23 commits)")

---

Tags

code-generationphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nbasnet-php-codewriter/health.svg)

```
[![Health](https://phpackages.com/badges/nbasnet-php-codewriter/health.svg)](https://phpackages.com/packages/nbasnet-php-codewriter)
```

PHPackages © 2026

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