Completed
Push — master ( 632b02...a06c21 )
by Mario
06:25
created

ProjectUsersService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 13.6 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 11
dl 17
loc 125
ccs 0
cts 54
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createProjectUser() 0 16 1
A updateProjectUser() 17 17 1
A deleteProjectUser() 0 10 1
A createMultipleProjectUsers() 0 23 2
A updateMultipleProjectUsers() 0 22 2
A deleteMultipleProjectUsers() 0 10 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 Marek\Toggable\Service\ProjectUsers;
4
5
use Marek\Toggable\API\Http\Request\Project\BulkDeleteProjects;
6
use Marek\Toggable\API\Http\Request\ProjectUsers\BulkUpdateProjectUsers;
7
use Marek\Toggable\API\Http\Request\ProjectUsers\CreateMultipleProjectUsers;
8
use Marek\Toggable\API\Http\Request\ProjectUsers\CreateProjectUser;
9
use Marek\Toggable\API\Http\Request\ProjectUsers\DeleteProjectUser;
10
use Marek\Toggable\API\Http\Request\ProjectUsers\UpdateProjectUser;
11
use Marek\Toggable\API\Http\Response\ProjectUsers\ProjectUser;
12
use Marek\Toggable\API\Http\Response\ProjectUsers as ProjectUsersResponse;
13
use Marek\Toggable\API\Toggl\Values\Project\User;
14
use Marek\Toggable\Service\AbstractService;
15
16
/**
17
 * Class ProjectUsersService
18
 * @package Marek\Toggable\Service\ProjectUsers
19
 */
20
class ProjectUsersService extends AbstractService implements \Marek\Toggable\API\Toggl\ProjectUsersServiceInterface
21
{
22
    /**
23
     * @inheritDoc
24
     */
25
    public function createProjectUser(\Marek\Toggable\API\Toggl\Values\Project\User $user)
26
    {
27
        $request = new CreateProjectUser(
28
            array(
29
                'data' => $this->extractDataFromObject($user),
30
            )
31
        );
32
33
        $response = $this->delegate($request);
34
35
        return new ProjectUser(
36
            array(
37
                'projectUser' => $this->hydrateDataFromArrayToObject($response, new User()),
38
            )
39
        );
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 View Code Duplication
    public function updateProjectUser($projectUserId, \Marek\Toggable\API\Toggl\Values\Project\User $user)
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...
46
    {
47
        $request = new UpdateProjectUser(
48
            array(
49
                'projectUserId' => $this->validate($projectUserId),
50
                'data' => $this->extractDataFromObject($user),
51
            )
52
        );
53
54
        $response = $this->delegate($request);
55
56
        return new ProjectUser(
57
            array(
58
                'projectUser' => $this->hydrateDataFromArrayToObject($response, new User()),
59
            )
60
        );
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66
    public function deleteProjectUser($projectUserId)
67
    {
68
        $request = new DeleteProjectUser(
69
            array(
70
                'projectUserId' => $this->validate($projectUserId),
71
            )
72
        );
73
74
        return $this->delegate($request);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function createMultipleProjectUsers($projectId, $userIds, \Marek\Toggable\API\Toggl\Values\Project\User $user)
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...
81
    {
82
        $request = new CreateMultipleProjectUsers(
83
            array(
84
                'projectId' => $this->validate($projectId),
85
                'userIds' => $userIds,
86
                'data' => $this->extractDataFromObject($user),
87
            )
88
        );
89
90
        $response = $this->delegate($request);
91
92
        $projectUsers = array();
93
        foreach ($response->body as $projectUser) {
94
            $projectUsers[] = $this->hydrator->hydrate($projectUser, new ProjectUser());
95
        }
96
97
        return new ProjectUsersResponse(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Marek\Toggab...rs' => $projectUsers)); (Marek\Toggable\API\Http\Response\ProjectUsers) is incompatible with the return type declared by the interface Marek\Toggable\API\Toggl...ateMultipleProjectUsers of type Marek\Toggable\API\Http\...ojectUsers\ProjectUsers.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
98
            array(
99
                'projectUsers' => $projectUsers,
100
            )
101
        );
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function updateMultipleProjectUsers($projectUserIds, \Marek\Toggable\API\Toggl\Values\Project\User $user)
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...
108
    {
109
        $request = new BulkUpdateProjectUsers(
110
            array(
111
                'projectUserIds' => $projectUserIds,
112
                'data' => $this->extractDataFromObject($user),
113
            )
114
        );
115
116
        $response = $this->delegate($request);
117
118
        $projectUsers = array();
119
        foreach ($response->body as $projectUser) {
120
            $projectUsers[] = $this->hydrator->hydrate($projectUser, new ProjectUser());
121
        }
122
123
        return new ProjectUsersResponse(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Marek\Toggab...rs' => $projectUsers)); (Marek\Toggable\API\Http\Response\ProjectUsers) is incompatible with the return type declared by the interface Marek\Toggable\API\Toggl...ateMultipleProjectUsers of type Marek\Toggable\API\Http\...ojectUsers\ProjectUsers.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
124
            array(
125
                'projectUsers' => $projectUsers,
126
            )
127
        );
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function deleteMultipleProjectUsers($projectUserIds)
134
    {
135
        $request = new BulkDeleteProjects(
136
            array(
137
                'projectUserIds' => $projectUserIds,
138
            )
139
        );
140
141
        return $this->delegate($request);
142
    }
143
144
}
145
146