Test Failed
Branch lab/data (a414ec)
by Gabor
07:41
created

ApplicationStorage::getApplicationList()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Data\Storage;
15
16
use WebHemi\Data\Query\QueryInterface;
17
use WebHemi\Data\Entity\EntitySet;
18
use WebHemi\Data\Entity\ApplicationEntity;
19
20
/**
21
 * Class ApplicationStorage.
22
 */
23
class ApplicationStorage extends AbstractStorage
24
{
25
     /**
26
     * Returns every Application entity.
27
     *
28
     * @param int $limit
29
     * @param int $offset
30
     * @return EntitySet
31
     */
32
    public function getApplicationList(
33
        int $limit = QueryInterface::MAX_ROW_LIMIT,
34
        int $offset = 0
35
    ) : EntitySet {
36
        $this->normalizeLimitAndOffset($limit, $offset);
37
38
        $data = $this->getQueryAdapter()->fetchData(
39
            'getApplicationList',
40
            [
41
                ':limit' => $limit,
42
                ':offset' => $offset
43
            ]
44
        );
45
46
        $entitySet = $this->createEntitySet();
47
48
        foreach ($data as $row) {
0 ignored issues
show
Bug introduced by
The expression $data of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
            /** @var ApplicationEntity $entity */
50
            $entity = $this->createEntity(ApplicationEntity::class, $row);
51
52
            if (!empty($entity)) {
53
                $entitySet[] = $entity;
54
            }
55
        }
56
57
        return $entitySet;
58
    }
59
60
    /**
61
     * Returns a Application entity identified by (unique) ID.
62
     *
63
     * @param  int $identifier
64
     * @return null|ApplicationEntity
65
     */
66
    public function getApplicationById(int $identifier) : ? ApplicationEntity
67
    {
68
        $data = $this->getQueryAdapter()->fetchData('getApplicationById', [':idApplication' => $identifier]);
69
70
        /** @var null|ApplicationEntity $entity */
71
        $entity = $this->createEntity(ApplicationEntity::class, $data[0] ?? []);
72
73
        return $entity;
74
    }
75
76
    /**
77
     * Returns an Application entity by name.
78
     *
79
     * @param  string $name
80
     * @return null|ApplicationEntity
81
     */
82
    public function getApplicationByName(string $name) : ? ApplicationEntity
83
    {
84
        $data = $this->getQueryAdapter()->fetchData('getApplicationByName', [':name' => $name]);
85
86
        /** @var null|ApplicationEntity $entity */
87
        $entity = $this->createEntity(ApplicationEntity::class, $data[0] ?? []);
88
89
        return $entity;
90
    }
91
}
92