These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Sylius package. |
||
5 | * |
||
6 | * (c) Paweł Jędrzejewski |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Bundle\ResourceBundle\Controller; |
||
15 | |||
16 | use Sylius\Component\Resource\Metadata\MetadataInterface; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | |||
19 | final class RequestConfigurationFactory implements RequestConfigurationFactoryInterface |
||
20 | { |
||
21 | private const API_VERSION_HEADER = 'Accept'; |
||
22 | private const API_GROUPS_HEADER = 'Accept'; |
||
23 | |||
24 | private const API_VERSION_REGEXP = '/(v|version)=(?P<version>[0-9\.]+)/i'; |
||
25 | private const API_GROUPS_REGEXP = '/(g|groups)=(?P<groups>[a-z,_\s]+)/i'; |
||
26 | |||
27 | /** |
||
28 | * @var ParametersParserInterface |
||
29 | */ |
||
30 | private $parametersParser; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $configurationClass; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | private $defaultParameters; |
||
41 | |||
42 | /** |
||
43 | * @param ParametersParserInterface $parametersParser |
||
44 | * @param string $configurationClass |
||
45 | * @param array $defaultParameters |
||
46 | */ |
||
47 | public function __construct(ParametersParserInterface $parametersParser, string $configurationClass, array $defaultParameters = []) |
||
48 | { |
||
49 | $this->parametersParser = $parametersParser; |
||
50 | $this->configurationClass = $configurationClass; |
||
51 | $this->defaultParameters = $defaultParameters; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function create(MetadataInterface $metadata, Request $request): RequestConfiguration |
||
58 | { |
||
59 | $parameters = array_merge($this->defaultParameters, $this->parseApiParameters($request)); |
||
60 | $parameters = $this->parametersParser->parseRequestValues($parameters, $request); |
||
61 | |||
62 | return new $this->configurationClass($metadata, $request, new Parameters($parameters)); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param Request $request |
||
67 | * |
||
68 | * @return array |
||
69 | * |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | private function parseApiParameters(Request $request): array |
||
73 | { |
||
74 | $parameters = []; |
||
75 | |||
76 | $apiVersionHeaders = $request->headers->get(self::API_VERSION_HEADER, null, false); |
||
77 | foreach ($apiVersionHeaders as $apiVersionHeader) { |
||
0 ignored issues
–
show
|
|||
78 | if (preg_match(self::API_VERSION_REGEXP, $apiVersionHeader, $matches)) { |
||
79 | $parameters['serialization_version'] = $matches['version']; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $apiGroupsHeaders = $request->headers->get(self::API_GROUPS_HEADER, null, false); |
||
84 | foreach ($apiGroupsHeaders as $apiGroupsHeader) { |
||
0 ignored issues
–
show
The expression
$apiGroupsHeaders of type string|array<integer,string> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
85 | if (preg_match(self::API_GROUPS_REGEXP, $apiGroupsHeader, $matches)) { |
||
86 | $parameters['serialization_groups'] = array_map('trim', explode(',', $matches['groups'])); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return array_merge($request->attributes->get('_sylius', []), $parameters); |
||
91 | } |
||
92 | } |
||
93 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.