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

YamlStorage::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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 data collection.
16
     *
17
     * @var array
18
     */
19
    private $data;
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->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...
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 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...
38
    {
39
        $data = $this->data;
40
        foreach ($data as $key => $item) {
41
            if (array_key_exists('ID', $item)) {
42
                continue;
43
            }
44
            $data[$key]['ID'] = $key;
45
        }
46
        usort($data, [$this, 'sort']);
47
48
        return $this->paginate($data, $limit, $offset);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 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...
55
    {
56
        $data = [];
57
        foreach ($this->data as $key => $item) {
58
            foreach ($criteria as $name => $singleCriteria) {
59
                if ($item[$name] === $singleCriteria) {
60
                    $data[$key] = $item;
61
                }
62
            }
63
            if (array_key_exists('ID', $item)) {
64
                continue;
65
            }
66
            $data[$key]['ID'] = $key;
67
        }
68
        usort($data, [$this, 'sort']);
69
70
        return $this->paginate($data, $limit, $offset);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function properties()
77
    {
78
        return array_keys($this->data[0]);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function size()
85
    {
86
        return count($this->data);
87
    }
88
89
    /**
90
     * Paginates the given collection of data.
91
     *
92
     * @param array $data   The collection data
93
     * @param int   $limit  Logs per page
94
     * @param int   $offset The number of the page
95
     *
96
     * @return array
97
     */
98
    private function paginate($data, $limit, $offset)
99
    {
100
        return array_slice($data, (($offset - 1) * $limit), $limit);
101
    }
102
103
    /**
104
     * Compares the given two items.
105
     *
106
     * @param mixed $item1 The first item
107
     * @param mixed $item2 The second item
108
     *
109
     * @return int
110
     */
111 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...
112
    {
113
        $orderBy = 'name';
114
        $order = 'asc';
115
116
        if (!empty($_GET['orderby'])) {
117
            $orderBy = $_GET['orderby'];
118
        }
119
        if (!empty($_GET['order'])) {
120
            $order = $_GET['order'];
121
        }
122
        $result = strcmp($item1[$orderBy], $item2[$orderBy]);
123
        if ('asc' === $order) {
124
            return $result;
125
        }
126
127
        return -$result;
128
    }
129
}
130