Completed
Push — multimodel-dp ( 745c1d...bcd65b )
by Peter
03:53
created

MultiScopeManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 59.09%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 57
ccs 13
cts 22
cp 0.5909
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNewCriteria() 0 6 1
A getModelCriteria() 0 15 3
A getOneModelCriteria() 0 15 3
1
<?php
2
3
namespace Maslosoft\Manganel;
4
5
use Maslosoft\Mangan\Abstracts\AbstractScopeManager;
6
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
7
use Maslosoft\Manganel\Interfaces\ModelsAwareInterface;
8
use Maslosoft\Manganel\Traits\UniqueModelsAwareTrait;
9
10
/**
11
 * Scope Manager with support for many models
12
 *
13
 * TODO, maybe it's not even necessary? Will be known when SearchFinder will be
14
 * implemented...
15
 *
16
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
17
 */
18
class MultiScopeManager extends AbstractScopeManager implements ScopeManagerInterface,
19
		ModelsAwareInterface
20
{
21
22
	use UniqueModelsAwareTrait;
23
24
	/**
25
	 *
26
	 * @param object $model Base model used for criteria
27
	 * @param object[] $models Additional models used for criteria
28
	 */
29 4
	public function __construct($model, $models = [])
30
	{
31 4
		$this->setModel($model);
32 4
		$this->setModels($models);
33 4
	}
34
35
	public function getNewCriteria($criteria = null)
36
	{
37
		$newCriteria = new SearchCriteria($criteria);
38
		$newCriteria->decorateWith($this->getModel());
39
		return $newCriteria;
40
	}
41
42 3
	protected function getModelCriteria()
43
	{
44 3
		$criteria = new SearchCriteria;
45
46 3
		foreach ($this->getModels() as $model)
47
		{
48 3
			$criteria->mergeWith($this->getOneModelCriteria($model));
0 ignored issues
show
Bug introduced by
It seems like $this->getOneModelCriteria($model) can be null; however, mergeWith() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
49
		}
50
51 3
		if (empty($criteria))
52
		{
53
			return $this->getNewCriteria();
54
		}
55 3
		return $criteria;
56
	}
57
58 3
	private function getOneModelCriteria($model)
59
	{
60 3
		if ($model instanceof WithCriteriaInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Manganel\WithCriteriaInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
61
		{
62
			$criteria = $model->getDbCriteria();
0 ignored issues
show
Unused Code introduced by
$criteria is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
		}
64
		elseif ($model instanceof CriteriaAwareInterface)
0 ignored issues
show
Bug introduced by
The class Maslosoft\Manganel\CriteriaAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65
		{
66
			$criteria = $model->getCriteria();
0 ignored issues
show
Unused Code introduced by
$criteria is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
		}
68
		else
69
		{
70 3
			return new SearchCriteria;
71
		}
72
	}
73
74
}
75