| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BFW\Memcache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class to manage connection to memcache(d) server with memcached lib | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Memcached extends \Memcached implements MemcacheInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     //Include Memcache trait to add some common methods with Memcache class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     use \BFW\Memcache\MemcacheTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @const ERR_SERVER_INFOS_FORMAT Exception code if server informations is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * not in a correct format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const ERR_SERVER_INFOS_FORMAT = 1309001; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @const ERR_NO_SERVER_CONNECTED Exception code if no server is connected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const ERR_NO_SERVER_CONNECTED = 1309002; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @const ERR_A_SERVER_IS_NOT_CONNECTED Exception code if a server is not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * connected. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     const ERR_A_SERVER_IS_NOT_CONNECTED = 1309003; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @const ERR_IFEXISTS_PARAM_TYPE Exception code if a parameter type is not | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * correct into the method ifExists(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     const ERR_IFEXISTS_PARAM_TYPE = 1309004; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @const ERR_UPDATEEXPIRE_PARAM_TYPE Exception code if a parameter type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * is not correct into the method updateExpire(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     const ERR_UPDATEEXPIRE_PARAM_TYPE = 1309005; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @const ERR_KEY_NOT_EXIST Exception code if the asked key not exist. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * Actually only used into the method updateExpire(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     const ERR_KEY_NOT_EXIST = 1309006; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var array $config Config define into bfw config file for memcache(d) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Call parent constructor with the persistentId if declared in config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Connect to servers. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $app          = \BFW\Application::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->config = $app->getConfig()->getValue( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'memcached', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             'memcached.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (!empty($this->config['persistentId'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             parent::__construct($this->config['persistentId']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Get accessor to the property config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function getConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Get the list of server already connected (persistent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Loop on server declared into the config file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Connect to server if not already connected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     public function connectToServers() | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         //Array for the list of server(s) to connect | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $addServers  = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         //Get all server already connected (persistent) | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $serversList = $this->generateServerList(); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         //Loop on server declared into config | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         foreach ($this->config['servers'] as $server) { | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             $this->completeServerInfos($server); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $host   = $server['host']; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             $port   = $server['port']; | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             $weight = $server['weight']; | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             //not check if port = (int) 0; Doc said to define to 0 for socket. | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             if (empty($host) || $port === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             //search if the reading server is not already connected | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             if (in_array($host.':'.$port, $serversList)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 continue; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             \BFW\Application::getInstance() | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 ->getMonolog() | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 ->getLogger() | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 ->debug( | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                     'Try to connect to memcached server.', | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                     [ | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                         'host' => $host, | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |                         'port' => $port | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |                     ] | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             //If not, add the server at the list to connect | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             $addServers[] = [$host, $port, $weight]; | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         //Connect to server(s) | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         $this->addServers($addServers); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |         //Check if connect is successfull | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         $this->testConnect(); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * Get the list of servers where we are already connected (persistent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     protected function generateServerList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $serversList = $this->getServerList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $servers     = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         foreach ($serversList as $serverInfos) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             $servers[] = $serverInfos['host'].':'.$serverInfos['port']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return $servers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 155 |  |  |  | 
            
                        
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..