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 |
||
4471 | 339 | public static function normalize_encoding($encoding, $fallback = '') |
|
4472 | { |
||
4473 | /** |
||
4474 | * @psalm-suppress ImpureStaticVariable |
||
4475 | * |
||
4476 | * @var array<string,string> |
||
4477 | */ |
||
4478 | 339 | static $STATIC_NORMALIZE_ENCODING_CACHE = []; |
|
4479 | |||
4480 | // init |
||
4481 | 339 | $encoding = (string) $encoding; |
|
4482 | |||
4483 | 339 | if (!$encoding) { |
|
4484 | 290 | return $fallback; |
|
4485 | } |
||
4486 | |||
4487 | if ( |
||
4488 | 53 | $encoding === 'UTF-8' |
|
4489 | || |
||
4490 | 53 | $encoding === 'UTF8' |
|
4491 | ) { |
||
4492 | 29 | return 'UTF-8'; |
|
4493 | } |
||
4494 | |||
4495 | if ( |
||
4496 | 44 | $encoding === '8BIT' |
|
4497 | || |
||
4498 | 44 | $encoding === 'BINARY' |
|
4499 | ) { |
||
4500 | return 'CP850'; |
||
4501 | } |
||
4502 | |||
4503 | if ( |
||
4504 | 44 | $encoding === 'HTML' |
|
4505 | || |
||
4506 | 44 | $encoding === 'HTML-ENTITIES' |
|
4507 | ) { |
||
4508 | 2 | return 'HTML-ENTITIES'; |
|
4509 | } |
||
4510 | |||
4511 | if ( |
||
4512 | 44 | $encoding === 'ISO' |
|
4513 | || |
||
4514 | 44 | $encoding === 'ISO-8859-1' |
|
4515 | ) { |
||
4516 | 41 | return 'ISO-8859-1'; |
|
4517 | } |
||
4518 | |||
4519 | if ( |
||
4520 | 11 | $encoding === '1' // only a fallback, for non "strict_types" usage ... |
|
4521 | || |
||
4522 | 11 | $encoding === '0' // only a fallback, for non "strict_types" usage ... |
|
4523 | ) { |
||
4524 | return $fallback; |
||
4525 | } |
||
4526 | |||
4527 | 11 | if (isset($STATIC_NORMALIZE_ENCODING_CACHE[$encoding])) { |
|
4528 | 8 | return $STATIC_NORMALIZE_ENCODING_CACHE[$encoding]; |
|
4529 | } |
||
4530 | |||
4531 | 5 | if (self::$ENCODINGS === null) { |
|
4532 | 1 | self::$ENCODINGS = self::getData('encodings'); |
|
4533 | } |
||
4534 | |||
4535 | 5 | if (\in_array($encoding, self::$ENCODINGS, true)) { |
|
4536 | 3 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding] = $encoding; |
|
4537 | |||
4538 | 3 | return $encoding; |
|
4539 | } |
||
4540 | |||
4541 | 4 | $encoding_original = $encoding; |
|
4542 | 4 | $encoding = \strtoupper($encoding); |
|
4543 | 4 | $encoding_upper_helper = (string) \preg_replace('/[^a-zA-Z0-9]/u', '', $encoding); |
|
4544 | |||
4545 | $equivalences = [ |
||
4546 | 4 | 'ISO8859' => 'ISO-8859-1', |
|
4547 | 'ISO88591' => 'ISO-8859-1', |
||
4548 | 'ISO' => 'ISO-8859-1', |
||
4549 | 'LATIN' => 'ISO-8859-1', |
||
4550 | 'LATIN1' => 'ISO-8859-1', // Western European |
||
4551 | 'ISO88592' => 'ISO-8859-2', |
||
4552 | 'LATIN2' => 'ISO-8859-2', // Central European |
||
4553 | 'ISO88593' => 'ISO-8859-3', |
||
4554 | 'LATIN3' => 'ISO-8859-3', // Southern European |
||
4555 | 'ISO88594' => 'ISO-8859-4', |
||
4556 | 'LATIN4' => 'ISO-8859-4', // Northern European |
||
4557 | 'ISO88595' => 'ISO-8859-5', |
||
4558 | 'ISO88596' => 'ISO-8859-6', // Greek |
||
4559 | 'ISO88597' => 'ISO-8859-7', |
||
4560 | 'ISO88598' => 'ISO-8859-8', // Hebrew |
||
4561 | 'ISO88599' => 'ISO-8859-9', |
||
4562 | 'LATIN5' => 'ISO-8859-9', // Turkish |
||
4563 | 'ISO885911' => 'ISO-8859-11', |
||
4564 | 'TIS620' => 'ISO-8859-11', // Thai |
||
4565 | 'ISO885910' => 'ISO-8859-10', |
||
4566 | 'LATIN6' => 'ISO-8859-10', // Nordic |
||
4567 | 'ISO885913' => 'ISO-8859-13', |
||
4568 | 'LATIN7' => 'ISO-8859-13', // Baltic |
||
4569 | 'ISO885914' => 'ISO-8859-14', |
||
4570 | 'LATIN8' => 'ISO-8859-14', // Celtic |
||
4571 | 'ISO885915' => 'ISO-8859-15', |
||
4572 | 'LATIN9' => 'ISO-8859-15', // Western European (with some extra chars e.g. €) |
||
4573 | 'ISO885916' => 'ISO-8859-16', |
||
4574 | 'LATIN10' => 'ISO-8859-16', // Southeast European |
||
4575 | 'CP1250' => 'WINDOWS-1250', |
||
4576 | 'WIN1250' => 'WINDOWS-1250', |
||
4577 | 'WINDOWS1250' => 'WINDOWS-1250', |
||
4578 | 'CP1251' => 'WINDOWS-1251', |
||
4579 | 'WIN1251' => 'WINDOWS-1251', |
||
4580 | 'WINDOWS1251' => 'WINDOWS-1251', |
||
4581 | 'CP1252' => 'WINDOWS-1252', |
||
4582 | 'WIN1252' => 'WINDOWS-1252', |
||
4583 | 'WINDOWS1252' => 'WINDOWS-1252', |
||
4584 | 'CP1253' => 'WINDOWS-1253', |
||
4585 | 'WIN1253' => 'WINDOWS-1253', |
||
4586 | 'WINDOWS1253' => 'WINDOWS-1253', |
||
4587 | 'CP1254' => 'WINDOWS-1254', |
||
4588 | 'WIN1254' => 'WINDOWS-1254', |
||
4589 | 'WINDOWS1254' => 'WINDOWS-1254', |
||
4590 | 'CP1255' => 'WINDOWS-1255', |
||
4591 | 'WIN1255' => 'WINDOWS-1255', |
||
4592 | 'WINDOWS1255' => 'WINDOWS-1255', |
||
4593 | 'CP1256' => 'WINDOWS-1256', |
||
4594 | 'WIN1256' => 'WINDOWS-1256', |
||
4595 | 'WINDOWS1256' => 'WINDOWS-1256', |
||
4596 | 'CP1257' => 'WINDOWS-1257', |
||
4597 | 'WIN1257' => 'WINDOWS-1257', |
||
4598 | 'WINDOWS1257' => 'WINDOWS-1257', |
||
4599 | 'CP1258' => 'WINDOWS-1258', |
||
4600 | 'WIN1258' => 'WINDOWS-1258', |
||
4601 | 'WINDOWS1258' => 'WINDOWS-1258', |
||
4602 | 'UTF16' => 'UTF-16', |
||
4603 | 'UTF32' => 'UTF-32', |
||
4604 | 'UTF8' => 'UTF-8', |
||
4605 | 'UTF' => 'UTF-8', |
||
4606 | 'UTF7' => 'UTF-7', |
||
4607 | '8BIT' => 'CP850', |
||
4608 | 'BINARY' => 'CP850', |
||
4609 | ]; |
||
4610 | |||
4611 | 4 | if (!empty($equivalences[$encoding_upper_helper])) { |
|
4612 | 3 | $encoding = $equivalences[$encoding_upper_helper]; |
|
4613 | } |
||
4614 | |||
4615 | 4 | $STATIC_NORMALIZE_ENCODING_CACHE[$encoding_original] = $encoding; |
|
4616 | |||
4617 | 4 | return $encoding; |
|
4618 | } |
||
13694 |