Completed
Push — master ( d1d4a9...91184f )
by Peter
12:35
created

CreateModel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 37
ccs 6
cts 7
cp 0.8571
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A createModel() 0 20 3
1
<?php
2
3
4
namespace Maslosoft\Mangan\Traits\Finder;
5
6
7
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
8
use Maslosoft\Mangan\Exceptions\ManganException;
9
use Maslosoft\Mangan\Meta\ManganMeta;
10
use Maslosoft\Mangan\Transformers\RawArray;
11
12
trait CreateModel
13
{
14
	/**
15
	 * Create model from `$data`.
16
	 *
17
	 * NOTE: It requires `getModel` method accessible
18
	 * within this method.
19
	 *
20
	 * NOTE: Do not declare abstract `getModel` here
21
	 * as *possibly* it might be protected or public in classes
22
	 * using this trait.
23
	 *
24
	 * @param                    $data
25
	 * @return AnnotatedInterface
26
	 * @throws ManganException
27
	 */
28 83
	protected function createModel($data)
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
29
	{
30 83
		if (!empty($data['$err']))
31
		{
32
			throw new ManganException(sprintf("There is an error in query: %s", $data['$err']));
33
		}
34
35
		// By default create instances of same
36
		// type as provided model
37 83
		$model = $this->getModel();
0 ignored issues
show
Bug introduced by
It seems like getModel() 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...
38
39
		// For non homogeneous collections class
40
		// need to be taken from data, not defined
41
		// by model
42 83
		if(ManganMeta::create($model)->type()->homogenous === false)
43
		{
44 5
			$model = null;
45
		}
46 83
		return RawArray::toModel($data, $model);
47
	}
48
}