helpers.php ➔ can_impersonate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 7
loc 7
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 Ryan Chandler
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()
18
            && app('auth')->guard($guard)->user()->canImpersonate();
19
	}
20
}
21
22
if (! function_exists('can_be_impersonated')) {
23
24
	/**
25
	 * Check whether the specified user can be impersonated.
26
	 *
27
	 * @param  Authenticatable  $user
28
	 * @param  string|null      $guard
29
	 * @return bool
30
	 */
31
	function can_be_impersonated(Authenticatable $user, string $guard = null): bool
32
	{
33
		$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
34
35
		return app('auth')->guard($guard)->check()
36
		       && app('auth')->guard($guard)->user()->id != $user->id
0 ignored issues
show
Bug introduced by Ryan Chandler
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...
37
		       && $user->canBeImpersonated();
0 ignored issues
show
Bug introduced by Ryan Chandler
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...
38
	}
39
}
40
41 View Code Duplication
if (! function_exists('is_impersonating')) {
0 ignored issues
show
Duplication introduced by Ryan Chandler
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...
42
43
	/**
44
	 * Check whether the current user is being impersonated.
45
	 *
46
	 * @param  string|null  $guard
47
	 * @return bool
48
	 */
49
	function is_impersonating(string $guard = null): bool
50
	{
51
		$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();
52
53
		return app('auth')->guard($guard)->check()
54
            && app('auth')->guard($guard)->user()->isImpersonated();
55
	}
56
}
57