Skip to content

Incorrect behavior of TypedPropertyRector: Generates non-nullable properties and initializes them with null #6832

@YetiCGN

Description

@YetiCGN

Bug Report

Subject Details
Rector version last dev-master
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.org/demo/1ec4d5b4-8951-6fd0-ae3f-410987af81e1

<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Table(name="board", indexes={@ORM\Index(columns={"name"}, flags={"fulltext"})})
 * @ORM\Entity(repositoryClass="AppBundle\Repository\BoardRepository")
 * @Vich\Uploadable
 */
class Board
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @Assert\Length(max="128", min="4")
     * @Assert\NotBlank()
     * @ORM\Column(name="name", type="string")
     */
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="UserBoardAccess", mappedBy="board", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $users;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\BoardMoodImage", mappedBy="board", cascade={"persist", "remove"}, orphanRemoval=true)
     * @ORM\OrderBy({"index" = "ASC"})
     * @Assert\Valid()
     */
    private $moodImages;

    public function __construct()
    {
        $this->moodImages = new ArrayCollection();
    }

    public function __clone()
    {
        if ($this->id) {
            $this->id = null;
        }
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param int $id
     */
    public function setId(int $id)
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get users
     *
     * @return mixed
     */
    public function getUsers()
    {
        return $this->users;
    }

    /**
     * Add user
     *
     * @param \AppBundle\Entity\UserBoardAccess $user
     *
     * @return Board
     */
    public function addUser(\AppBundle\Entity\UserBoardAccess $user)
    {
        $this->users[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \AppBundle\Entity\UserBoardAccess $user
     */
    public function removeUser(\AppBundle\Entity\UserBoardAccess $user)
    {
        $this->users->removeElement($user);
    }

    /**
     * @return mixed
     */
    public function getMoodImages()
    {
        return $this->moodImages;
    }

    /**
     * @param mixed $moodImages
     */
    public function setMoodImages($moodImages)
    {
        $this->moodImages = $moodImages;
    }

    /**
     * Add BoardMoodImage
     *
     * @param \AppBundle\Entity\BoardMoodImage $moodImage
     *
     * @return Board
     */
    public function addMoodImage(BoardMoodImage $moodImage)
    {
        $this->moodImages[] = $moodImage;

        return $this;
    }

    /**
     * Remove BoardMoodImage
     *
     * @param \AppBundle\Entity\BoardMoodImage $moodImage
     */
    public function removeMoodImage(BoardMoodImage $moodImage)
    {
        $this->moodImages->removeElement($moodImage);
    }
}

Responsible rules

  • TypedPropertyRector

Expected Behavior

The moodImage property is initialized with null but not declared as nullable, whereas the users property works just fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions