Completed
Pull Request — master (#47)
by Robbie
01:27
created

ControllerExtensionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 31.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 27
loc 86
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testOnBeforeInit() 0 16 1
A testOnAfterInit() 13 13 1
A testBeforeCallActionHandler() 7 16 1
A testAfterCallActionHandler() 7 15 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
namespace LeKoala\DebugBar\Test\Extension;
4
5
use DebugBar\DataCollector\TimeDataCollector;
6
use LeKoala\DebugBar\DebugBar;
7
use LeKoala\DebugBar\Extension\ControllerExtension;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Dev\FunctionalTest;
11
12
class ControllerExtensionTest extends FunctionalTest
13
{
14
    protected static $required_extensions = [
15
        Controller::class => [ControllerExtension::class]
16
    ];
17
18
    /**
19
     * @var Controller
20
     */
21
    protected $controller;
22
23
    public function setUp()
24
    {
25
        parent::setUp();
26
27
        $this->controller = Controller::curr();
28
        // $this->controller->pushCurrent();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
30
        DebugBar::$bufferingEnabled = false;
31
        DebugBar::initDebugBar();
32
    }
33
34
    public function testOnBeforeInit()
35
    {
36
        $this->controller->extend('onBeforeInit');
37
38
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
39
            $controller = $debugbar->getCollector('silverstripe')->getController();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DebugBar\DataCollector\DataCollectorInterface as the method getController() does only exist in the following implementations of said interface: LeKoala\DebugBar\Collector\SilverStripeCollector.

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...
40
            $this->assertInstanceOf(Controller::class, $controller);
41
            $this->assertSame(Controller::curr(), $controller);
42
43
            $timeData = $debugbar->getCollector('time');
44
            $this->assertInstanceOf(TimeDataCollector::class, $timeData);
45
46
            $this->assertFalse($timeData->hasStartedMeasure('pre_request'));
47
            $this->assertTrue($timeData->hasStartedMeasure('init'));
48
        });
49
    }
50
51 View Code Duplication
    public function testOnAfterInit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
    {
53
        $this->controller->extend('onAfterInit');
54
55
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
56
            $timeData = $debugbar->getCollector('time');
57
            $this->assertInstanceOf(TimeDataCollector::class, $timeData);
58
59
            $this->assertFalse($timeData->hasStartedMeasure('cms_init'));
60
            $this->assertFalse($timeData->hasStartedMeasure('init'));
61
            $this->assertTrue($timeData->hasStartedMeasure('handle'));
62
        });
63
    }
64
65
    public function testBeforeCallActionHandler()
66
    {
67
        $request = new HTTPRequest('GET', '/');
68
        $action = 'someaction';
69
        $this->controller->extend('beforeCallActionHandler', $request, $action);
70
71 View Code Duplication
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
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...
72
            $timeData = $debugbar->getCollector('time');
73
            $this->assertInstanceOf(TimeDataCollector::class, $timeData);
74
75
            $this->assertFalse($timeData->hasStartedMeasure('handle'));
76
            $this->assertTrue($timeData->hasStartedMeasure('action'));
77
        });
78
79
        $this->assertTrue($this->controller->beforeCallActionHandlerCalled);
0 ignored issues
show
Bug introduced by
The property beforeCallActionHandlerCalled does not seem to exist. Did you mean action?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
    }
81
82
    public function testAfterCallActionHandler()
83
    {
84
        $request = new HTTPRequest('GET', '/');
85
        $action = 'someaction';
86
        $result = null;
87
        $this->controller->extend('afterCallActionHandler', $request, $action, $result);
88
89 View Code Duplication
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
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...
90
            $timeData = $debugbar->getCollector('time');
91
            $this->assertInstanceOf(TimeDataCollector::class, $timeData);
92
93
            $this->assertFalse($timeData->hasStartedMeasure('action'));
94
            $this->assertTrue($timeData->hasStartedMeasure('after_action'));
95
        });
96
    }
97
}
98