Completed
Push — EZP-26000-permission-lookup-2 ( e6b0cc )
by
unknown
25:32
created

Repository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Repository\Permission;
8
9
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
10
use eZ\Publish\API\Repository\Values\ValueObject;
11
use eZ\Publish\API\Repository\Values\User\UserReference;
12
13
class Repository implements RepositoryInterface
14
{
15
    /**
16
     * @var \eZ\Publish\API\Repository\Repository
17
     */
18
    protected $innerRepository;
19
20
    /**
21
     * @param \eZ\Publish\API\Repository\Repository $innerRepository
22
     */
23
    public function __construct(RepositoryInterface $innerRepository)
24
    {
25
        $this->innerRepository = $innerRepository;
26
    }
27
28
    public function getCurrentUser()
29
    {
30
        return $this->innerRepository->getCurrentUser();
31
    }
32
33
    public function getCurrentUserReference()
34
    {
35
        return $this->innerRepository->getCurrentUserReference();
36
    }
37
38
    public function setCurrentUser(UserReference $user)
39
    {
40
        return $this->innerRepository->setCurrentUser($user);
41
    }
42
43
    public function sudo(\Closure $callback)
44
    {
45
        return $this->innerRepository->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\Permission\Repository, eZ\Publish\Core\Repository\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...
46
    }
47
48
    public function hasAccess($module, $function, UserReference $user = null)
49
    {
50
        return $this->innerRepository->hasAccess($module, $function, $user);
51
    }
52
53
    public function canUser($module, $function, ValueObject $object, $targets = null)
54
    {
55
        return $this->innerRepository->canUser($module, $function, $object, $targets);
56
    }
57
58
    public function getContentService()
59
    {
60
        return $this->innerRepository->getContentService();
61
    }
62
63
    public function getContentLanguageService()
64
    {
65
        return $this->innerRepository->getContentLanguageService();
66
    }
67
68
    public function getContentTypeService()
69
    {
70
        return $this->innerRepository->getContentTypeService();
71
    }
72
73
    public function getLocationService()
74
    {
75
        return $this->innerRepository->getLocationService();
76
    }
77
78
    public function getTrashService()
79
    {
80
        return $this->innerRepository->getTrashService();
81
    }
82
83
    public function getSectionService()
84
    {
85
        return $this->innerRepository->getSectionService();
86
    }
87
88
    public function getUserService()
89
    {
90
        return $this->innerRepository->getUserService();
91
    }
92
93
    public function getURLAliasService()
94
    {
95
        return $this->innerRepository->getURLAliasService();
96
    }
97
98
    public function getURLWildcardService()
99
    {
100
        return $this->innerRepository->getURLWildcardService();
101
    }
102
103
    public function getObjectStateService()
104
    {
105
        return $this->innerRepository->getObjectStateService();
106
    }
107
108
    public function getRoleService()
109
    {
110
        return $this->innerRepository->getRoleService();
111
    }
112
113
    public function getSearchService()
114
    {
115
        return $this->innerRepository->getSearchService();
116
    }
117
118
    public function getFieldTypeService()
119
    {
120
        return $this->innerRepository->getFieldTypeService();
121
    }
122
123
    public function beginTransaction()
124
    {
125
        return $this->innerRepository->beginTransaction();
126
    }
127
128
    public function commit()
129
    {
130
        return $this->innerRepository->commit();
131
    }
132
133
    public function rollback()
134
    {
135
        return $this->innerRepository->rollback();
136
    }
137
138
    public function commitEvent($event)
139
    {
140
        return $this->innerRepository->commitEvent($event);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...pository::commitEvent() has been deprecated with message: In 5.3.3, to be removed. Signals are emitted after transaction instead of being required to use this.

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...
141
    }
142
143
    public function createDateTime($timestamp = null)
144
    {
145
        return $this->innerRepository->createDateTime($timestamp);
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 createDateTime() does only exist in the following implementations of said interface: eZ\Publish\Core\Repository\Permission\Repository, eZ\Publish\Core\Repository\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...
146
    }
147
}
148