StringTrimmingMiddleware::trimArray()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Middleware;
15
16
use Closure;
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
use function array_walk_recursive;
23
use function function_exists;
24
use function is_array;
25
use function is_string;
26
use function mb_trim;
27
use function trim;
28
29
/**
30
 * @since 3.0.0
31
 */
32
final class StringTrimmingMiddleware implements MiddlewareInterface
33
{
34
    /**
35
     * @var Closure(string): string
36
     */
37
    private readonly Closure $trimmer;
38
39
    /**
40
     * @param (Closure(string): string)|null $trimmer
0 ignored issues
show
Documentation Bug introduced by
The doc comment (Closure(string): string)|null at position 1 could not be parsed: Expected ')' at position 1, but found 'Closure'.
Loading history...
41
     */
42 4
    public function __construct(?Closure $trimmer = null)
43
    {
44 4
        $this->trimmer = $trimmer ?? (function_exists('mb_trim') ? mb_trim(...) : trim(...));
0 ignored issues
show
Bug introduced by
The property trimmer is declared read-only in Sunrise\Http\Router\Midd...tringTrimmingMiddleware.
Loading history...
Bug introduced by
The type trim was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type mb_trim was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 4
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
51
    {
52 4
        $queryParams = $request->getQueryParams();
53 4
        if ($queryParams !== []) {
54 2
            $request = $request->withQueryParams(
55 2
                self::trimArray($queryParams, $this->trimmer)
56 2
            );
57
        }
58
59 4
        $parsedBody = $request->getParsedBody();
60 4
        if ($parsedBody !== [] && is_array($parsedBody)) {
61 2
            $request = $request->withParsedBody(
62 2
                self::trimArray($parsedBody, $this->trimmer)
63 2
            );
64
        }
65
66 4
        return $handler->handle($request);
67
    }
68
69
    /**
70
     * @param array<array-key, mixed> $array
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
71
     * @param Closure(string): string $trimmer
72
     *
73
     * @return array<array-key, mixed>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
74
     */
75 2
    private static function trimArray(array $array, Closure $trimmer): array
76
    {
77 2
        $walker = static function (mixed &$value) use ($trimmer): void {
78 2
            if (is_string($value)) {
79 2
                $value = $trimmer($value);
80
            }
81 2
        };
82
83 2
        array_walk_recursive($array, $walker);
84
85 2
        return $array;
86
    }
87
}
88