Completed
Push — master ( a6123a...0a636a )
by Peter
03:30
created

ParamsExpander::expand()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.0572

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 26
ccs 17
cts 19
cp 0.8947
rs 6.7273
cc 7
eloc 14
nc 18
nop 3
crap 7.0572
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link http://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Helpers;
16
17
use Maslosoft\Addendum\Interfaces\AnnotationInterface;
18
19
/**
20
 * Helper class for easier annotation params setting
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class ParamsExpander
25
{
26
27
	/**
28
	 * Expand params to annotation values.
29
	 * When annotation is used with multi top values, this will help assign this values to annotation.
30
	 * Examples of multi valued params include:
31
	 * <ul>
32
	 * <li>&commat;Annotation(Class\Literal, 2)</li>
33
	 * <li>&commat;Annotation(class = Class\Literal, items = 2)</li>
34
	 * </ul>
35
	 * Example of use:
36
	 * 		ParamsExpander::expand($annotation, ['class', 'items'])
37
	 * This will assign `$annotation->class` and `$annotation->items` with named or anonymous params (based on order of params).
38
	 * @param AnnotationInterface $annotation
39
	 * @param string[] $params List of parameters names in order.
40
	 * @param mixed[] $values Values used to expand params, if not set `$annotation->value` will be used if available.
41
	 * @return mixed[] Expanded params
42
	 */
43 2
	public static function expand(AnnotationInterface $annotation, $params, $values = [])
44
	{
45 2
		if (!empty($annotation->value))
1 ignored issue
show
Bug introduced by
Accessing value on the interface Maslosoft\Addendum\Interfaces\AnnotationInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
46 2
		{
47 2
			$values = $values? : (array) $annotation->value;
48
		}
49 2
		$data = [];
50 2
		foreach ($params as $key => $name)
51 2
		{
52 2
			if (isset($values[$key]))
53 2
			{
54
				$data[$name] = $values[$key];
55 2
				unset($values[$key]);
56 2
				continue;
57 2
			}
58 2
			if (isset($values[$name]))
59 2
			{
60 2
				$data[$name] = $values[$name];
61
			}
62
			if (isset($annotation->$name))
63 2
			{
64 2
				$data[$name] = $annotation->$name;
65
			}
66
		}
67
		return $data;
68
	}
69
70
}
71