Redirect::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 9.6666
1
<?php
2
3
namespace App\Middleware;
4
5
use Zend\Diactoros\Response\HtmlResponse;
6
use App\Models\Redirect as RedirectModel;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Zend\Diactoros\Response\RedirectResponse;
9
use Interop\Http\ServerMiddleware\DelegateInterface;
10
use Interop\Http\ServerMiddleware\MiddlewareInterface;
11
12
class Redirect implements MiddlewareInterface
13
{
14
    /**
15
     * PSR-15 middleware callback
16
     *
17
     * @param ServerRequestInterface $request
18
     * @param DelegateInterface $delegate
19
     * @return Psr\Http\Message\ServerRequestInterface
0 ignored issues
show
Bug introduced by
The type App\Middleware\Psr\Http\...\ServerRequestInterface was not found. Did you mean Psr\Http\Message\ServerRequestInterface? If so, make sure to prefix the type with \.
Loading history...
20
     */
21 2
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
22
    {
23 2
        $path = substr($request->getUri()->getPath(), 1);
24 2
        $redirect = RedirectModel::where('hash', $path)->first();
25 2
        if ($redirect) {
26 1
            $redirect->increment('count');
27 1
            return new RedirectResponse($redirect->redirect_to, 301);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Zend\Diactoro...rect->redirect_to, 301) returns the type Zend\Diactoros\Response\RedirectResponse which is incompatible with the documented return type App\Middleware\Psr\Http\...\ServerRequestInterface.
Loading history...
Bug introduced by
The property redirect_to does not seem to exist on App\Models\Redirect. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
28
        }
29 1
        return new HtmlResponse(view('404.php'), 404);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Zend\Diactoro...e(view('404.php'), 404) returns the type Zend\Diactoros\Response\HtmlResponse which is incompatible with the documented return type App\Middleware\Psr\Http\...\ServerRequestInterface.
Loading history...
Bug introduced by
Are you sure the usage of view('404.php') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
Bug introduced by
view('404.php') of type void is incompatible with the type string|Psr\Http\Message\StreamInterface expected by parameter $html of Zend\Diactoros\Response\...Response::__construct(). ( Ignorable by Annotation )

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

29
        return new HtmlResponse(/** @scrutinizer ignore-type */ view('404.php'), 404);
Loading history...
30
    }
31
}
32