Skip to content

Incorrect behavior of RemoveDuplicatedReturnSelfDocblockRector, RemoveNonExistingVarAnnotationRector #9793

Description

@whataboutpereira

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/05307445-cea2-4393-bbea-ee1d3de8827f

<?php

declare(strict_types=1);

namespace App\Component;

/**
 * @template TValue
 * @template TError
 */
final readonly class Result
{
    /**
     * @param TValue $value
     * @param TError $error
     */
    private function __construct(
        private bool $success,
        private mixed $value = null,
        private mixed $error = null,
    ) {}

    /**
     * @template TSuccessValue
     *
     * @param TSuccessValue $value
     *
     * @return self<TSuccessValue, never>
     */
    #[\NoDiscard]
    public static function success(mixed $value = null): self
    {
        /** @var self<TSuccessValue, never> */
        return new self(true, value: $value);
    }

    /**
     * @template TFailureError
     *
     * @param TFailureError $error
     *
     * @return self<never, TFailureError>
     */
    #[\NoDiscard]
    public static function failure(mixed $error): self
    {
        /** @var self<never, TFailureError> */
        return new self(false, error: $error);
    }

    public function isSuccess(): bool
    {
        return $this->success;
    }

    public function isFailure(): bool
    {
        return !$this->success;
    }

    /**
     * @return TValue
     */
    public function getValue(): mixed
    {
        return $this->value;
    }

    /**
     * @return TError
     */
    public function getError(): mixed
    {
        return $this->error;
    }
}

Responsible rules

  • RemoveDuplicatedReturnSelfDocblockRector

  • RemoveNonExistingVarAnnotationRector

Expected Behavior

RemoveDuplicatedReturnSelfDocblockRector - @return here is needed to narrow the return type for phpstan
RemoveNonExistingVarAnnotationRector - @var is valid before a return statement and affects the returned value, also needed by phpstan

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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