TaskManagerTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testTaskPriority() 0 15 1
1
<?php
2
3
namespace As3\Bundle\PostProcessBundle\Tests;
4
5
use As3\Bundle\PostProcessBundle\Task\TaskManager;
6
7
class TaskManagerTest extends \PHPUnit_Framework_TestCase
8
{
9
    private $manager;
10
11
    protected function setUp()
12
    {
13
        $this->manager = new TaskManager();
14
    }
15
16
    public function testTaskPriority()
17
    {
18
        $task1 = new TestTask('1');
19
        $task2 = new TestTask('2');
20
        $task3 = new TestTask('3');
21
22
        $this->manager->addTask($task1);
23
        $this->manager->addTask($task2, 5);
24
        $this->manager->addTask($task3, 1);
25
26
        $tasks = $this->manager->getTasks();
27
        $this->assertEquals(3, count($tasks));
28
        $this->assertEquals('2', $tasks[0]->getKey(), 'Task priority order is incorrect.');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface As3\Bundle\PostProcessBundle\Task\TaskInterface as the method getKey() does only exist in the following implementations of said interface: As3\Bundle\PostProcessBundle\Tests\TestTask.

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...
29
        $this->assertEquals('3', $tasks[1]->getKey(), 'Task priority order is incorrect.');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface As3\Bundle\PostProcessBundle\Task\TaskInterface as the method getKey() does only exist in the following implementations of said interface: As3\Bundle\PostProcessBundle\Tests\TestTask.

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...
30
    }
31
}
32