Completed
Push — master ( 7dd2dd...7e98b2 )
by Adam
03:24 queued 01:04
created

Permission   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 104
ccs 12
cts 16
cp 0.75
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getResource() 0 4 1
A getPrivilege() 0 4 1
A getAssertion() 0 4 1
A setDetails() 0 4 1
A getTitle() 0 4 2
A getDescription() 0 4 2
A __toString() 0 4 1
1
<?php
2
/**
3
 * Permission.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:Permissions!
9
 * @subpackage     Entities
10
 * @since          1.0.0
11
 *
12
 * @date           13.03.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Permissions\Entities;
18
19
use Nette;
20
use Nette\Utils;
21
22
use IPub;
23
use IPub\Permissions\Security;
24
25 1
class Permission extends Nette\Object implements IPermission
26
{
27
	/**
28
	 * Permission resource
29
	 *
30
	 * @var string
31
	 */
32
	private $resource;
33
34
	/**
35
	 * Permission privilege
36
	 *
37
	 * @var string
38
	 */
39
	private $privilege;
40
41
	/**
42
	 * @var callable|NULL
43
	 */
44
	private $assertion;
45
46
	/**
47
	 * Permission details
48
	 *
49
	 * @var Utils\ArrayHash
50
	 */
51
	private $details;
52
53
	/**
54
	 * @param IResource|NULL $resource
55
	 * @param string|NULL $privilege
56
	 * @param array $details
57
	 * @param callable|NULL $assertion
58
	 */
59
	public function __construct(IResource $resource = NULL, string $privilege = NULL, $details = [], callable $assertion = NULL)
60
	{
61 1
		$this->resource = $resource;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resource can also be of type object<IPub\Permissions\Entities\IResource>. However, the property $resource is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
62 1
		$this->privilege = $privilege;
63 1
		$this->assertion = $assertion;
64
65
		// Check if permission details are provided too
66 1
		if (!empty($details)) {
67 1
			$this->details = Utils\ArrayHash::from($details);
68
		}
69 1
	}
70
71
	/**
72
	 * {@inheritdoc}
73
	 */
74
	public function getResource()
75
	{
76 1
		return $this->resource;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->resource; (string) is incompatible with the return type declared by the interface IPub\Permissions\Entities\IPermission::getResource of type IPub\Permissions\Entities\IResource|null.

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...
77
	}
78
79
	/**
80
	 * {@inheritdoc}
81
	 */
82
	public function getPrivilege()
83
	{
84 1
		return $this->privilege;
85
	}
86
87
	/**
88
	 * {@inheritdoc}
89
	 */
90
	public function getAssertion()
91
	{
92 1
		return $this->assertion;
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98
	public function setDetails(array $details)
99
	{
100
		$this->details = Utils\ArrayHash::from($details);
101
	}
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function getTitle()
107
	{
108
		return $this->details->offsetExists('title') ? $this->details->offsetGet('title') : NULL;
109
	}
110
111
	/**
112
	 * {@inheritdoc}
113
	 */
114
	public function getDescription()
115
	{
116
		return $this->details->offsetExists('description') ? $this->details->offsetGet('description') : NULL;
117
	}
118
119
	/**
120
	 * Convert permission object to string
121
	 *
122
	 * @return string
123
	 */
124
	public function __toString()
125
	{
126 1
		return ((string) $this->resource) . self::DELIMITER . $this->privilege;
127
	}
128
}
129