Skip to content

How to add a namespace to a class if non is set? #8289

@numediaweb

Description

@numediaweb

Question

I have been playing with this nice library: good job by the way 👏

Trying to create my own custom rule that adds a namespace to a class if non is set.. But I couldn't figure it out sofar...

The file I have:

<?php

class MyClass extends SecureControllerAction {
...
}

The results I expect:

<?php

namespace App\Controller;

class MyClass extends SecureControllerAction {
...
}

My current rule

    /**
     * @return string[]
     */
    public function getNodeTypes(): array
    {
        return [
            Name::class,
            Class_::class,
            Namespace_::class,
        ];
    }

    /**
     * @param Class_ $node
     */
    public function refactor(Node $node): ?Node
    {
         // Here I'am stuck as the file itself doesn't have a namespace node!
    }

Any help would be appreciated 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions