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 ( 54c72b...e7e058 )
by Aden
03:11
created

AttributeManager::renderAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 5
1
<?php
2
3
namespace LaravelFlare\Flare\Admin\Attributes;
4
5
class AttributeManager
6
{
7
    /**
8
     * Render Attribute.
9
     *
10
     * @param string $action
11
     * @param string $attribute
12
     * @param string $field
13
     * @param string $model
14
     * @param string $modelManager
15
     *
16
     * @return \Illuminate\Http\Response
17
     */
18
    public function renderAttribute($action, $attribute, $field, $model, $modelManager)
19
    {
20
        if (!isset($field['type'])) {
21
            throw new \Exception('Attribute Field Type cannot be empty or undefined.');
22
        }
23
24
        if ($this->attributeTypeExists($field['type'])) {
25
            $fieldType = $this->resolveAttributeClass($field['type']);
26
27
            return call_user_func_array([new $fieldType($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
28
        }
29
30
        return call_user_func_array([new BaseAttribute($attribute, $field, $model, $modelManager), camel_case('render_'.$action)], []);
31
    }
32
33
    /**
34
     * Returns an array of all of the Available Attribute Types.
35
     * 
36
     * @return array
37
     */
38
    protected function availableAttributes()
39
    {
40
        $availableAttributes = [];
41
42
        foreach (\Flare::config('attributes') as $attributeType => $attributeFullClassname) {
43
            $availableAttributes = array_add(
44
                                            $availableAttributes,
45
                                            $attributeType,
46
                                            $attributeFullClassname
47
                                        );
48
        }
49
50
        return $availableAttributes;
51
    }
52
53
    /**
54
     * Determines if an AttributeType class exists or not.
55
     * 
56
     * @param string $type
57
     * 
58
     * @return bool
59
     */
60
    protected function attributeTypeExists($type)
61
    {
62
        return $this->resolveAttributeClass($type) ? true : false;
63
    }
64
65
    /**
66
     * Resolves the Class of an Attribute and returns it as a string.
67
     * 
68
     * @param string $type
69
     * 
70
     * @return string
71
     */
72
    protected function resolveAttributeClass($type)
73
    {
74
        if (class_exists($type)) {
75
            return $type;
76
        }
77
78
        if (array_key_exists($type, $attributes = $this->availableAttributes())) {
79
            return $this->availableAttributes()[$type];
80
        }
81
82
        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...
83
    }
84
}
85