Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 109 |
|
0.00% |
0 / 31 |
CRAP | |
0.00% |
0 / 1 |
| ConfigBuilder | |
0.00% |
0 / 109 |
|
0.00% |
0 / 31 |
1482 | |
0.00% |
0 / 1 |
| setRawOptions | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| make | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setFileList | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addFiles | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| setExcludedFileList | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| excludeFiles | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| setExcludeFileRegex | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setDirectoryList | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| addDirectories | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| setExcludedDirectoryList | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| excludeDirectories | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| setMinimumSeverity | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| allowMissingProperties | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| allowScalarImplicitCasts | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| allowNullCastsAsAnyType | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| enableDeadCodeDetection | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| shouldDeadCodeDetectionPreferFalseNegatives | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setProgressBarMode | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
| setSuppressedIssuesList | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| suppressIssueTypes | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| addGlobalsWithTypes | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| readClassAliases | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| enableRedundantConditionDetection | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setMinimumPHPVersion | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setTargetPHPVersion | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| addExceptionClassesWithOptionalThrowsPHPDoc | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| addPlugins | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| addCustomPlugins | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| enableTaintCheck | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
| getTaintCheckPluginName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| filterDirs | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | /** @noinspection PhpUnused */ |
| 4 | |
| 5 | namespace MediaWikiPhanConfig; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | |
| 9 | class ConfigBuilder { |
| 10 | public const PROGRESS_BAR_DISABLED = 0; |
| 11 | public const PROGRESS_BAR_STANDARD = 1; |
| 12 | public const PROGRESS_BAR_LONG = 2; |
| 13 | |
| 14 | public const SET_WITHOUT_FILTER = 0; |
| 15 | /** |
| 16 | * @internal For use in this repo only |
| 17 | * This is used so that the default config can include commonly-used dir |
| 18 | * names without phan complaining about "directory not found". |
| 19 | */ |
| 20 | public const SET_WITH_DIR_FILTER = 1; |
| 21 | |
| 22 | /** @var array */ |
| 23 | private array $options = []; |
| 24 | |
| 25 | /** |
| 26 | * Sets an array of raw phan options. This should generally be avoided, in favour of the setters below. |
| 27 | * @param array $options |
| 28 | * @return self |
| 29 | */ |
| 30 | public function setRawOptions( array $options ): self { |
| 31 | $this->options = array_replace( $this->options, $options ); |
| 32 | return $this; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @return array |
| 37 | */ |
| 38 | public function make(): array { |
| 39 | return $this->options; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param array $list |
| 44 | * @return $this |
| 45 | */ |
| 46 | public function setFileList( array $list ): self { |
| 47 | $this->options['file_list'] = $list; |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param string ...$files |
| 53 | * @return $this |
| 54 | */ |
| 55 | public function addFiles( string ...$files ): self { |
| 56 | $this->options['file_list'] = array_merge( |
| 57 | $this->options['file_list'] ?? [], |
| 58 | $files |
| 59 | ); |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param array $list |
| 65 | * @return $this |
| 66 | */ |
| 67 | public function setExcludedFileList( array $list ): self { |
| 68 | $this->options['exclude_file_list'] = $list; |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param string ...$files |
| 74 | * @return $this |
| 75 | */ |
| 76 | public function excludeFiles( string ...$files ): self { |
| 77 | $this->options['exclude_file_list'] = array_merge( |
| 78 | $this->options['exclude_file_list'] ?? [], |
| 79 | $files |
| 80 | ); |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param string $regex |
| 86 | * @return $this |
| 87 | */ |
| 88 | public function setExcludeFileRegex( string $regex ): self { |
| 89 | $this->options['exclude_file_regex'] = $regex; |
| 90 | return $this; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param array $list |
| 95 | * @param int $filterOption For internal use only, self::SET_WITHOUT_FILTER or self::SET_WITH_DIR_FILTER |
| 96 | * @return $this |
| 97 | */ |
| 98 | public function setDirectoryList( array $list, int $filterOption = self::SET_WITHOUT_FILTER ): self { |
| 99 | if ( $filterOption === self::SET_WITH_DIR_FILTER ) { |
| 100 | $list = $this->filterDirs( $list ); |
| 101 | } |
| 102 | $this->options['directory_list'] = $list; |
| 103 | return $this; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param string ...$dirs |
| 108 | * @return $this |
| 109 | */ |
| 110 | public function addDirectories( string ...$dirs ): self { |
| 111 | $this->options['directory_list'] = array_merge( |
| 112 | $this->options['directory_list'] ?? [], |
| 113 | $dirs |
| 114 | ); |
| 115 | return $this; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @param array $list |
| 120 | * @return $this |
| 121 | */ |
| 122 | public function setExcludedDirectoryList( array $list ): self { |
| 123 | $this->options['exclude_analysis_directory_list'] = $list; |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param string ...$dirs |
| 129 | * @return $this |
| 130 | */ |
| 131 | public function excludeDirectories( string ...$dirs ): self { |
| 132 | $this->options['exclude_analysis_directory_list'] = array_merge( |
| 133 | $this->options['exclude_analysis_directory_list'] ?? [], |
| 134 | $dirs |
| 135 | ); |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param int $minSev |
| 141 | * @return $this |
| 142 | */ |
| 143 | public function setMinimumSeverity( int $minSev ): self { |
| 144 | $this->options['minimum_severity'] = $minSev; |
| 145 | return $this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param bool $yn |
| 150 | * @return $this |
| 151 | */ |
| 152 | public function allowMissingProperties( bool $yn ): self { |
| 153 | $this->options['allow_missing_properties'] = $yn; |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param bool $yn |
| 159 | * @return $this |
| 160 | */ |
| 161 | public function allowScalarImplicitCasts( bool $yn ): self { |
| 162 | $this->options['scalar_implicit_cast'] = $yn; |
| 163 | return $this; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @param bool $yn |
| 168 | * @return $this |
| 169 | */ |
| 170 | public function allowNullCastsAsAnyType( bool $yn ): self { |
| 171 | $this->options['null_casts_as_any_type'] = $yn; |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @param bool $yn |
| 177 | * @return $this |
| 178 | */ |
| 179 | public function enableDeadCodeDetection( bool $yn ): self { |
| 180 | $this->options['dead_code_detection'] = $yn; |
| 181 | return $this; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @param bool $yn |
| 186 | * @return $this |
| 187 | */ |
| 188 | public function shouldDeadCodeDetectionPreferFalseNegatives( bool $yn ): self { |
| 189 | $this->options['dead_code_detection_prefer_false_negative'] = $yn; |
| 190 | return $this; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param int $mode One of the PROGRESS_BAR_* constants |
| 195 | * @return $this |
| 196 | */ |
| 197 | public function setProgressBarMode( int $mode ): self { |
| 198 | switch ( $mode ) { |
| 199 | case self::PROGRESS_BAR_DISABLED: |
| 200 | $this->options['progress_bar'] = false; |
| 201 | break; |
| 202 | case self::PROGRESS_BAR_STANDARD: |
| 203 | $this->options['progress_bar'] = true; |
| 204 | break; |
| 205 | case self::PROGRESS_BAR_LONG: |
| 206 | $this->options['progress_bar'] = true; |
| 207 | $this->options['long_progress_bar'] = true; |
| 208 | break; |
| 209 | default: |
| 210 | throw new InvalidArgumentException( "Invalid $mode" ); |
| 211 | } |
| 212 | return $this; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @param array $list |
| 217 | * @return $this |
| 218 | */ |
| 219 | public function setSuppressedIssuesList( array $list ): self { |
| 220 | $this->options['suppress_issue_types'] = $list; |
| 221 | return $this; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @param string ...$types |
| 226 | * @return $this |
| 227 | */ |
| 228 | public function suppressIssueTypes( string ...$types ): self { |
| 229 | $this->options['suppress_issue_types'] = array_merge( |
| 230 | $this->options['suppress_issue_types'] ?? [], |
| 231 | $types |
| 232 | ); |
| 233 | return $this; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @param array $globals [ 'global_name' => 'union_type' ] |
| 238 | * @return $this |
| 239 | */ |
| 240 | public function addGlobalsWithTypes( array $globals ): self { |
| 241 | $this->options['globals_type_map'] = array_merge( |
| 242 | $this->options['globals_type_map'] ?? [], |
| 243 | $globals |
| 244 | ); |
| 245 | return $this; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * @param bool $yn |
| 250 | * @return $this |
| 251 | */ |
| 252 | public function readClassAliases( bool $yn ): self { |
| 253 | $this->options['enable_class_alias_support'] = $yn; |
| 254 | return $this; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @param bool $yn |
| 259 | * @return $this |
| 260 | */ |
| 261 | public function enableRedundantConditionDetection( bool $yn ): self { |
| 262 | $this->options['redundant_condition_detection'] = $yn; |
| 263 | return $this; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Set the minimum PHP version that the codebase should support. |
| 268 | * @param string $version |
| 269 | * @return $this |
| 270 | */ |
| 271 | public function setMinimumPHPVersion( string $version ): self { |
| 272 | $this->options['minimum_target_php_version'] = $version; |
| 273 | return $this; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Set the PHP version to be checked against for forward-compatibility warnings. |
| 278 | * @param string $version |
| 279 | * @return $this |
| 280 | */ |
| 281 | public function setTargetPHPVersion( string $version ): self { |
| 282 | $this->options['target_php_version'] = $version; |
| 283 | return $this; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @param array $classes |
| 288 | * @return self |
| 289 | */ |
| 290 | public function addExceptionClassesWithOptionalThrowsPHPDoc( array $classes ): self { |
| 291 | $this->options['exception_classes_with_optional_throws_phpdoc'] = array_merge( |
| 292 | $this->options['exception_classes_with_optional_throws_phpdoc'] ?? [], |
| 293 | $classes |
| 294 | ); |
| 295 | return $this; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Adds one or more built-in plugins. |
| 300 | * @param string[] $plugins |
| 301 | * @return self |
| 302 | */ |
| 303 | public function addPlugins( array $plugins ): self { |
| 304 | $this->options['plugins'] = array_merge( |
| 305 | $this->options['plugins'] ?? [], |
| 306 | $plugins |
| 307 | ); |
| 308 | return $this; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Adds one or more custom plugins. |
| 313 | * |
| 314 | * @param string[] $plugins |
| 315 | * @return self |
| 316 | */ |
| 317 | public function addCustomPlugins( array $plugins ): self { |
| 318 | foreach ( $plugins as $plugin ) { |
| 319 | $this->options['plugins'][] = __DIR__ . "/Plugin/$plugin.php"; |
| 320 | } |
| 321 | return $this; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * @internal |
| 326 | * This should only be used by the config file in this repo. |
| 327 | * |
| 328 | * @param string $vendorPath |
| 329 | * @return $this |
| 330 | */ |
| 331 | public function enableTaintCheck( string $vendorPath ): self { |
| 332 | $taintCheckPluginName = $this->getTaintCheckPluginName(); |
| 333 | $taintCheckPath = __DIR__ . "/../../phan-taint-check-plugin/$taintCheckPluginName.php"; |
| 334 | if ( !file_exists( $taintCheckPath ) ) { |
| 335 | $taintCheckPath = |
| 336 | "$vendorPath/vendor/mediawiki/phan-taint-check-plugin/$taintCheckPluginName.php"; |
| 337 | } |
| 338 | $this->options['plugins'][] = $taintCheckPath; |
| 339 | // Taint-check specific settings. NOTE: don't remove these lines, even if they duplicate some of |
| 340 | // the settings above. taint-check may fail hard if one of these settings goes missing. |
| 341 | $this->options['quick_mode'] = false; |
| 342 | $this->options['suppress_issue_types'] = array_merge( |
| 343 | $this->options['suppress_issue_types'], |
| 344 | [ |
| 345 | // We obviously don't want to report false positives |
| 346 | 'SecurityCheck-LikelyFalsePositive', |
| 347 | // This one still has a lot of false positives |
| 348 | 'SecurityCheck-PHPSerializeInjection', |
| 349 | ] |
| 350 | ); |
| 351 | return $this; |
| 352 | } |
| 353 | |
| 354 | protected function getTaintCheckPluginName(): string { |
| 355 | return 'GenericSecurityCheckPlugin'; |
| 356 | } |
| 357 | |
| 358 | private function filterDirs( array $dirs ): array { |
| 359 | return array_filter( $dirs, file_exists( ... ) ); |
| 360 | } |
| 361 | } |