Completed
Push — l10n_master ( 67e2d3...a09cbd )
by Sander
539:06 queued 523:44
created

ChangeableLimitTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 82
Duplicated Lines 15.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 13
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C bindRequest() 13 53 12
A getLimit() 0 4 1
A getLimitOptions() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kunstmaan\AdminListBundle\Traits;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Trait ChangeableLimitTrait
9
 */
10
trait ChangeableLimitTrait
11
{
12
    private $limit;
13
14
    /**
15
     * Bind current request.
16
     *
17
     * @param Request $request
18
     */
19
    public function bindRequest(Request $request)
20
    {
21
        $query = $request->query;
22
        $session = $request->getSession();
23
24
        $adminListName = 'listconfig_' . $request->get('_route');
25
26
        $this->page = $request->query->getInt('page', 1);
0 ignored issues
show
Bug introduced by
The property page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        $this->limit = $request->query->getInt('limit', $this->getLimitOptions()[0]);
28
29
        $adminListSessionData = $request->getSession()->get($adminListName);
30
        if (!$query->has('limit') && null !== $adminListSessionData && isset($adminListSessionData['limit'])) {
31
            $this->limit = $adminListSessionData['limit'];
32
        }
33
34
        if ($request->query->has('limit') && !$request->query->has('page')) {
35
            $this->page = 1;
36
        }
37
38
        // Allow alphanumeric, _ & . in order by parameter!
39
        $this->orderBy = preg_replace('/[^[a-zA-Z0-9\_\.]]/', '', $request->query->get('orderBy', ''));
0 ignored issues
show
Bug introduced by
The property orderBy does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
        $this->orderDirection = $request->query->getAlpha('orderDirection', '');
0 ignored issues
show
Bug introduced by
The property orderDirection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
42
        // there is a session and the filter param is not set
43 View Code Duplication
        if ($session->has($adminListName) && !$query->has('filter')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            if (!$query->has('page') && !$query->has('limit')) {
45
                $this->page = $adminListSessionData['page'];
46
            }
47
48
            if (!$query->has('orderBy')) {
49
                $this->orderBy = $adminListSessionData['orderBy'];
50
            }
51
52
            if (!$query->has('orderDirection')) {
53
                $this->orderDirection = $adminListSessionData['orderDirection'];
54
            }
55
        }
56
57
        // save current parameters
58
        $session->set(
59
            $adminListName,
60
            [
61
                'page' => $this->page,
62
                'limit' => $this->limit,
63
                'orderBy' => $this->orderBy,
64
                'orderDirection' => $this->orderDirection,
65
            ]
66
        );
67
68
        // Remove limit from query param so it doesn't affect the session of the filter builder
69
        $request->query->remove('limit');
70
        $this->getFilterBuilder()->bindRequest($request);
0 ignored issues
show
Bug introduced by
It seems like getFilterBuilder() 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...
71
    }
72
73
    /**
74
     * @return int
75
     */
76
    public function getLimit()
77
    {
78
        return $this->limit;
79
    }
80
81
    /** @return array */
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
82
    public function getLimitOptions()
83
    {
84
        return [
85
            10,
86
            20,
87
            50,
88
            100,
89
        ];
90
    }
91
}
92