Coder Social home page Coder Social logo

criskell / generator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from php-stubs/generator

0.0 0.0 0.0 96 KB

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

Home Page: https://packagist.org/packages/php-stubs/generator

License: MIT License

PHP 100.00%

generator's Introduction

PHP Stubs Generator

Build Status

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 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, and the code also relies on several Symfony components.

Contributions in the form of issues or Pull Requests are welcome!

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

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

To install:

composer require php-stubs/generator

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

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.

generator's People

Contributors

giacocorsiglia avatar herndlm avatar johnbillion avatar kkmuffme avatar machitgarha avatar simonhammes avatar szepeviktor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.