Completed
Push — EZP-31644 ( d0f8f8...1e4423 )
by
unknown
16:33
created

SiteAccessSerializationTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A serializeSiteAccess() 0 19 3
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Bundle\EzPublishCoreBundle\Fragment;
10
11
use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
15
trait SiteAccessSerializationTrait
16
{
17
    use SerializerTrait;
18
19
    public function serializeSiteAccess(SiteAccess $siteAccess, ControllerReference $uri): void
20
    {
21
        // Serialize the siteaccess to get it back after.
22
        // @see eZ\Publish\Core\MVC\Symfony\EventListener\SiteAccessMatchListener
23
        $uri->attributes['serialized_siteaccess'] = json_encode($siteAccess);
24
        $uri->attributes['serialized_siteaccess_matcher'] = $this->getSerializer()->serialize(
25
            $siteAccess->matcher,
26
            'json'
27
        );
28
        if ($siteAccess->matcher instanceof SiteAccess\Matcher\CompoundInterface) {
29
            $subMatchers = $siteAccess->matcher->getSubMatchers() ?? null;
30
            foreach ($subMatchers as $subMatcher) {
0 ignored issues
show
Bug introduced by
The expression $subMatchers of type array<integer,object<eZ\...teAccess\Matcher>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
31
                $uri->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)] = $this->getSerializer()->serialize(
32
                    $subMatcher,
33
                    'json'
34
                );
35
            }
36
        }
37
    }
38
}
39