Completed
Push — master ( 4e0ac6...0fdc9b )
by Łukasz
25:36
created

Repository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 11
dl 0
loc 25
rs 9.52
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Repository class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\SiteAccessAware;
10
11
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12
use eZ\Publish\API\Repository\Values\ValueObject;
13
use eZ\Publish\API\Repository\Values\User\UserReference;
14
use Closure;
15
16
/**
17
 * Repository class.
18
 */
19
class Repository implements RepositoryInterface
0 ignored issues
show
Bug introduced by
There is one abstract method getNotificationService in this class; you could implement it, or declare this class as abstract.
Loading history...
20
{
21
    /** @var \eZ\Publish\API\Repository\Repository */
22
    protected $repository;
23
24
    /** @var \eZ\Publish\API\Repository\ContentService */
25
    protected $contentService;
26
27
    /** @var \eZ\Publish\API\Repository\SectionService */
28
    protected $sectionService;
29
30
    /** @var \eZ\Publish\API\Repository\SearchService */
31
    protected $searchService;
32
33
    /** @var \eZ\Publish\API\Repository\UserService */
34
    protected $userService;
35
36
    /** @var \eZ\Publish\API\Repository\LanguageService */
37
    protected $languageService;
38
39
    /** @var \eZ\Publish\API\Repository\LocationService */
40
    protected $locationService;
41
42
    /** @var \eZ\Publish\API\Repository\TrashService */
43
    protected $trashService;
44
45
    /** @var \eZ\Publish\API\Repository\ContentTypeService */
46
    protected $contentTypeService;
47
48
    /** @var \eZ\Publish\API\Repository\ObjectStateService */
49
    protected $objectStateService;
50
51
    /** @var \eZ\Publish\API\Repository\URLAliasService */
52
    protected $urlAliasService;
53
54
    /**
55
     * Construct repository object from aggregated repository.
56
     */
57
    public function __construct(
58
        RepositoryInterface $repository,
59
        ContentService $contentService,
60
        ContentTypeService $contentTypeService,
61
        ObjectStateService $objectStateService,
62
        URLAliasService $urlAliasService,
63
        UserService $userService,
64
        SearchService $searchService,
65
        SectionService $sectionService,
66
        TrashService $trashService,
67
        LocationService $locationService,
68
        LanguageService $languageService
69
    ) {
70
        $this->repository = $repository;
71
        $this->contentService = $contentService;
72
        $this->contentTypeService = $contentTypeService;
73
        $this->objectStateService = $objectStateService;
74
        $this->urlAliasService = $urlAliasService;
75
        $this->userService = $userService;
76
        $this->searchService = $searchService;
77
        $this->sectionService = $sectionService;
78
        $this->trashService = $trashService;
79
        $this->locationService = $locationService;
80
        $this->languageService = $languageService;
81
    }
82
83
    public function getCurrentUser()
84
    {
85
        return $this->repository->getCurrentUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::getCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user. Loads the full user object if not already loaded, if you only need to know user id use {@see getCurrentUserReference()}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
86
    }
87
88
    public function getCurrentUserReference()
89
    {
90
        return $this->repository->getCurrentUserReference();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...tCurrentUserReference() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::getCurrentUserReference() instead. Get current user reference.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91
    }
92
93
    public function setCurrentUser(UserReference $user)
94
    {
95
        return $this->repository->setCurrentUser($user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...itory::setCurrentUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::setCurrentUserReference() instead. Sets the current user to the given $user.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
    }
97
98
    public function sudo(Closure $callback)
99
    {
100
        return $this->repository->sudo($callback, $this);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface eZ\Publish\API\Repository\Repository as the method sudo() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Repository, eZ\Publish\Core\Reposito...eAccessAware\Repository, eZ\Publish\Core\SignalSlot\Repository.

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...
101
    }
102
103
    public function hasAccess($module, $function, UserReference $user = null)
104
    {
105
        return $this->repository->hasAccess($module, $function, $user);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::hasAccess() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::hasAccess() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
106
    }
107
108
    public function canUser($module, $function, ValueObject $object, $targets = null)
109
    {
110
        return $this->repository->canUser($module, $function, $object, $targets);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repository\Repository::canUser() has been deprecated with message: since 6.6, to be removed. Use PermissionResolver::canUser() instead. Indicates if the current user is allowed to perform an action given by the function on the given
objects. Example: canUser( 'content', 'edit', $content, $location ); This will check edit permission on content given the specific location, if skipped if will check on all locations. Example2: canUser( 'section', 'assign', $content, $section ); Check if user has access to assign $content to $section.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
111
    }
112
113
    public function getContentService()
114
    {
115
        return $this->contentService;
116
    }
117
118
    public function getContentLanguageService()
119
    {
120
        return $this->languageService;
121
    }
122
123
    public function getContentTypeService()
124
    {
125
        return $this->contentTypeService;
126
    }
127
128
    public function getLocationService()
129
    {
130
        return $this->locationService;
131
    }
132
133
    public function getTrashService()
134
    {
135
        return $this->trashService;
136
    }
137
138
    public function getSectionService()
139
    {
140
        return $this->sectionService;
141
    }
142
143
    public function getUserService()
144
    {
145
        return $this->userService;
146
    }
147
148
    public function getURLAliasService()
149
    {
150
        return $this->urlAliasService;
151
    }
152
153
    public function getURLWildcardService()
154
    {
155
        return $this->repository->getURLWildcardService();
156
    }
157
158
    public function getObjectStateService()
159
    {
160
        return $this->objectStateService;
161
    }
162
163
    public function getRoleService()
164
    {
165
        return $this->repository->getRoleService();
166
    }
167
168
    public function getSearchService()
169
    {
170
        return $this->searchService;
171
    }
172
173
    public function getFieldTypeService()
174
    {
175
        return $this->repository->getFieldTypeService();
176
    }
177
178
    public function getPermissionResolver()
179
    {
180
        return $this->repository->getPermissionResolver();
181
    }
182
183
    public function getURLService()
184
    {
185
        return $this->repository->getURLService();
186
    }
187
188
    public function getBookmarkService()
189
    {
190
        return $this->repository->getBookmarkService();
191
    }
192
193
    public function beginTransaction()
194
    {
195
        return $this->repository->beginTransaction();
196
    }
197
198
    public function commit()
199
    {
200
        return $this->repository->commit();
201
    }
202
203
    public function rollback()
204
    {
205
        return $this->repository->rollback();
206
    }
207
}
208