extension_builder icon indicating copy to clipboard operation
extension_builder copied to clipboard

Add property annotations instead of php doc comments

Open simonschaufi opened this issue 4 years ago • 4 comments

Instead of the following code:

class News extends AbstractEntity
{
    /**
     * title
     *
     * @var string
     */
    protected $title = '';

    /**
     * Returns the title
     *
     * @return string $title
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Sets the title
     *
     * @param string $title
     * @return void
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }
}

I want this code to be generated:

class News extends AbstractEntity
{
    /**
     * title
     *
     * @var string
     */
    protected $title = '';

    public function getTitle(): string
    {
        return $this->title;
    }

    public function setSlug(string $slug): void
    {
        $this->title = $title;
    }
}

simonschaufi avatar May 13 '21 23:05 simonschaufi

The property can't be of type string as extbase accesses the property in some cases before it is initialized. Otherwise the constructor needs to initialize all properties in the beginning

simonschaufi avatar May 13 '21 23:05 simonschaufi

The property can't be of type string as extbase accesses the property in some cases before it is initialized. Otherwise the constructor needs to initialize all properties in the beginning

We do this already in extensions, but all model properties need to be initialized, so this works:

class News extends AbstractEntity
{
    protected string $title = '';

    public function getTitle(): string
    {
        return $this->title;
    }

    public function setSlug(string $slug): void
    {
        $this->title = $title;
    }
}

I also opt to remove the phpdoc text that simply reflects the properties name. This does not convey any useful further information and just bloats the source code.

liayn avatar May 14 '21 06:05 liayn

@liayn do you have an understanding of how the php parser works? The entry point is this file: Classes/Service/ClassBuilder.php:generateModelClassFileObject and this one: Classes/Service/FileGenerator.php:generateDomainObjectCode.

simonschaufi avatar May 14 '21 08:05 simonschaufi

We do this already in extensions, but all model properties need to be initialized, so this works:

I agree but some properties can also be null and then it crashes

simonschaufi avatar May 14 '21 08:05 simonschaufi