src/main/php/NewInstance.php
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;
    }
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;
    }
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 ; }
detects whether a method should return the instance or null

@param   \ReflectionClass $class
@param   \ReflectionMethod $method
@return  bool
detects whether a method should return the instance or null