PHPackages                             php-stubs/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. php-stubs/generator

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

php-stubs/generator
===================

Generate stubs from any PHP code for IDE completion and static analysis.

v0.9.0(5d ago)82333.6k↓33.2%25[4 issues](https://github.com/php-stubs/generator/issues)[1 PRs](https://github.com/php-stubs/generator/pulls)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Feb 12Pushed 5d ago1 watchersCompare

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

READMEChangelog (10)Dependencies (20)Versions (16)Used By (20)

PHP Stubs Generator
===================

[](#php-stubs-generator)

[![Build Status](https://github.com/php-stubs/generator/actions/workflows/test.yml/badge.svg)](https://github.com/php-stubs/generator/actions/workflows/test.yml)

Use this tool to generate stub declarations for functions, classes, interfaces, and global variables defined in any PHP code. The stubs can subsequently be used to facilitate IDE completion or static analysis via [PHPStan](https://phpstan.org) or potentially other tools. Stub generation is particularly useful for code which mixes definitions with side-effects.

The generator is based on nikic's [PHP-Parser](https://github.com/nikic/PHP-Parser), and the code also relies on several [Symfony](https://symfony.com) components.

Contributions in the form of [issues](https://github.com/php-stubs/generator/issues/new) or Pull Requests are welcome!

Example
-------

[](#example)

The idea is to turn this:

```
// source-file-1.php
/**
 * @param string $bar
 * @return int
 */
function foo($bar)
{
    return (int) $bar;
}

/** @var string */
$something = '123abc';

if ($something) {
    echo foo($something);
}

// source-file-2.php
namespace MyNamespace;

class MyClass extends MyParentClass
{
    public function method(): string
    {
        return '';
    }
}
```

Into this:

```
// stubs.php
namespace {
    /**
     * @param string $bar
     * @return int
     */
    function foo($bar)
    {
    }

    /** @var string */
    $something = '123abc';
}

namespace MyNamespace {
    class MyClass extends MyParentClass
    {
        public function method(): string
        {
        }
    }
}
```

Command Line Usage
------------------

[](#command-line-usage)

To install:

```
composer global require php-stubs/generator
```

To get the pretty-printed stubs for all the PHP files in a directory:

```
generate-stubs /path/to/my-library
```

You may also pass multiple directories, or filenames, separated by spaces. All stubs will be concatenated in the output.

To write the stubs to a file (and see a few statistics in the stdout):

```
generate-stubs /path/to/my-library --out=/path/to/output.php
```

For the complete set of command line options:

```
generate-stubs --help
```

Usage in PHP
------------

[](#usage-in-php)

To install:

```
composer require php-stubs/generator
```

### Simple Example

[](#simple-example)

```
// You'll need the Composer Autoloader.
require 'vendor/autoload.php';

// You may alias the classnames for convenience.
use StubsGenerator\{StubsGenerator, Finder};

// First, instantiate a `StubsGenerator\StubsGenerator`.
$generator = new StubsGenerator();

// Then, create a `StubsGenerator\Finder` which contains
// the set of files you wish to generate stubs for.
$finder = Finder::create()->in('/path/to/my-library/');

// Now you may use the `StubsGenerator::generate()` method,
// which will return a `StubsGenerator\Result` instance.
$result = $generator->generate($finder);

// You can use the `Result` instance to pretty-print the stubs.
echo $result->prettyPrint();

// You can also use it to retrieve the PHP-Parser nodes
// that represent the generated stub declarations.
$stmts = $result->getStubStmts();
```

### Additional Features

[](#additional-features)

You can restrict the set of symbol types for which stubs are generated:

```
// This will only generate stubs for function declarations.
$generator = new StubsGenerator(StubsGenerator::FUNCTIONS);

// This will only generate stubs for class or interface declarations.
$generator = new StubsGenerator(StubsGenerator::CLASSES | StubsGenerator::INTERFACES);
```

The set of symbol types are:

- `StubsGenerator::FUNCTIONS`: Function declarations.
- `StubsGenerator::CLASSES`: Class declarations.
- `StubsGenerator::TRAITS`: Trait declarations.
- `StubsGenerator::INTERFACES`: Interface declarations.
- `StubsGenerator::DOCUMENTED_GLOBALS`: Global variables, but only those with a doc comment.
- `StubsGenerator::UNDOCUMENTED_GLOBALS`: Global variable, but only those without a doc comment.
- `StubsGenerator::GLOBALS`: Shortcut to include both documented and undocumented global variables.
- `StubsGenerator::CONSTANTS`: Constant declarations.
- `StubsGenerator::DEFAULT`: Shortcut to include everything *except* undocumented global variables.
- `StubsGenerator::ALL`: Shortcut to include everything.

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance98

Actively maintained with recent releases

Popularity51

Moderate usage in the ecosystem

Community37

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~309 days

Total

15

Last Release

5d ago

PHP version history (4 changes)v0.1PHP &gt;=7.1

v0.6.0PHP ^7.3

v0.7.0PHP ^7.3 || ^8.0

v0.9.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/135d8104cdf926e2b15b6a06413153a9582729f0cd48ccba7bc7e62878a39781?d=identicon)[szepeviktor](/maintainers/szepeviktor)

---

Top Contributors

[![GiacoCorsiglia](https://avatars.githubusercontent.com/u/15148404?v=4)](https://github.com/GiacoCorsiglia "GiacoCorsiglia (37 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (18 commits)")[![simonhammes](https://avatars.githubusercontent.com/u/10352679?v=4)](https://github.com/simonhammes "simonhammes (8 commits)")[![machitgarha](https://avatars.githubusercontent.com/u/14151550?v=4)](https://github.com/machitgarha "machitgarha (7 commits)")[![IanDelMar](https://avatars.githubusercontent.com/u/42134098?v=4)](https://github.com/IanDelMar "IanDelMar (5 commits)")[![johnbillion](https://avatars.githubusercontent.com/u/208434?v=4)](https://github.com/johnbillion "johnbillion (4 commits)")[![kkmuffme](https://avatars.githubusercontent.com/u/11071985?v=4)](https://github.com/kkmuffme "kkmuffme (3 commits)")[![herndlm](https://avatars.githubusercontent.com/u/5738896?v=4)](https://github.com/herndlm "herndlm (2 commits)")[![mikemanger](https://avatars.githubusercontent.com/u/74821?v=4)](https://github.com/mikemanger "mikemanger (2 commits)")[![thelovekesh](https://avatars.githubusercontent.com/u/54371619?v=4)](https://github.com/thelovekesh "thelovekesh (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![philipjohn](https://avatars.githubusercontent.com/u/136342?v=4)](https://github.com/philipjohn "philipjohn (1 commits)")[![selfiens](https://avatars.githubusercontent.com/u/2247403?v=4)](https://github.com/selfiens "selfiens (1 commits)")[![criskell](https://avatars.githubusercontent.com/u/96352451?v=4)](https://github.com/criskell "criskell (1 commits)")

---

Tags

idestatic analysis

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/php-stubs-generator/health.svg)

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

###  Alternatives

[jolicode/castor

A lightweight and modern task runner. Automate everything. In PHP.

54743.1k4](/packages/jolicode-castor)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)[friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

13.5k251.2M25.2k](/packages/friendsofphp-php-cs-fixer)[deptrac/deptrac

Deptrac is a static code analysis tool that helps to enforce rules for dependencies between software layers.

3.0k8.8M118](/packages/deptrac-deptrac)[friendsoftypo3/content-blocks

TYPO3 CMS Content Blocks - Content Types API | Define reusable components via YAML

103519.9k53](/packages/friendsoftypo3-content-blocks)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)

PHPackages © 2026

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