Permission::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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