| Conditions | 7 |
| Paths | 7 |
| Total Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 32 | public function hasRoles($user, $roles, $or = true) { |
||
| 33 | |||
| 34 | // Check the user. |
||
| 35 | if (null === $user || false === ($user instanceof UserInterface)) { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | |||
| 39 | // Check the roles. |
||
| 40 | if (false === is_array($roles)) { |
||
| 41 | $roles = [$roles]; |
||
| 42 | } |
||
| 43 | |||
| 44 | // Initialize the result. |
||
| 45 | $result = 1 <= count($roles); |
||
| 46 | |||
| 47 | // Handle each role. |
||
| 48 | foreach ($roles as $role) { |
||
| 49 | $result &= in_array($role, $user->getRoles()); |
||
| 50 | if (true == $result && true === $or) { |
||
|
|
|||
| 51 | break; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | // Return the result. |
||
| 56 | return boolval($result); |
||
| 57 | } |
||
| 58 | |||
| 60 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.