MikeLindsay
1 week ago

Does this code break the LSP, as I'm deferring to another method and then returning an array?

Posted 1 week ago by MikeLindsay

interface UserRepository {
    /**
     * @return array
     */
    public function all();
}

interface FetchableWebService {
    /**
     * @return array
     */
    public function fetch();
}

class DatabaseUserRepository implements UserRepository {
    public function all()
    {
        return ['Data from local database'];
    }
}

class WebServiceUserRespository implements UserRepository, FetchableWebService {

    /* Am I breaking the Liskov Substitution principle by calling the fetch method
    even though it is returning an array type just like the DatabaseUserRepository?
    */
    public function all()
    {
        return $this->fetch();
    }

    public function fetch()
    {
        return ['Data From User stored on a 3rd Party that exposes an API'];
    }
}


Please sign in or create an account to participate in this conversation.