Completed
Push — master ( 425846...6cb170 )
by Adam
07:36
created

Permission::getAssertion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
class Permission implements IPermission
26
{
27
	/**
28
	 * Implement nette smart magic
29
	 */
30
	use Nette\SmartObject;
31
32
	/**
33
	 * Permission resource
34
	 *
35
	 * @var string
36
	 */
37
	private $resource;
38
39
	/**
40
	 * Permission privilege
41
	 *
42
	 * @var string
43
	 */
44
	private $privilege;
45
46
	/**
47
	 * @var callable|NULL
48
	 */
49
	private $assertion;
50
51
	/**
52
	 * Permission details
53
	 *
54
	 * @var Utils\ArrayHash
55
	 */
56
	private $details;
57
58
	/**
59
	 * @param IResource|NULL $resource
60
	 * @param string|NULL $privilege
61
	 * @param array $details
62
	 * @param callable|NULL $assertion
63
	 */
64
	public function __construct(IResource $resource = NULL, string $privilege = NULL, $details = [], callable $assertion = NULL)
65
	{
66
		$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...
67
		$this->privilege = $privilege;
68
		$this->assertion = $assertion;
69
70
		// Check if permission details are provided too
71
		if (!empty($details)) {
72
			$this->details = Utils\ArrayHash::from($details);
73
		}
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function getResource()
80
	{
81
		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...
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function getPrivilege()
88
	{
89
		return $this->privilege;
90
	}
91
92
	/**
93
	 * {@inheritdoc}
94
	 */
95
	public function getAssertion()
96
	{
97
		return $this->assertion;
98
	}
99
100
	/**
101
	 * {@inheritdoc}
102
	 */
103
	public function setDetails(array $details)
104
	{
105
		$this->details = Utils\ArrayHash::from($details);
106
	}
107
108
	/**
109
	 * {@inheritdoc}
110
	 */
111
	public function getTitle()
112
	{
113
		return $this->details->offsetExists('title') ? $this->details->offsetGet('title') : NULL;
114
	}
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	public function getDescription()
120
	{
121
		return $this->details->offsetExists('description') ? $this->details->offsetGet('description') : NULL;
122
	}
123
124
	/**
125
	 * Convert permission object to string
126
	 *
127
	 * @return string
128
	 */
129
	public function __toString()
130
	{
131
		return ((string) $this->resource) . self::DELIMITER . $this->privilege;
132
	}
133
}
134