private static function shouldReturnSelf(\ReflectionClass $class, \ReflectionMethod $method): bool
    {
        $returnType = self::detectReturnType($method);
        if (null === $returnType) {
            return false;
        }

        if (in_array($returnType, ['$this', 'self', $class->getName(), $class->getShortName()])) {
            return true;
        }

        foreach ($class->getInterfaces() as $interface) {
            if ($interface->getName() !== 'Traversable' && ($interface->getName() === $returnType || $interface->getShortName() === $returnType)) {
                return true;
            }
        }

        while ($parent = $class->getParentClass()) {
            if ($parent->getName() === $returnType || $parent->getShortName() === $returnType) {
                return true;
            }

            $class = $parent;
        }

        return false;
    }