Conditions | 16 |
Paths | 13 |
Total Lines | 147 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 16.0476 |
Changes | 8 | ||
Bugs | 4 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
4466 | 339 | public static function normalize_encoding($encoding, $fallback = '') |
|
4467 | { |
||
4468 | /** |
||
4469 | * @psalm-suppress ImpureStaticVariable |
||
4470 | * |
||
4471 | * @var array<string,string> |
||
4472 | */ |
||
4473 | 339 | static $STATIC_NORMALIZE_ENCODING_CACHE = []; |
|
4474 | |||
4475 | // init |
||
4476 | 339 | $encoding = (string) $encoding; |
|
4477 | |||
4478 | 339 | if (!$encoding) { |
|
4479 | 290 | return $fallback; |
|
4480 | } |
||
4481 | |||
4482 | if ( |
||
4483 | 53 | $encoding === 'UTF-8' |
|
4484 | || |
||
4485 | 53 | $encoding === 'UTF8' |
|
4486 | ) { |
||
4487 | 29 | return 'UTF-8'; |
|
4488 | } |
||
4489 | |||
4490 | if ( |
||
4491 | 44 | $encoding === '8BIT' |
|
4492 | || |
||
4493 | 44 | $encoding === 'BINARY' |
|
4494 | ) { |
||
4495 | return 'CP850'; |
||
4496 | } |
||
4497 | |||
4498 | if ( |
||
4499 | 44 | $encoding === 'HTML' |
|
4500 | || |
||
4501 | 44 | $encoding === 'HTML-ENTITIES' |
|
4502 | ) { |
||
4503 | 2 | return 'HTML-ENTITIES'; |
|
4504 | } |
||
4505 | |||
4506 | if ( |
||
4507 | 44 | $encoding === 'ISO' |
|
4508 | || |
||
4509 | 44 | $encoding === 'ISO-8859-1' |
|
4510 | ) { |
||
4511 | 41 | return 'ISO-8859-1'; |
|
4512 | } |
||
4513 | |||
4514 | if ( |
||
4515 | 11 | $encoding === '1' // only a fallback, for non "strict_types" usage ... |
|
4516 | || |
||
4517 | 11 | $encoding === '0' // only a fallback, for non "strict_types" usage ... |
|
4518 | ) { |
||
4519 | return $fallback; |
||
4520 | } |
||
4521 | |||
4522 | 11 | if (isset($STATIC_NORMALIZE_ENCODING_CACHE[$encoding])) { |
|
4523 | 8 | return $STATIC_NORMALIZE_ENCODING_CACHE[$encoding]; |
|
4524 | } |
||
4525 | |||
4526 | 5 | if (self::$ENCODINGS === null) { |
|
4527 | 1 | self::$ENCODINGS = self::getData('encodings'); |
|
4528 | } |
||
4529 | |||
4530 | 5 | if (\in_array($encoding, self::$ENCODINGS, true)) { |
|
4531 | 3 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding] = $encoding; |
|
4532 | |||
4533 | 3 | return $encoding; |
|
4534 | } |
||
4535 | |||
4536 | 4 | $encoding_original = $encoding; |
|
4537 | 4 | $encoding = \strtoupper($encoding); |
|
4538 | 4 | $encoding_upper_helper = (string) \preg_replace('/[^a-zA-Z0-9]/u', '', $encoding); |
|
4539 | |||
4540 | $equivalences = [ |
||
4541 | 4 | 'ISO8859' => 'ISO-8859-1', |
|
4542 | 'ISO88591' => 'ISO-8859-1', |
||
4543 | 'ISO' => 'ISO-8859-1', |
||
4544 | 'LATIN' => 'ISO-8859-1', |
||
4545 | 'LATIN1' => 'ISO-8859-1', // Western European |
||
4546 | 'ISO88592' => 'ISO-8859-2', |
||
4547 | 'LATIN2' => 'ISO-8859-2', // Central European |
||
4548 | 'ISO88593' => 'ISO-8859-3', |
||
4549 | 'LATIN3' => 'ISO-8859-3', // Southern European |
||
4550 | 'ISO88594' => 'ISO-8859-4', |
||
4551 | 'LATIN4' => 'ISO-8859-4', // Northern European |
||
4552 | 'ISO88595' => 'ISO-8859-5', |
||
4553 | 'ISO88596' => 'ISO-8859-6', // Greek |
||
4554 | 'ISO88597' => 'ISO-8859-7', |
||
4555 | 'ISO88598' => 'ISO-8859-8', // Hebrew |
||
4556 | 'ISO88599' => 'ISO-8859-9', |
||
4557 | 'LATIN5' => 'ISO-8859-9', // Turkish |
||
4558 | 'ISO885911' => 'ISO-8859-11', |
||
4559 | 'TIS620' => 'ISO-8859-11', // Thai |
||
4560 | 'ISO885910' => 'ISO-8859-10', |
||
4561 | 'LATIN6' => 'ISO-8859-10', // Nordic |
||
4562 | 'ISO885913' => 'ISO-8859-13', |
||
4563 | 'LATIN7' => 'ISO-8859-13', // Baltic |
||
4564 | 'ISO885914' => 'ISO-8859-14', |
||
4565 | 'LATIN8' => 'ISO-8859-14', // Celtic |
||
4566 | 'ISO885915' => 'ISO-8859-15', |
||
4567 | 'LATIN9' => 'ISO-8859-15', // Western European (with some extra chars e.g. €) |
||
4568 | 'ISO885916' => 'ISO-8859-16', |
||
4569 | 'LATIN10' => 'ISO-8859-16', // Southeast European |
||
4570 | 'CP1250' => 'WINDOWS-1250', |
||
4571 | 'WIN1250' => 'WINDOWS-1250', |
||
4572 | 'WINDOWS1250' => 'WINDOWS-1250', |
||
4573 | 'CP1251' => 'WINDOWS-1251', |
||
4574 | 'WIN1251' => 'WINDOWS-1251', |
||
4575 | 'WINDOWS1251' => 'WINDOWS-1251', |
||
4576 | 'CP1252' => 'WINDOWS-1252', |
||
4577 | 'WIN1252' => 'WINDOWS-1252', |
||
4578 | 'WINDOWS1252' => 'WINDOWS-1252', |
||
4579 | 'CP1253' => 'WINDOWS-1253', |
||
4580 | 'WIN1253' => 'WINDOWS-1253', |
||
4581 | 'WINDOWS1253' => 'WINDOWS-1253', |
||
4582 | 'CP1254' => 'WINDOWS-1254', |
||
4583 | 'WIN1254' => 'WINDOWS-1254', |
||
4584 | 'WINDOWS1254' => 'WINDOWS-1254', |
||
4585 | 'CP1255' => 'WINDOWS-1255', |
||
4586 | 'WIN1255' => 'WINDOWS-1255', |
||
4587 | 'WINDOWS1255' => 'WINDOWS-1255', |
||
4588 | 'CP1256' => 'WINDOWS-1256', |
||
4589 | 'WIN1256' => 'WINDOWS-1256', |
||
4590 | 'WINDOWS1256' => 'WINDOWS-1256', |
||
4591 | 'CP1257' => 'WINDOWS-1257', |
||
4592 | 'WIN1257' => 'WINDOWS-1257', |
||
4593 | 'WINDOWS1257' => 'WINDOWS-1257', |
||
4594 | 'CP1258' => 'WINDOWS-1258', |
||
4595 | 'WIN1258' => 'WINDOWS-1258', |
||
4596 | 'WINDOWS1258' => 'WINDOWS-1258', |
||
4597 | 'UTF16' => 'UTF-16', |
||
4598 | 'UTF32' => 'UTF-32', |
||
4599 | 'UTF8' => 'UTF-8', |
||
4600 | 'UTF' => 'UTF-8', |
||
4601 | 'UTF7' => 'UTF-7', |
||
4602 | '8BIT' => 'CP850', |
||
4603 | 'BINARY' => 'CP850', |
||
4604 | ]; |
||
4605 | |||
4606 | 4 | if (!empty($equivalences[$encoding_upper_helper])) { |
|
4607 | 3 | $encoding = $equivalences[$encoding_upper_helper]; |
|
4608 | } |
||
4609 | |||
4610 | 4 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding_original] = $encoding; |
|
4611 | |||
4612 | 4 | return $encoding; |
|
4613 | } |
||
13689 |