AuthorizationHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
dl 0
loc 41
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateDescription() 0 7 3
A extractAction() 0 5 1
A getWords() 0 7 3
A authorizationException() 0 12 1
1
<?php
2
3
namespace SMartins\JsonHandler;
4
5
use Illuminate\Auth\Access\AuthorizationException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Auth\Access\AuthorizationException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait AuthorizationHandler
8
{
9
    public function authorizationException(AuthorizationException $exception)
10
    {
11
        $error = [[
12
            'status'    => 403,
13
            'code'      => $this->getCode('authorization'),
0 ignored issues
show
Bug introduced by
It seems like getCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

13
            'code'      => $this->/** @scrutinizer ignore-call */ getCode('authorization'),
Loading history...
14
            'source'    => ['pointer' => $exception->getFile().':'.$exception->getLine()],
15
            'title'     => __('exception::exceptions.authorization.title'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
            'title'     => /** @scrutinizer ignore-call */ __('exception::exceptions.authorization.title'),
Loading history...
16
            'detail'    => $exception->getMessage(),
17
        ]];
18
19
        $this->jsonApiResponse->setStatus(403);
0 ignored issues
show
Bug Best Practice introduced by
The property jsonApiResponse does not exist on SMartins\JsonHandler\AuthorizationHandler. Did you maybe forget to declare it?
Loading history...
20
        $this->jsonApiResponse->setErrors($error);
21
    }
22
23
    public function generateDescription($traces)
24
    {
25
        $action = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
26
        foreach ($traces as $trace) {
27
            if ($trace['function'] === 'authorize') {
28
                $action = $this->extractAction($trace['args']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $action is correct as $this->extractAction($trace['args']) targeting SMartins\JsonHandler\Aut...andler::extractAction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
                break;
30
            }
31
        }
32
    }
33
34
    public function extractAction($args)
35
    {
36
        $action = reset($args);
37
38
        $this->getWord($action);
0 ignored issues
show
Bug introduced by
The method getWord() does not exist on SMartins\JsonHandler\AuthorizationHandler. Did you maybe mean getWords()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        $this->/** @scrutinizer ignore-call */ 
39
               getWord($action);

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...
39
    }
40
41
    public function getWords($action)
42
    {
43
        $words = explode('.', $action);
44
        if (! (count($words) > 1)) {
45
            $words = explode('-', $action);
46
            if (! (count($words) > 1)) {
47
                $words = preg_split('/(?=[A-Z])/', $action);
0 ignored issues
show
Unused Code introduced by
The assignment to $words is dead and can be removed.
Loading history...
48
            }
49
        }
50
    }
51
}
52