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

YamlStorage   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 108
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A get() 0 7 1
A properties() 0 4 1
A size() 0 4 1
A all() 0 12 3
A paginate() 0 4 1
A sort() 0 18 4
1
<?php
2
3
namespace LIN3S\WPSymfonyForm\Admin\Storage;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
/**
8
 * YAML strategy of storage.
9
 *
10
 * @author Beñat Espiña <[email protected]>
11
 */
12
class YamlStorage implements Storage
13
{
14
    /**
15
     * The logs collection.
16
     *
17
     * @var array
18
     */
19
    private $logs;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @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...
25
     */
26
    public function __construct($yamlFile = null)
27
    {
28
        if (null === $yamlFile) {
29
            $yamlFile = __DIR__ . '/../../../../../../../../wp_symfony_form_email_log.yml';
30
        }
31
        $this->logs = 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 $logs 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...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function get($limit, $offset)
38
    {
39
        $logs = $this->all();
40
        usort($logs, [$this, 'sort']);
41
42
        return $this->paginate($logs, $limit, $offset);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function properties()
49
    {
50
        return array_keys($this->logs[0]);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function size()
57
    {
58
        return count($this->logs);
59
    }
60
61
    /**
62
     * Retrieve all the logs from YAML storage.
63
     *
64
     * @return array
65
     */
66
    private function all()
67
    {
68
        $logs = $this->logs;
69
        foreach ($logs as $key => $item) {
70
            if (array_key_exists('ID', $item)) {
71
                continue;
72
            }
73
            $logs[$key]['ID'] = $key;
74
        }
75
76
        return $logs;
77
    }
78
79
    /**
80
     * Paginates the given collection of logs.
81
     *
82
     * @param array $logs   The collection logs
83
     * @param int   $limit  Logs per page
84
     * @param int   $offset The number of the page
85
     *
86
     * @return array
87
     */
88
    private function paginate($logs, $limit, $offset)
89
    {
90
        return array_slice($logs, (($offset - 1) * $limit), $limit);
91
    }
92
93
    /**
94
     * Compares the given two logs.
95
     *
96
     * @param array $log1 The first log
97
     * @param array $log2 The second log
98
     *
99
     * @return int
100
     */
101
    private function sort($log1, $log2)
0 ignored issues
show
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...
102
    {
103
        $orderBy = 'date';
104
        $order = 'asc';
105
106
        if (!empty($_GET['orderby'])) {
107
            $orderBy = $_GET['orderby'];
108
        }
109
        if (!empty($_GET['order'])) {
110
            $order = $_GET['order'];
111
        }
112
        $result = strcmp($log1[$orderBy], $log2[$orderBy]);
113
        if ('asc' === $order) {
114
            return $result;
115
        }
116
117
        return -$result;
118
    }
119
}
120