| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of ninja-mutex. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (C) Kamil Dziedzic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace NinjaMutex\Lock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Lock implementor using MySQL PDO driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Kamil Dziedzic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class MySQLPDOLock extends LockAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * MySQL connections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var PDO[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $pdo = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $dsn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected $username; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $passwd; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected $classname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Provide data for PDO connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @link http://php.net/manual/en/pdo.construct.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $dsn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $username [optional] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string $passwd   [optional] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param array  $options  [optional] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $classname class name to create as PDO connection, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      *                          by default this is PDO, but in tests we can inject MockPDO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function __construct($dsn, $username = null, $passwd = null, $options = null, $classname = 'PDO') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->dsn = $dsn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->username = $username; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->passwd = $passwd; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->options = $options; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->classname = $classname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 10 |  |     public function __clone() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 10 |  |         parent::__clone(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 10 |  |         $this->pdo = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Acquire lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param  string   $name    name of lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param  null|int $timeout 1. null if you want blocking lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      *                           2. 0 if you want just lock and go | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      *                           3. $timeout > 0 if you want to wait for lock some time (in milliseconds) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 32 |  |     public function acquireLock($name, $timeout = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 32 |  |         if (!$this->setupPDO($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 32 |  |         return parent::acquireLock($name, $timeout); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param  string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param  bool   $blocking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 32 |  |     protected function getLock($name, $blocking) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 32 |  |         return !$this->isLocked($name) && $this->pdo[$name]->query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 32 |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 32 |  |                 'SELECT GET_LOCK(%s, %d)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 32 |  |                 $this->pdo[$name]->quote($name), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 32 |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 32 |  |             PDO::FETCH_COLUMN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 32 |  |         )->fetch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Release lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param  string $name name of lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 32 |  |     public function releaseLock($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 32 |  |         if (!$this->setupPDO($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 32 |  |         $released = (bool) $this->pdo[$name]->query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 32 |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 32 |  |                 'SELECT RELEASE_LOCK(%s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 32 |  |                 $this->pdo[$name]->quote($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 32 |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 32 |  |             PDO::FETCH_COLUMN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 32 |  |         )->fetch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 32 |  |         if (!$released) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 4 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 32 |  |         unset($this->pdo[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 32 |  |         unset($this->locks[$name]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 32 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Check if lock is locked | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param  string $name name of lock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 32 |  |     public function isLocked($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 32 |  |         if (empty($this->pdo) && !$this->setupPDO($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 32 |  |         return !current($this->pdo)->query( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 32 |  |             sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 32 |  |                 'SELECT IS_FREE_LOCK(%s)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 32 |  |                 current($this->pdo)->quote($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 32 |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 32 |  |             PDO::FETCH_COLUMN, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 32 |  |         )->fetch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param  string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 32 |  |     protected function setupPDO($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 32 |  |         if (isset($this->pdo[$name])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 32 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 32 |  |         $this->pdo[$name] = new $this->classname($this->dsn, $this->username, $this->passwd, $this->options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 32 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 182 | 10 |  |     public function __destruct() | 
            
                                                        
            
                                    
            
            
                | 183 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 184 | 10 |  |         parent::__destruct(); | 
            
                                                        
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 186 | 10 |  |         foreach($this->pdo as $name => $pdo) { | 
            
                                                        
            
                                    
            
            
                | 187 | 6 |  |             unset($this->pdo[$name]); | 
            
                                                        
            
                                    
            
            
                | 188 | 10 |  |         } | 
            
                                                        
            
                                    
            
            
                | 189 | 10 |  |     } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  | 
            
                        
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needlebut will only accept an array as$haystack.