CheckBoard::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Model\helpdesk\Settings\System;
6
use Closure;
7
8
/**
9
 * CheckBoard.
10
 * Checking if the system board is online or offline.
11
 *
12
 * @author   Ladybird <[email protected]>
13
 */
14
class CheckBoard
15
{
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param \Illuminate\Http\Request $request
20
     * @param \Closure                 $next
21
     *
22
     * @return type Mixed
23
     */
24
    public function handle($request, Closure $next)
25
    {
26
        if ($this->checkBoard() == '1') {
27
            return $next($request);
28
        } else {
29
            return redirect()->route('board.offline')->with('offline', 'the system seems to be offline please try later');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect()->route...ine please try later'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Middleware\CheckBoard::handle of type App\Http\Middleware\type.

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...
30
        }
31
    }
32
33
    /**
34
     * Function to get the system offline details.
35
     *
36
     * @return type Mixed
37
     */
38
    public function checkBoard()
39
    {
40
        $res = 0;
41
        $system = new System();
42
        if ($system->first()) {
0 ignored issues
show
Documentation Bug introduced by
The method first does not exist on object<App\Model\helpdesk\Settings\System>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
43
            $res = $system->first()->status;
0 ignored issues
show
Documentation Bug introduced by
The method first does not exist on object<App\Model\helpdesk\Settings\System>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
        }
45
46
        return $res;
47
    }
48
}
49