Completed
Pull Request — master (#2)
by Beñat
02:38
created

InMemoryStorage   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 119
Duplicated Lines 41.18 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 0
dl 49
loc 119
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findAll() 13 13 3
B query() 18 18 5
A properties() 0 8 2
A size() 0 4 1
A paginate() 0 4 1
A sort() 18 18 4

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 LIN3S\WPSymfonyForm\Admin\Storage;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
/**
8
 * In memory strategy of storage.
9
 *
10
 * @author Beñat Espiña <[email protected]>
11
 */
12
class InMemoryStorage implements Storage
13
{
14
    /**
15
     * The data collection.
16
     *
17
     * @var array
18
     */
19
    private $data;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param mixed $data The data
25
     */
26
    public function __construct($data)
27
    {
28
        $this->data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function findAll($limit, $offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
    {
36
        $data = $this->data;
37
        foreach ($data as $key => $item) {
38
            if (array_key_exists('ID', $item)) {
39
                continue;
40
            }
41
            $data[$key]['ID'] = $key;
42
        }
43
        usort($data, [$this, 'sort']);
44
45
        return $this->paginate($data, $limit, $offset);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function query(array $criteria, $limit, $offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
52
    {
53
        $data = [];
54
        foreach ($this->data as $key => $item) {
55
            foreach ($criteria as $name => $singleCriteria) {
56
                if ($item[$name] === $singleCriteria) {
57
                    $data[$key] = $item;
58
                }
59
            }
60
            if (array_key_exists('ID', $item)) {
61
                continue;
62
            }
63
            $data[$key]['ID'] = $key;
64
        }
65
        usort($data, [$this, 'sort']);
66
67
        return $this->paginate($data, $limit, $offset);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function properties()
74
    {
75
        if (is_array($this->data[0])) {
76
            return array_keys($this->data[0]);
77
        }
78
79
        return array_keys($this->data);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function size()
86
    {
87
        return count($this->data);
88
    }
89
90
    /**
91
     * Paginates the given collection of data.
92
     *
93
     * @param array $data   The collection data
94
     * @param int   $limit  Logs per page
95
     * @param int   $offset The number of the page
96
     *
97
     * @return array
98
     */
99
    private function paginate($data, $limit, $offset)
100
    {
101
        return array_slice($data, (($offset - 1) * $limit), $limit);
102
    }
103
104
    /**
105
     * Compares the given two items.
106
     *
107
     * @param mixed $item1 The first item
108
     * @param mixed $item2 The second item
109
     *
110
     * @return int
111
     */
112 View Code Duplication
    private function sort($item1, $item2)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
Coding Style introduced by
sort uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
113
    {
114
        $orderBy = 'name';
115
        $order = 'asc';
116
117
        if (!empty($_GET['orderby'])) {
118
            $orderBy = $_GET['orderby'];
119
        }
120
        if (!empty($_GET['order'])) {
121
            $order = $_GET['order'];
122
        }
123
        $result = strcmp($item1[$orderBy], $item2[$orderBy]);
124
        if ('asc' === $order) {
125
            return $result;
126
        }
127
128
        return -$result;
129
    }
130
}
131