ExtensionsCommandTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 11.48 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 4
dl 7
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTestCommand() 0 4 1
A testConfigure() 0 4 1
A testGetRows() 0 4 1
A rowsProvider() 0 13 1
A testExecute() 7 7 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 SilverLeague\Console\Tests\Command\Object;
4
5
use SilverLeague\Console\Tests\Command\AbstractCommandTest;
6
use SilverStripe\Assets\AssetControlExtension;
7
use SilverStripe\Security\Member;
8
9
/**
10
 * @coversDefaultClass \SilverLeague\Console\Command\Object\ExtensionsCommand
11
 * @package silverstripe-console
12
 * @author  Robbie Averill <[email protected]>
13
 */
14
class ExtensionsCommandTest extends AbstractCommandTest
15
{
16
    protected function getTestCommand()
17
    {
18
        return 'object:extensions';
19
    }
20
21
    /**
22
     * Ensure that the Injector's class resolution is returned for a given Object
23
     *
24
     * @covers ::execute
25
     */
26 View Code Duplication
    public function testExecute()
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...
27
    {
28
        $tester = $this->executeTest(['object' => Member::class]);
29
        $output = $tester->getDisplay();
30
        $this->assertContains(AssetControlExtension::class, $output);
31
        $this->assertContains('silverstripe/assets', $output);
32
    }
33
34
    /**
35
     * Ensure that the InputArgument for the object is added
36
     *
37
     * @covers ::configure
38
     */
39
    public function testConfigure()
40
    {
41
        $this->assertTrue($this->command->getDefinition()->hasArgument('object'));
42
    }
43
44
    /**
45
     * Ensure that extra headers are added for CMS pages
46
     *
47
     * @covers ::getRows
48
     * @dataProvider rowsProvider
49
     *
50
     * @param array    $extensions
51
     * @param string[] $expected
52
     */
53
    public function testGetRows($extensions, $expected)
54
    {
55
        $this->assertSame($expected, $this->command->getRows($extensions));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SilverLeague\Console\Command\SilverStripeCommand as the method getRows() does only exist in the following sub-classes of SilverLeague\Console\Command\SilverStripeCommand: SilverLeague\Console\Com...bject\ExtensionsCommand. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
    }
57
58
    /**
59
     * @return array[]
60
     */
61
    public function rowsProvider()
62
    {
63
        $extensions = [
64
            AssetControlExtension::class,
65
        ];
66
67
        return [
68
            [
69
                $extensions,
70
                [array_merge($extensions, ['silverstripe/assets', 0])],
71
            ],
72
        ];
73
    }
74
}
75