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

YamlStorage::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Admin\Storage;
13
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * YAML strategy of storage.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class YamlStorage implements Storage
22
{
23
    /**
24
     * The data collection.
25
     *
26
     * @var array
27
     */
28
    private $data;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param string $yamlFile The dir path of YAML file
0 ignored issues
show
Documentation introduced by
Should the type for parameter $yamlFile not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     */
35
    public function __construct($yamlFile = null)
36
    {
37
        if (null === $yamlFile) {
38
            $yamlFile = __DIR__ . '/../../../../../../../../wp_symfony_form_email_log.yml';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $yamlFile. This often makes code more readable.
Loading history...
39
        }
40
        $this->data = Yaml::parse(file_get_contents($yamlFile));
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\...et_contents($yamlFile)) can also be of type string or object<stdClass>. However, the property $data is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
        if (null === $this->data) {
42
            $this->data = [];
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 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...
50
    {
51
        $data = $this->data;
52
        foreach ($data as $key => $item) {
53
            if (array_key_exists('ID', $item)) {
54
                continue;
55
            }
56
            $data[$key]['ID'] = $key;
57
        }
58
        usort($data, [$this, 'sort']);
59
60
        return $this->paginate($data, $limit, $offset);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 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...
67
    {
68
        $data = [];
69
        foreach ($this->data as $key => $item) {
70
            foreach ($criteria as $name => $singleCriteria) {
71
                if ($item[$name] === $singleCriteria) {
72
                    $data[$key] = $item;
73
                }
74
            }
75
            if (array_key_exists('ID', $item)) {
76
                continue;
77
            }
78
            $data[$key]['ID'] = $key;
79
        }
80
        usort($data, [$this, 'sort']);
81
82
        return $this->paginate($data, $limit, $offset);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function properties()
89
    {
90
        return isset($this->data[0]) ? array_keys($this->data[0]) : null;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function size()
97
    {
98
        return count($this->data);
99
    }
100
101
    /**
102
     * Paginates the given collection of data.
103
     *
104
     * @param array $data   The collection data
105
     * @param int   $limit  Logs per page
106
     * @param int   $offset The number of the page
107
     *
108
     * @return array
109
     */
110
    private function paginate($data, $limit, $offset)
111
    {
112
        return array_slice($data, (($offset - 1) * $limit), $limit);
113
    }
114
115
    /**
116
     * Compares the given two items.
117
     *
118
     * @param mixed $item1 The first item
119
     * @param mixed $item2 The second item
120
     *
121
     * @return int
122
     */
123 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...
124
    {
125
        $orderBy = 'name';
126
        $order = 'asc';
127
128
        if (!empty($_GET['orderby'])) {
129
            $orderBy = $_GET['orderby'];
130
        }
131
        if (!empty($_GET['order'])) {
132
            $order = $_GET['order'];
133
        }
134
        $result = strcmp($item1[$orderBy], $item2[$orderBy]);
135
        if ('asc' === $order) {
136
            return $result;
137
        }
138
139
        return -$result;
140
    }
141
}
142