| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @link https://github.com/LAV45/yii2-settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2016 LAV45 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Alexey Loban <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license http://opensource.org/licenses/BSD-3-Clause | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | namespace lav45\settings\storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use yii\base\Object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use yii\helpers\FileHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class FileStorage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package lav45\settings\storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class FileStorage extends Object implements StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public $path = '@settings'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public $fileMode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public $dirMode = 0755; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $fileSuffix = '.bin'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |     public function init() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         parent::init(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $this->path = Yii::getAlias($this->path); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         FileHelper::createDirectory($this->path, $this->dirMode, true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return false|null|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     public function getValue($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $file = $this->getFile($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |         $fp = @fopen($file, 'r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         if ($fp !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |             @flock($fp, LOCK_SH); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             $value = @stream_get_contents($fp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             @flock($fp, LOCK_UN); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             @fclose($fp); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |     public function setValue($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $file = $this->getFile($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |         if (@file_put_contents($file, $value, LOCK_EX) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |             if ($this->fileMode !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |                 @chmod($file, $this->fileMode); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $error = error_get_last(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             Yii::warning("Unable to write file '{$file}': {$error['message']}", __METHOD__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |     public function deleteValue($key) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         $file = $this->getFile($key); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         return @unlink($file); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Returns the storage file path given the key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @return string the file path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     protected function getFile($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         return $this->path . DIRECTORY_SEPARATOR . $key . $this->fileSuffix; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  | } | 
            
                        
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.