Passed
Push — master ( 9eb36d...479891 )
by Artem
01:50
created

ArrayRepository::getMeta()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prozorov\Repositories;
6
7
use Prozorov\Repositories\Contracts\RepositoryInterface;
8
use Prozorov\Repositories\Traits\HasMeta;
9
use Prozorov\Repositories\{Query, Result};
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Prozorov\Repositories\Result. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Prozorov\Repositories\Query. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Prozorov\Repositories\Exceptions\DataNotFound;
11
use Illuminate\Support\Collection;
12
13
class ArrayRepository implements RepositoryInterface
14
{
15
    use HasMeta;
16
17
    /**
18
     * @var Collection $data
19
     */
20
    protected $data;
21
22
    /**
23
     * @var string $idField
24
     */
25
    protected $idField;
26
27 15
    public function __construct(array $data, string $idField = 'id')
28
    {
29 15
        $this->data = (new Collection($data))->keyBy('id');
30 15
        $this->idField = $idField;
31 15
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 1
    public function create(array $data)
37
    {
38 1
        $this->data->put($data[$this->idField], $data);
39 1
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 1
    public function update($model, array $data): void
45
    {
46 1
        if (is_int($model)) {
47 1
            $model = $this->data->get($model);
48
49 1
            $this->data->put($model[$this->idField], array_merge($model, $data));
50
        }
51 1
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 1
    public function delete(int $id): void
57
    {
58 1
        $this->data->forget($id);
59 1
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1
    public function exists(array $filter): bool
65
    {
66 1
        $data = $this->getRaw((new Query())->where($filter));
67
68 1
        return ! empty($data);
69
    }
70
71
    /**
72
     * @inheritDoc
73
     */
74 5
    public function count(array $filter = []): int
75
    {
76 5
        $data = $this->getRaw((new Query())->where($filter));
77
78 5
        return count($data);
79
    }
80
81
    /**
82
     * @inheritDoc
83
     */
84 2
    public function get(Query $query): Result
85
    {
86 2
        return new Result(
87 2
            $this->getRaw($query),
0 ignored issues
show
Bug introduced by
It seems like $this->getRaw($query) can also be of type null; however, parameter $data of Prozorov\Repositories\Result::__construct() does only seem to accept iterable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            /** @scrutinizer ignore-type */ $this->getRaw($query),
Loading history...
88 2
            $this->getMeta($query)
89
        );
90
    }
91
92
    /**
93
     * @inheritDoc
94
     */
95
    public function first(array $filter)
96
    {
97
        $data = $this->getRaw((new Query())->where($filter)->limit(1));
98
99
        if (empty($data[0])) {
100
            return null;
101
        }
102
103
        return $data[0];
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109 6
    public function getById(int $id, array $select = null)
110
    {
111 6
        return $this->data->get($id);
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117 1
    public function getByIdOrFail(int $id, array $select = null)
118
    {
119 1
        $data = $this->getById($id, $select);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $data is correct as $this->getById($id, $select) targeting Prozorov\Repositories\ArrayRepository::getById() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
121 1
        if (empty($data)) {
122 1
            throw new DataNotFound();
123
        }
124
125
        return $data;
126
    }
127
128
    /**
129
     * Returns raw data
130
     *
131
     * @access	protected
132
     * @param	Query	$query	
133
     * @return	iterable|null
134
     */
135 7
    protected function getRaw(Query $query)
136
    {
137 7
        $data = $this->data;
138
139 7
        if (! empty($query->getWhere())) {
140 3
            foreach ($query->getWhere() as $key => $value) {
141 3
                $data = $this->data->where($key, $value);
142
            }
143
        }
144
145 7
        return $data->values()->toArray();
146
    }
147
}
148