PHPackages                             stefna/php-code-builder - 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. stefna/php-code-builder

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

stefna/php-code-builder
=======================

2.5.3(3mo ago)112.0k↓44.1%2MITPHPPHP ^8.1CI passing

Since May 5Pushed 3mo ago5 watchersCompare

[ Source](https://github.com/stefna/php-code-builder)[ Packagist](https://packagist.org/packages/stefna/php-code-builder)[ RSS](/packages/stefna-php-code-builder/feed)WikiDiscussions 2.x Synced 1mo ago

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

Php code builder
================

[](#php-code-builder)

Installation
------------

[](#installation)

```
$ composer require stefna/php-code-builder

```

API
---

[](#api)

### PhpFile

[](#phpfile)

The actual file to save.

#### Methods

[](#methods)

##### `setStrict()`

[](#setstrict)

Mark files with `declare(strict_types=1);`

##### `setNamespace(string $ns)`

[](#setnamespacestring-ns)

Set file namespace

##### `setSource(string $code)`

[](#setsourcestring-code)

Set raw php code to be included in file

Like

```
$file = new PhpFile();
$file->setSource("spl_autoload_register('$autoloaderName');");
```

##### `addFunction(PhpFunction $func)`

[](#addfunctionphpfunction-func)

Add function to file. Files can contain multiple functions

##### `addClass(PhpClass $class)`

[](#addclassphpclass-class)

Add class to file. Files can contain multiple classes

##### `addTrait(PhpTrait $trait)`

[](#addtraitphptrait-trait)

Add trait to file. Files can contain multiple traits

##### `addInterface(PhpInterface $interface)`

[](#addinterfacephpinterface-interface)

Add interface to file. Files can contain multiple interfaces

### PhpParam

[](#phpparam)

Used to make complex parameter arguments

#### Usage

[](#usage)

```
new PhpParam('string', 'test') => string $test
new PhpParam('object', 'test', null) => object $test = null

$param = new PhpParam('DateTimeInterface', 'date');
$param->allowNull(true);
$param->getSource() => ?DateTimeInterface $date

$param = new PhpParam('float', 'price', 1.5);
$param->allowNull(true);
$param->getSource() => ?float $price = 1.5
```

### PhpFunction

[](#phpfunction)

#### Usage

[](#usage-1)

`__construct(string $identifier, array $params, string $source, ?PhpDocComment $comment = null, ?string $returnTypeHint = null)`

##### Example

[](#example)

```
$func = new PhpFunction(
    'testFunc',
    [
        'param1', // Simple parameter
        new PhpParam('int', 'param2', 1),
    ],
    "return $param1 + $param2",
    null, // no docblock
    'int'
);

echo $func->getSource();
```

##### Output:

[](#output)

```
function testFunc($param1, int $param2 = 1): int
{
    return $param1 + $param2;
}
```

### PhpMethod

[](#phpmethod)

Method is an extension of `PhpFunction` with support for accessors like `public`, `protected`, `private`, `final`, `static` and `abstract`

#### Usage

[](#usage-2)

With return typehint

```
$method = new PhpMethod('private', 'test', [], 'return 1', null, 'int');
echo $method->getSource();

-------

private function test(): int
{
    return 1;
}
```

With docblock

```
$method = new PhpMethod(
    'private',
    'test',
    [],
    'return 1',
    new PhpDocComment('Test Description')
);
echo $method->getSource();

-------
/**
 * Test Description
 */
private function test()
{
    return 1;
}
```

Static method

```
$method = new PhpMethod('protected', 'test', [], 'return 1');
$method->setStatic();
echo $method->getSource();

-------

protected static function test()
{
    return 1;
}
```

Final method

```
$method = new PhpMethod('public', 'test', [], 'return 1');
$method->setFinal();
echo $method->getSource();

-------

final public function test()
{
    return 1;
}
```

Abstract method

```
$method = new PhpMethod('public', 'test', [], 'return 1', null, 'int');
$method->setAbstract();
echo $method->getSource();

-------

abstract public function test(): int;
```

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance80

Actively maintained with recent releases

Popularity29

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 99.2% 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 ~123 days

Recently: every ~19 days

Total

18

Last Release

103d ago

Major Versions

1.x-dev → 2.0.02023-04-04

PHP version history (2 changes)1.0.0PHP &gt;=7.2.0

2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce9d40996574fd1f18f69556c872c6eb94e59977c5f0b33c9df541d82fbb3800?d=identicon)[sunkan](/maintainers/sunkan)

---

Top Contributors

[![sunkan](https://avatars.githubusercontent.com/u/568492?v=4)](https://github.com/sunkan "sunkan (242 commits)")[![dcolt](https://avatars.githubusercontent.com/u/16351965?v=4)](https://github.com/dcolt "dcolt (1 commits)")[![Marrikulus](https://avatars.githubusercontent.com/u/15344566?v=4)](https://github.com/Marrikulus "Marrikulus (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/stefna-php-code-builder/health.svg)

```
[![Health](https://phpackages.com/badges/stefna-php-code-builder/health.svg)](https://phpackages.com/packages/stefna-php-code-builder)
```

PHPackages © 2026

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