SettingsController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 12 3
A showProjects() 0 13 1
1
<?php
2
3
namespace Larafolio\Http\Controllers;
4
5
use View;
6
use Larafolio\Models\Project;
7
8
class SettingsController extends Controller
9
{
10
    /**
11
     * Show the requested settings page if it exists.
12
     *
13
     * @param string $page Settings page name.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function show($page)
18
    {
19
        if (!View::exists("larafolio::settings.{$page}")) {
20
            abort(404);
21
        }
22
23
        if (method_exists($this, $method = 'show'.ucfirst($page))) {
24
            return $this->{$method}();
25
        }
26
27
        return view("larafolio::settings.{$page}");
1 ignored issue
show
Bug Best Practice introduced by
The return type of return view("larafolio::settings.{$page}"); (Illuminate\View\View|Ill...\Contracts\View\Factory) is incompatible with the return type documented by Larafolio\Http\Controlle...ettingsController::show of type Illuminate\Http\Response.

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...
28
    }
29
30
    /**
31
     * Show the projects settings page.
32
     *
33
     * @return \Illuminate\Http\Response
34
     */
35
    private function showProjects()
36
    {
37
        $deletedProjects = Project::onlyTrashed()
1 ignored issue
show
Bug introduced by
The method onlyTrashed() does not exist on Larafolio\Models\Project. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
            ->orderBy('deleted_at', 'DESC')
39
            ->get()
40
            ->map(function (Project $project) {
41
                return $project->generateProps();
42
            });
43
44
        return view('larafolio::settings.projects', [
45
            'deletedProjects' => $deletedProjects,
46
        ]);
47
    }
48
}
49