Completed
Push — master ( 11dcd2...0079da )
by Marceau
01:13
created

helpers.php ➔ can_impersonate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 6
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 6
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Contracts\Auth\Authenticatable;
4
5 View Code Duplication
if (! function_exists('can_impersonate')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
6
7
	/**
8
	 * Check whether the current user is authorized to impersonate.
9
	 *
10
	 * @param  null  $guard
11
	 * @return bool
12
	 */
13
	function can_impersonate(string $guard = null): bool
14
	{
15
		$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
16
17
		return app('auth')->guard($guard)->check() && app('auth')->guard($guard)->canImpersonate();
18
	}
19
}
20
21
if (! function_exists('can_be_impersonated')) {
22
23
	/**
24
	 * Check whether the specified user can be impersonated.
25
	 *
26
	 * @param  Authenticatable  $user
27
	 * @param  string|null      $guard
28
	 * @return bool
29
	 */
30
	function can_be_impersonated(Authenticatable $user, string $guard = null): bool
31
	{
32
		$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
33
34
		return app('auth')->guard($guard)->check()
35
		       && app('auth')->guard($guard)->user()->id != $user->id
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
36
		       && $user->canBeImpersonated();
0 ignored issues
show
Bug introduced by
The method canBeImpersonated() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

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...
37
	}
38
}
39
40 View Code Duplication
if (! function_exists('is_impersonating')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
42
	/**
43
	 * Check whether the current user is being impersonated.
44
	 *
45
	 * @param  string|null  $guard
46
	 * @return bool
47
	 */
48
	function is_impersonating(string $guard = null): bool
49
	{
50
		$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
51
52
		return app('auth')->guard($guard)->check() && app('auth')->guard($guard)->user()->isImpersonated();
53
	}
54
}
55