Passed
Push — master ( 700b0f...aafb0a )
by Björn
18:25 queued 10s
created

Isdenied::getAuthService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 4
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * BB's Zend Framework 2 Components
4
 * 
5
 * AdminModule
6
 *
7
 * @package   [MyApplication]
8
 * @package   BB's Zend Framework 2 Components
9
 * @package   AdminModule
10
 * @author    Björn Bartels <[email protected]>
11
 * @link      https://gitlab.bjoernbartels.earth/groups/zf2
12
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
13
 * @copyright copyright (c) 2016 Björn Bartels <[email protected]>
14
 */
15
16
namespace Admin\View\Helper;
17
18
use Zend\View\Helper\AbstractHelper;
19
20 View Code Duplication
class Isdenied extends AbstractHelper
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    /**
23
     * @var AuthenticationService
24
     */
25
    protected $authService;
26
    
27
    /**
28
     * __invoke
29
     *
30
     * @access public
31
     * @return \ZfcUser\Entity\UserInterface
32
     */
33
    public function __invoke( $resource )
34
    {
35
        /**
36
 * @var \Zend\Permissions\Acl\Acl $acl 
37
**/
38
        $acl = $this->view->navigation()->getAcl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\View\Renderer\RendererInterface as the method navigation() does only exist in the following implementations of said interface: Zend\View\Renderer\PhpRenderer.

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...
39
        if (empty($resource) || !$acl->hasResource($resource) ) {
40
            return true;
41
        }    
42
        /**
43
 * @var \Admin\Entity\User $user 
44
**/
45
        $user = $this->view->zfcUserIdentity(); // ->getIdentity();
0 ignored issues
show
Bug introduced by
The method zfcUserIdentity() does not seem to exist on object<Zend\View\Renderer\RendererInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
        if ($user) { // ($this->getAuthService()->hasIdentity()) {
47
            //$user = $this->getAuthService()->getIdentity();
48
            $role = $user->getAclrole();
49
        } else {
50
            $role = 'public';
51
        }
52
        return ( !$acl->isAllowed($role, $resource) );
53
    }
54
    
55
    /**
56
     * Get authService.
57
     *
58
     * @return AuthenticationService
59
     */
60
    public function getAuthService()
61
    {
62
        return $this->authService;
63
    }
64
    
65
    /**
66
     * Set authService.
67
     *
68
     * @param  AuthenticationService $authService
69
     * @return \ZfcUser\View\Helper\ZfcUserIdentity
70
     */
71
    public function setAuthService(AuthenticationService $authService)
72
    {
73
        $this->authService = $authService;
74
        return $this;
75
    }
76
    
77
}