Passed
Push — master ( 41990d...862f39 )
by Gabor
09:16 queued 04:24
created

getEntityListFromDataSet()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 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
namespace WebHemi\Data\Storage\Traits;
13
14
use WebHemi\Data\Entity\DataEntityInterface;
15
16
/**
17
 * Class GetEntityListFromDataSetTrait
18
 */
19
trait GetEntityListFromDataSetTrait
20
{
21
    /**
22
     * Gets entity list from data storage set.
23
     *
24
     * @param bool|array $dataList
25
     * @return bool|array<DataEntityInterface>
26
     */
27 4
    protected function getEntityListFromDataSet($dataList)
28
    {
29 4
        $entityList = false;
30
31 4
        if (!empty($dataList)) {
32 4
            foreach ($dataList as $entityData) {
0 ignored issues
show
Bug introduced by
The expression $dataList of type boolean|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...
33
                /** @var DataEntityInterface $entity */
34 4
                $entity = $this->createEntity();
0 ignored issues
show
Bug introduced by
It seems like createEntity() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35 4
                $this->populateEntity($entity, $entityData);
0 ignored issues
show
Bug introduced by
It seems like populateEntity() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
36 4
                $entityList[] = $entity;
37 4
            }
38 4
        }
39
40 4
        return $entityList;
41
    }
42
}
43