Conditions | 16 |
Paths | 13 |
Total Lines | 147 |
Code Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Tests | 33 |
CRAP Score | 16.0476 |
Changes | 7 | ||
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 |
||
4517 | 339 | public static function normalize_encoding($encoding, $fallback = '') |
|
4518 | { |
||
4519 | /** |
||
4520 | * @psalm-suppress ImpureStaticVariable |
||
4521 | * |
||
4522 | * @var array<string,string> |
||
4523 | */ |
||
4524 | 339 | static $STATIC_NORMALIZE_ENCODING_CACHE = []; |
|
4525 | |||
4526 | // init |
||
4527 | 339 | $encoding = (string) $encoding; |
|
4528 | |||
4529 | 339 | if (!$encoding) { |
|
4530 | 290 | return $fallback; |
|
4531 | } |
||
4532 | |||
4533 | if ( |
||
4534 | 53 | $encoding === 'UTF-8' |
|
4535 | || |
||
4536 | 53 | $encoding === 'UTF8' |
|
4537 | ) { |
||
4538 | 29 | return 'UTF-8'; |
|
4539 | } |
||
4540 | |||
4541 | if ( |
||
4542 | 44 | $encoding === '8BIT' |
|
4543 | || |
||
4544 | 44 | $encoding === 'BINARY' |
|
4545 | ) { |
||
4546 | return 'CP850'; |
||
4547 | } |
||
4548 | |||
4549 | if ( |
||
4550 | 44 | $encoding === 'HTML' |
|
4551 | || |
||
4552 | 44 | $encoding === 'HTML-ENTITIES' |
|
4553 | ) { |
||
4554 | 2 | return 'HTML-ENTITIES'; |
|
4555 | } |
||
4556 | |||
4557 | if ( |
||
4558 | 44 | $encoding === 'ISO' |
|
4559 | || |
||
4560 | 44 | $encoding === 'ISO-8859-1' |
|
4561 | ) { |
||
4562 | 39 | return 'ISO-8859-1'; |
|
4563 | } |
||
4564 | |||
4565 | if ( |
||
4566 | 11 | $encoding === '1' // only a fallback, for non "strict_types" usage ... |
|
4567 | || |
||
4568 | 11 | $encoding === '0' // only a fallback, for non "strict_types" usage ... |
|
4569 | ) { |
||
4570 | return $fallback; |
||
4571 | } |
||
4572 | |||
4573 | 11 | if (isset($STATIC_NORMALIZE_ENCODING_CACHE[$encoding])) { |
|
4574 | 8 | return $STATIC_NORMALIZE_ENCODING_CACHE[$encoding]; |
|
4575 | } |
||
4576 | |||
4577 | 5 | if (self::$ENCODINGS === null) { |
|
4578 | 1 | self::$ENCODINGS = self::getData('encodings'); |
|
4579 | } |
||
4580 | |||
4581 | 5 | if (\in_array($encoding, self::$ENCODINGS, true)) { |
|
4582 | 3 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding] = $encoding; |
|
4583 | |||
4584 | 3 | return $encoding; |
|
4585 | } |
||
4586 | |||
4587 | 4 | $encoding_original = $encoding; |
|
4588 | 4 | $encoding = \strtoupper($encoding); |
|
4589 | 4 | $encoding_upper_helper = (string) \preg_replace('/[^a-zA-Z0-9]/u', '', $encoding); |
|
4590 | |||
4591 | 4 | $equivalences = [ |
|
4592 | 'ISO8859' => 'ISO-8859-1', |
||
4593 | 'ISO88591' => 'ISO-8859-1', |
||
4594 | 'ISO' => 'ISO-8859-1', |
||
4595 | 'LATIN' => 'ISO-8859-1', |
||
4596 | 'LATIN1' => 'ISO-8859-1', // Western European |
||
4597 | 'ISO88592' => 'ISO-8859-2', |
||
4598 | 'LATIN2' => 'ISO-8859-2', // Central European |
||
4599 | 'ISO88593' => 'ISO-8859-3', |
||
4600 | 'LATIN3' => 'ISO-8859-3', // Southern European |
||
4601 | 'ISO88594' => 'ISO-8859-4', |
||
4602 | 'LATIN4' => 'ISO-8859-4', // Northern European |
||
4603 | 'ISO88595' => 'ISO-8859-5', |
||
4604 | 'ISO88596' => 'ISO-8859-6', // Greek |
||
4605 | 'ISO88597' => 'ISO-8859-7', |
||
4606 | 'ISO88598' => 'ISO-8859-8', // Hebrew |
||
4607 | 'ISO88599' => 'ISO-8859-9', |
||
4608 | 'LATIN5' => 'ISO-8859-9', // Turkish |
||
4609 | 'ISO885911' => 'ISO-8859-11', |
||
4610 | 'TIS620' => 'ISO-8859-11', // Thai |
||
4611 | 'ISO885910' => 'ISO-8859-10', |
||
4612 | 'LATIN6' => 'ISO-8859-10', // Nordic |
||
4613 | 'ISO885913' => 'ISO-8859-13', |
||
4614 | 'LATIN7' => 'ISO-8859-13', // Baltic |
||
4615 | 'ISO885914' => 'ISO-8859-14', |
||
4616 | 'LATIN8' => 'ISO-8859-14', // Celtic |
||
4617 | 'ISO885915' => 'ISO-8859-15', |
||
4618 | 'LATIN9' => 'ISO-8859-15', // Western European (with some extra chars e.g. €) |
||
4619 | 'ISO885916' => 'ISO-8859-16', |
||
4620 | 'LATIN10' => 'ISO-8859-16', // Southeast European |
||
4621 | 'CP1250' => 'WINDOWS-1250', |
||
4622 | 'WIN1250' => 'WINDOWS-1250', |
||
4623 | 'WINDOWS1250' => 'WINDOWS-1250', |
||
4624 | 'CP1251' => 'WINDOWS-1251', |
||
4625 | 'WIN1251' => 'WINDOWS-1251', |
||
4626 | 'WINDOWS1251' => 'WINDOWS-1251', |
||
4627 | 'CP1252' => 'WINDOWS-1252', |
||
4628 | 'WIN1252' => 'WINDOWS-1252', |
||
4629 | 'WINDOWS1252' => 'WINDOWS-1252', |
||
4630 | 'CP1253' => 'WINDOWS-1253', |
||
4631 | 'WIN1253' => 'WINDOWS-1253', |
||
4632 | 'WINDOWS1253' => 'WINDOWS-1253', |
||
4633 | 'CP1254' => 'WINDOWS-1254', |
||
4634 | 'WIN1254' => 'WINDOWS-1254', |
||
4635 | 'WINDOWS1254' => 'WINDOWS-1254', |
||
4636 | 'CP1255' => 'WINDOWS-1255', |
||
4637 | 'WIN1255' => 'WINDOWS-1255', |
||
4638 | 'WINDOWS1255' => 'WINDOWS-1255', |
||
4639 | 'CP1256' => 'WINDOWS-1256', |
||
4640 | 'WIN1256' => 'WINDOWS-1256', |
||
4641 | 'WINDOWS1256' => 'WINDOWS-1256', |
||
4642 | 'CP1257' => 'WINDOWS-1257', |
||
4643 | 'WIN1257' => 'WINDOWS-1257', |
||
4644 | 'WINDOWS1257' => 'WINDOWS-1257', |
||
4645 | 'CP1258' => 'WINDOWS-1258', |
||
4646 | 'WIN1258' => 'WINDOWS-1258', |
||
4647 | 'WINDOWS1258' => 'WINDOWS-1258', |
||
4648 | 'UTF16' => 'UTF-16', |
||
4649 | 'UTF32' => 'UTF-32', |
||
4650 | 'UTF8' => 'UTF-8', |
||
4651 | 'UTF' => 'UTF-8', |
||
4652 | 'UTF7' => 'UTF-7', |
||
4653 | '8BIT' => 'CP850', |
||
4654 | 'BINARY' => 'CP850', |
||
4655 | ]; |
||
4656 | |||
4657 | 4 | if (!empty($equivalences[$encoding_upper_helper])) { |
|
4658 | 3 | $encoding = $equivalences[$encoding_upper_helper]; |
|
4659 | } |
||
4660 | |||
4661 | 4 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding_original] = $encoding; |
|
4662 | |||
4663 | 4 | return $encoding; |
|
4664 | } |
||
13722 |