GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 151d6f...d20332 )
by Aden
03:17
created

AttributeManager::createAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 5
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
class AttributeManager
6
{
7
    /**
8
     * Create a new Attribute Instance 
9
     * 
10
     * @param string $type
11
     * @param string $action
0 ignored issues
show
Bug introduced by
There is no parameter named $action. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
12
     * @param string $attribute
13
     * @param string $field
14
     * @param string $model
15
     * @param string $modelManager
16
     */
17
    public function createAttribute($type, $attribute, $field, $model = null, $modelManager = null)
18
    {
19
        if ($this->attributeTypeExists($type)) {
20
            $fieldType = $this->resolveAttributeClass($type);
21
22
            return new $fieldType($attribute, $field, $model, $modelManager);
23
        }
24
25
        return new BaseAttribute($attribute, $field, $model, $modelManager);
26
    }
27
28
    /**
29
     * Render Attribute.
30
     *
31
     * @param string $action
32
     * @param string $attribute
33
     * @param string $field
34
     * @param string $model
35
     * @param string $modelManager
36
     *
37
     * @return \Illuminate\Http\Response
38
     */
39
    public function renderAttribute($action, $attribute, $field, $model = null, $modelManager = null)
40
    {
41
        if (!isset($field['type'])) {
42
            throw new \Exception('Attribute Field Type cannot be empty or undefined.');
43
        }
44
45
        return call_user_func_array([$this->createAttribute($field['type'], $action, $attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
0 ignored issues
show
Unused Code introduced by
The call to AttributeManager::createAttribute() has too many arguments starting with $modelManager.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
    }
47
48
    /**
49
     * Determines if an AttributeType class exists or not.
50
     * 
51
     * @param string $type
52
     * 
53
     * @return bool
54
     */
55
    public function attributeTypeExists($type)
56
    {
57
        return $this->resolveAttributeClass($type) ? true : false;
58
    }
59
60
    /**
61
     * Returns an array of all of the Available Attribute Types.
62
     * 
63
     * @return array
64
     */
65
    protected function availableAttributes()
66
    {
67
        $availableAttributes = [];
68
69
        foreach (\Flare::config('attributes') as $attributeType => $attributeFullClassname) {
70
            $availableAttributes = array_add(
71
                                            $availableAttributes,
72
                                            $attributeType,
73
                                            $attributeFullClassname
74
                                        );
75
        }
76
77
        return $availableAttributes;
78
    }
79
80
    /**
81
     * Resolves the Class of an Attribute and returns it as a string.
82
     * 
83
     * @param string $type
84
     * 
85
     * @return string
86
     */
87
    protected function resolveAttributeClass($type)
88
    {
89
        if (class_exists($type)) {
90
            return $type;
91
        }
92
93
        if (array_key_exists($type, $attributes = $this->availableAttributes())) {
94
            return $this->availableAttributes()[$type];
95
        }
96
97
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by LaravelFlare\Flare\Admin...::resolveAttributeClass of type string.

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...
98
    }
99
}
100