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

testRendererControllerReferenceWithCompoundMatcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 54

Duplication

Lines 9
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 9
loc 54
rs 9.0036
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

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\Tests\Fragment;
8
9
use eZ\Bundle\EzPublishCoreBundle\Fragment\InlineFragmentRenderer;
10
use eZ\Publish\Core\MVC\Symfony\Component\Serializer\SerializerTrait;
11
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Controller\ControllerReference;
14
15
class InlineFragmentRendererTest extends DecoratedFragmentRendererTest
16
{
17
    use SerializerTrait;
18
19
    public function testRendererControllerReference()
20
    {
21
        $reference = new ControllerReference('FooBundle:bar:baz');
22
        $matcher = new SiteAccess\Matcher\HostElement(1);
23
        $siteAccess = new SiteAccess(
24
            'test',
25
            'test',
26
            $matcher
27
        );
28
        $request = new Request();
29
        $request->attributes->set('siteaccess', $siteAccess);
30
        $request->attributes->set('semanticPathinfo', '/foo/bar');
31
        $request->attributes->set('viewParametersString', '/(foo)/bar');
32
        $options = ['foo' => 'bar'];
33
        $expectedReturn = '/_fragment?foo=bar';
34
        $this->innerRenderer
35
            ->expects($this->once())
36
            ->method('render')
37
            ->with($reference, $request, $options)
38
            ->will($this->returnValue($expectedReturn));
39
40
        $renderer = new InlineFragmentRenderer($this->innerRenderer);
41
        $this->assertSame($expectedReturn, $renderer->render($reference, $request, $options));
42
        $this->assertTrue(isset($reference->attributes['serialized_siteaccess']));
43
        $serializedSiteAccess = json_encode($siteAccess);
44
        $this->assertSame($serializedSiteAccess, $reference->attributes['serialized_siteaccess']);
45
        $this->assertTrue(isset($reference->attributes['serialized_siteaccess_matcher']));
46
        $this->assertSame(
47
            $this->getSerializer()->serialize(
48
                $siteAccess->matcher,
49
                'json'
50
            ),
51
            $reference->attributes['serialized_siteaccess_matcher']
52
        );
53
        $this->assertTrue(isset($reference->attributes['semanticPathinfo']));
54
        $this->assertSame('/foo/bar', $reference->attributes['semanticPathinfo']);
55
        $this->assertTrue(isset($reference->attributes['viewParametersString']));
56
        $this->assertSame('/(foo)/bar', $reference->attributes['viewParametersString']);
57
    }
58
59
    public function testRendererControllerReferenceWithCompoundMatcher()
60
    {
61
        $reference = new ControllerReference('FooBundle:bar:baz');
62
        $compoundMatcher = new SiteAccess\Matcher\Compound\LogicalAnd([]);
63
        $subMatchers = [
64
            'Map\URI' => new SiteAccess\Matcher\Map\URI([]),
65
            'Map\Host' => new SiteAccess\Matcher\Map\Host([]),
66
        ];
67
        $compoundMatcher->setSubMatchers($subMatchers);
68
        $siteAccess = new SiteAccess(
69
            'test',
70
            'test',
71
            $compoundMatcher
72
        );
73
        $request = new Request();
74
        $request->attributes->set('siteaccess', $siteAccess);
75
        $request->attributes->set('semanticPathinfo', '/foo/bar');
76
        $request->attributes->set('viewParametersString', '/(foo)/bar');
77
        $options = ['foo' => 'bar'];
78
        $expectedReturn = '/_fragment?foo=bar';
79
        $this->innerRenderer
80
            ->expects($this->once())
81
            ->method('render')
82
            ->with($reference, $request, $options)
83
            ->will($this->returnValue($expectedReturn));
84
85
        $renderer = new InlineFragmentRenderer($this->innerRenderer);
86
        $this->assertSame($expectedReturn, $renderer->render($reference, $request, $options));
87
        $this->assertTrue(isset($reference->attributes['serialized_siteaccess']));
88
        $serializedSiteAccess = json_encode($siteAccess);
89
        $this->assertSame($serializedSiteAccess, $reference->attributes['serialized_siteaccess']);
90
        $this->assertTrue(isset($reference->attributes['serialized_siteaccess_matcher']));
91
        $this->assertSame(
92
            $this->getSerializer()->serialize(
93
                $siteAccess->matcher,
94
                'json'
95
            ),
96
            $reference->attributes['serialized_siteaccess_matcher']
97
        );
98
        $this->assertTrue(isset($reference->attributes['serialized_siteaccess_sub_matchers']));
99 View Code Duplication
        foreach ($siteAccess->matcher->getSubMatchers() as $subMatcher) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher as the method getSubMatchers() does only exist in the following implementations of said interface: eZ\Publish\Core\MVC\Symf...Access\Matcher\Compound, eZ\Publish\Core\MVC\Symf...her\Compound\LogicalAnd, eZ\Publish\Core\MVC\Symf...cher\Compound\LogicalOr.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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...
100
            $this->assertSame(
101
                $this->getSerializer()->serialize(
102
                    $subMatcher,
103
                    'json'
104
                ),
105
                $reference->attributes['serialized_siteaccess_sub_matchers'][get_class($subMatcher)]
106
            );
107
        }
108
        $this->assertTrue(isset($reference->attributes['semanticPathinfo']));
109
        $this->assertSame('/foo/bar', $reference->attributes['semanticPathinfo']);
110
        $this->assertTrue(isset($reference->attributes['viewParametersString']));
111
        $this->assertSame('/(foo)/bar', $reference->attributes['viewParametersString']);
112
    }
113
}
114