Completed
Push — EZP-31644 ( 47e85b )
by
unknown
17:57
created

InlineFragmentRenderer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 28.13 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 18
loc 64
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setFragmentPath() 0 6 2
A setSiteAccess() 0 4 1
B render() 18 31 7
A getName() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
namespace eZ\Bundle\EzPublishCoreBundle\Fragment;
8
9
use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
10
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware;
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
15
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer as BaseRenderer;
16
use Symfony\Component\HttpKernel\Fragment\RoutableFragmentRenderer;
17
18
class InlineFragmentRenderer extends BaseRenderer implements SiteAccessAware
19
{
20
    use SerializerTrait;
21
22
    /** @var \Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface */
23
    private $innerRenderer;
24
25
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess */
26
    private $siteAccess;
27
28
    public function __construct(FragmentRendererInterface $innerRenderer)
29
    {
30
        $this->innerRenderer = $innerRenderer;
31
    }
32
33
    public function setFragmentPath($path)
34
    {
35
        if ($this->innerRenderer instanceof RoutableFragmentRenderer) {
36
            $this->innerRenderer->setFragmentPath($path);
37
        }
38
    }
39
40
    public function setSiteAccess(SiteAccess $siteAccess = null)
41
    {
42
        $this->siteAccess = $siteAccess;
43
    }
44
45
    public function render($uri, Request $request, array $options = [])
46
    {
47
        if ($uri instanceof ControllerReference) {
48 View Code Duplication
            if ($request->attributes->has('siteaccess')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
                /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess $siteAccess */
50
                $siteAccess = $request->attributes->get('siteaccess');
51
                $uri->attributes['serialized_siteaccess'] = json_encode($siteAccess);
52
                $uri->attributes['serialized_siteaccess_matcher'] = $this->getSerializer()->serialize(
53
                    $siteAccess->matcher,
54
                    'json'
55
                );
56
                if ($siteAccess->matcher instanceof SiteAccess\Matcher\CompoundInterface) {
57
                    $subMatchers = $siteAccess->matcher->getSubMatchers() ?? null;
58
                    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...
59
                        $uri->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)] = $this->getSerializer()->serialize(
60
                            $subMatcher,
61
                            'json'
62
                        );
63
                    }
64
                }
65
            }
66
            if ($request->attributes->has('semanticPathinfo')) {
67
                $uri->attributes['semanticPathinfo'] = $request->attributes->get('semanticPathinfo');
68
            }
69
            if ($request->attributes->has('viewParametersString')) {
70
                $uri->attributes['viewParametersString'] = $request->attributes->get('viewParametersString');
71
            }
72
        }
73
74
        return $this->innerRenderer->render($uri, $request, $options);
75
    }
76
77
    public function getName()
78
    {
79
        return $this->innerRenderer->getName();
80
    }
81
}
82