Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

HasPreviousUrlTrait::previous()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 20
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Generator\Traits;
4
5
use Nip\Router\RequestContext;
6
7
/**
8
 * Trait HasPreviousUrlTrait
9
 * @package Nip\Router\Generator\Traits
10
 * @method RequestContext getContext()
11
 */
12
trait HasPreviousUrlTrait
13
{
14
15
    /**
16
     * Get the URL for the previous request.
17
     *
18
     * @param  mixed $fallback
19
     * @return string
20
     */
21
    public function previous($fallback = false)
22
    {
23
        $referrer = $this->getContext()->getHeaders()->get('referer');
24
        $url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
0 ignored issues
show
Bug introduced by
It seems like to() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
        if ($url) {
26
            return $url;
27
        } elseif ($fallback) {
28
            return $this->to($fallback);
0 ignored issues
show
Bug introduced by
It seems like to() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
        } else {
30
            return $this->to('/');
0 ignored issues
show
Bug introduced by
It seems like to() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
31
        }
32
    }
33
34
    /**
35
     * Get the previous URL from the session if possible.
36
     *
37
     * @return string|null
38
     */
39
    protected function getPreviousUrlFromSession()
40
    {
41
        return null;
42
//        $session = $this->getSession();
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
43
//        return $session ? $session->previousUrl() : null;
44
    }
45
46
}