Completed
Push — master ( 2b5327...235414 )
by Peter
09:57
created

IndexAnnotation::init()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 0
crap 20
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Annotations\Indexes;
15
16
use Maslosoft\Addendum\Helpers\ParamsExpander;
17
use Maslosoft\Mangan\Meta\IndexMeta;
18
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation;
19
use Maslosoft\Mangan\Sort;
20
21
/**
22
 * IndexAnnotation
23
 *
24
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
25
 */
26
class IndexAnnotation extends ManganPropertyAnnotation
27
{
28
29
	const Ns = __NAMESPACE__;
30
31
	public $value;
32
33
	/**
34
	 * This can be either:
35
	 *
36
	 * * Empty - for simple ascending index
37
	 * * `Sort::SortAsc` - for simple ascending index
38
	 * * `Sort::SortDesc` - for simple descending index
39
	 * * `array` - for any other keys specification
40
	 *
41
	 * @var mixed
42
	 */
43
	public $keys;
44
	public $options;
45
46
	public function init()
47
	{
48
		$data = (object)ParamsExpander::expand($this, ['keys', 'options']);
49
50
		$entity = $this->getEntity();
51
		$name = $entity->name;
52
		if(empty($data->keys))
53
		{
54
			$keys[$name] = Sort::SortAsc;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$keys was never initialized. Although not strictly required by PHP, it is generally a good practice to add $keys = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
		}
56
		else
57
		{
58
			if(!is_array($data->keys))
59
			{
60
				$keys[$name] = $data->keys;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$keys was never initialized. Although not strictly required by PHP, it is generally a good practice to add $keys = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
61
			}
62
			else
63
			{
64
				$keys = $data->keys;
65
			}
66
		}
67
		if(empty($data->options))
68
		{
69
			$data->options = [];
70
		}
71
		$entity->index[] = new IndexMeta($keys, $data->options);
72
	}
73
74
}
75