| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Psalm\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use function array_filter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use function array_map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use function dirname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use function in_array; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function scandir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SimpleXMLElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function strtolower; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function substr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use const SCANDIR_SORT_NONE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class IssueHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $error_level = \Psalm\Config::REPORT_ERROR; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array<ErrorLevelFileFilter> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $custom_levels = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param  SimpleXMLElement $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param  string           $base_dir | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public static function loadFromXMLElement(SimpleXMLElement $e, $base_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $handler = new self(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 36 |  | View Code Duplication |         if (isset($e['errorLevel'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $handler->error_level = (string) $e['errorLevel']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             if (!in_array($handler->error_level, \Psalm\Config::$ERROR_LEVELS, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 throw new \Psalm\Exception\ConfigException('Unexpected error level ' . $handler->error_level); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         /** @var \SimpleXMLElement $error_level */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($e->errorLevel as $error_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $handler->custom_levels[] = ErrorLevelFileFilter::loadFromXMLElement($error_level, $base_dir, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $error_level | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function setErrorLevel($error_level) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!in_array($error_level, \Psalm\Config::$ERROR_LEVELS, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             throw new \Psalm\Exception\ConfigException('Unexpected error level ' . $error_level); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->error_level = $error_level; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $file_path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function getReportingLevelForFile($file_path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($custom_level->allows($file_path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this->error_level; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $fq_classlike_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function getReportingLevelForClass($fq_classlike_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             if ($custom_level->allowsClass($fq_classlike_name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $method_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function getReportingLevelForMethod($method_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             if ($custom_level->allowsMethod(strtolower($method_id))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function getReportingLevelForFunction(string $function_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             if ($custom_level->allowsMethod(strtolower($function_id))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     public function getReportingLevelForArgument(string $function_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             if ($custom_level->allowsMethod(strtolower($function_id))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param string $property_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function getReportingLevelForProperty($property_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             if ($custom_level->allowsProperty($property_id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param string $var_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return string|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |     public function getReportingLevelForVariable($var_name) | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         foreach ($this->custom_levels as $custom_level) { | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |             if ($custom_level->allowsVariable($var_name)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |                 return $custom_level->getErrorLevel(); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @return       string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @psalm-return array<string> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public static function getAllIssueTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         return array_filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                  * @param string $file_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                  * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 function ($file_name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                     return substr($file_name, 0, -4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 scandir(dirname(__DIR__) . '/Issue', SCANDIR_SORT_NONE) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |              * @param string $issue_name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |              * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |              * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             function ($issue_name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |                 return !empty($issue_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |                     && $issue_name !== 'MethodIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |                     && $issue_name !== 'PropertyIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                     && $issue_name !== 'FunctionIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |                     && $issue_name !== 'ArgumentIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |                     && $issue_name !== 'VariableIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |                     && $issue_name !== 'ClassIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                     && $issue_name !== 'CodeIssue' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                     && $issue_name !== 'PsalmInternalError' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |                     && $issue_name !== 'ParseError' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |                     && $issue_name !== 'PluginIssue'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.