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

Isallowed::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 9
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
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 Isallowed 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;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by Admin\View\Helper\Isallowed::__invoke of type ZfcUser\Entity\UserInterface.

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...
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
}