Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading from 3.1.4 to 3.1.5 throws warning when using typehinting #464

Open
c0deright opened this issue Oct 19, 2020 · 3 comments
Open

Comments

@c0deright
Copy link

Since upgrading from 3.1.4 to 3.1.5 we're getting warning

PHP Warning: Declaration of MemcachedWrapper::addServers(array $a_servers): bool should be compatible with Memcached::addServers($servers) in test.php on line 47

<?php

/**
 * Class MemcachedWrapper
 * @package App\System\Memcached
 */
class MemcachedWrapper extends \Memcached
{
    /**
     * Prevent adding of new servers as duplicates
     *
     * @param array $a_servers
     *
     * @return bool
     */
    public function addServers(array $a_servers): bool
    {
        if (TRUE === empty($this->getServerList()))
        {
            return parent::addServers($a_servers);
        }

        return FALSE;
    }

    /**
     * Prevent adding of new server as duplicate
     *
     * @param string $s_host
     * @param mixed  $m_port
     * @param int    $i_weight
     *
     * @return bool
     */
    public function addServer($s_host, $m_port, $i_weight = 0): bool
    {
        foreach ($this->getServerList() as $server)
        {
            if ($server['host'] === $s_host && (int) $server['port'] === (int) $m_port)
            {
                return FALSE;
            }
        }

        return parent::addServer($s_host, $m_port, $i_weight);
    }
}

$m = new MemcachedWrapper();
$m->addServer('localhost', 11211);

print_r($m->getVersion());
% php test.php
PHP Warning:  Declaration of MemcachedWrapper::addServers(array $a_servers): bool should be compatible with Memcached::addServers($servers) in test.php on line 47
Array
(
    [localhost:11211] => 1.6.7
)

Server

  • memcached-1.6.7

Client

PHP Version => 7.1.33-19+ubuntu16.04.1+deb.sury.org+1
PHP API => 20160303
PHP Extension => 20160303

memcached

memcached support => enabled
Version => 3.1.5
libmemcached version => 1.0.18
SASL support => yes
Session support => yes
igbinary support => yes
json support => yes
msgpack support => yes

Directive => Local Value => Master Value
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.default_binary_protocol => Off => Off
memcached.default_connect_timeout => 0 => 0
memcached.default_consistent_hash => Off => Off
memcached.serializer => php => php
memcached.sess_binary_protocol => On => On
memcached.sess_connect_timeout => 0 => 0
memcached.sess_consistent_hash => On => On
memcached.sess_consistent_hash_type => ketama => ketama
memcached.sess_lock_expire => 0 => 0
memcached.sess_lock_max_wait => not set => not set
memcached.sess_lock_retries => 5 => 5
memcached.sess_lock_wait => not set => not set
memcached.sess_lock_wait_max => 150 => 150
memcached.sess_lock_wait_min => 150 => 150
memcached.sess_locking => On => On
memcached.sess_number_of_replicas => 0 => 0
memcached.sess_persistent => Off => Off
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
memcached.sess_randomize_replica_read => Off => Off
memcached.sess_remove_failed_servers => Off => Off
memcached.sess_sasl_password => no value => no value
memcached.sess_sasl_username => no value => no value
memcached.sess_server_failure_limit => 0 => 0
memcached.store_retry_count => 2 => 2
@GrahamCampbell
Copy link
Contributor

I think this is correct. You cannot restrict the parameter type to array. It is a bug that PHP was not warning you about this before. Parameters must be contravariant in PHP.

@c0deright
Copy link
Author

I think this is correct. You cannot restrict the parameter type to array. It is a bug that PHP was not warning you about this before. Parameters must be contravariant in PHP.

According to https://github.com/php-memcached-dev/php-memcached/blob/master/php_memcached.stub.php#L55 you're declaring the function as

public function addServers(array $servers): bool {}

which is exactly what we're using in the class that extends Memcached class.

Can you elaborate why this WARNING is correct, please?

@marcos-devarts
Copy link

Sorry @GrahamCampbell, but I have the same problem as @c0deright, and I agree with him: the function signature includes array $servers in:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants