Giter VIP home page Giter VIP logo

php-code-ast's People

Contributors

codeliner avatar open-code-modeling-bot avatar sandrokeil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

tacman

php-code-ast's Issues

ClassExtends is missing

Currently it is not possible to generate a class that extends another class. We need a generator similar to ClassImplements

Fix namespace and trait imports

To ensure the correct order, new statements has to be prepended to the current node statements. If not, namespace imports will be added after a class definition.

This reverts #21 and #22

Should typed = true be the default for properties?

Typed properties are available since PHP 7.4 and a de facto standard since then.
I'm wondering if properties should be typed by default and one can drop-out from this with setting the typed argument to false. At the moment it is the other way round.

Support DocComments for properties and methods

Unfortunately, we still need doc comments for certain types like arrays to provide better hints for IDEs and static code analysis tools like PHPStan. Therefor it would be useful to pass a string containing a DocComment to MethodGenerator and PropertyGenerator.

As a workaround I added the functionality using a custom node visitor like this one:

final class ClassMethodWithDocComment extends NodeVisitorAbstract
{
    /**
     * @var MethodGenerator
     **/
    private $methodGenerator;

    private string $docComment;

    public function __construct(MethodGenerator $methodGenerator, string $docComment)
    {
        $this->methodGenerator = $methodGenerator;
        $this->docComment = $docComment;
    }

    public function afterTraverse(array $nodes): ?array
    {
        $newNodes = [];

        foreach ($nodes as $node) {
            $newNodes[] = $node;

            if ($node instanceof Namespace_) {
                foreach ($node->stmts as $stmt) {
                    if ($stmt instanceof Stmt\Class_) {
                        if ($this->checkMethodExists($stmt)) {
                            return null;
                        }
                        $stmt->stmts[] = $this->addDocComment($this->methodGenerator->generate());
                    }
                }
            } elseif ($node instanceof Stmt\Class_) {
                if ($this->checkMethodExists($node)) {
                    return null;
                }
                $node->stmts[] = $this->addDocComment($this->methodGenerator->generate());
            }
        }

        return $newNodes;
    }

    private function checkMethodExists(Class_ $node): bool
    {
        foreach ($node->stmts as $stmt) {
            if ($stmt instanceof Node\Stmt\ClassMethod
                && $stmt->name->name === $this->methodGenerator->getName()
            ) {
                return true;
            }
        }

        return false;
    }

    private function addDocComment(Stmt\ClassMethod $method): Stmt\ClassMethod
    {
        $method->setDocComment(new Doc($this->docComment));
        return $method;
    }
}

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.